/////////////////////////////////////////
// methods.js
/////////////////////////////////////////



/////////////////////////////////////////
/////////////////////////////////////////
// open windows
/////////////////////////////////////////
/////////////////////////////////////////


/////////////////////////////////////////
// designfenzider shop 
/////////////////////////////////////////
function showShop(shopLink) {
	windowW=screen.availWidth*0.9;
	windowH=screen.availHeight*0.9;
	windowX=(screen.availWidth/2)-(windowW/2);
	windowY=(screen.availHeight/2)-(windowH/2);

	sizeParam = 'left='+windowX+',top="'+windowY+',screenX='+windowX+',screenY='+windowY+',width='+windowW+',height='+windowH+' ';
	
	shopUrl = 'http://designfenzider.com/ShoppingNew/index.html';
	//shopUrl = 'http://dfobjects.com/qaz/mk/shop/index.html';
	if (shopLink != '') {
	   shopUrl += "#" + shopLink;
	 }
	
	window.open(shopUrl,'', sizeParam + ',scrollbars=yes,menubar=no,resizable=yes,toolbar=yes,location=no,status=no');
}

/////////////////////////////////////////
// mailing form
/////////////////////////////////////////
function showMailing() {
	window.open('mailing.html','','width=500,height=500,scrollbars=yes,menubar=no,resizable=yes,toolbar=no,location=no,status=no');
}

/////////////////////////////////////////
// employment info
/////////////////////////////////////////
function showEmployment() {
	window.open('employment.html','','width=500,height=500,scrollbars=yes,menubar=no,resizable=yes,toolbar=no,location=no,status=no');
}

/////////////////////////////////////////
// credits
/////////////////////////////////////////
function showCredits() {
	window.open('credits.html','','width=500,height=500,scrollbars=yes,menubar=no,resizable=yes,toolbar=no,location=no,status=no');
}

/////////////////////////////////////////
// main page
/////////////////////////////////////////
function showMain() {
	window.open('about.html', '','fullscreen=yes,scrollbars=yes,menubar=no,resizable=yes,toolbar=no,location=no,status=no');
}


/////////////////////////////////////////
/////////////////////////////////////////
// email
/////////////////////////////////////////
/////////////////////////////////////////
function sendEml(subject, pre, domain, top, ccpre, ccdomain, cctop) {
	text = "mai" + "lto:" + pre + "@" + domain + "." + top;
	if (subject) {
		text += "?subject=" + subject;
	}
	if (ccpre) {
		if (subject) {
			text += "&";
		}
		else {
			text += "?";
		}
		text += "cc=" + ccpre + "@" + ccdomain + "." + cctop;
	}
	window.location = text;
}


/////////////////////////////////////////
/////////////////////////////////////////
// hilight for object and news selection
/////////////////////////////////////////
/////////////////////////////////////////

// global current hilite if any
var globalHilite = "";

function hilite(item) {
		 if (globalHilite != "") {
		 	$(globalHilite).style.fontWeight = "normal";		 	
		 }
		 globalHilite = item;
		 
		 $(item).style.fontWeight = "bold";
}

/////////////////////////////////////////
/////////////////////////////////////////
// objects 
/////////////////////////////////////////
/////////////////////////////////////////

/////////////////////////////////////////
// load category list for objects
/////////////////////////////////////////
function loadCategories() {
	myHtml = "";
	for (i=0; i<categoryList.length; i++) {
		currentCategory = categoryList[i];
		myHtml += '<div id="category-' + i + '"><a href="javascript:showCategory(' + i + ');">' + currentCategory + '</a></div>';
	}
	setHTML("submenu", myHtml);
	showCategory(0);
}

/////////////////////////////////////////
// show all objects of a category
/////////////////////////////////////////
function showCategory(categoryNumber) {



	hilite('category-' + categoryNumber);
	
   	myHtml = "";
	currentCategory = categoryList[categoryNumber];
	currentCategoryObjects = allCategories[currentCategory];
    
	// go through all objects in category
    for (k=0; k<currentCategoryObjects.length; k++) {
    	objectName = currentCategoryObjects[k];
   				
       	// get info
       	currentObject = allObjects[objectName];
      	currentDir = currentObject.dir;
      	currentName = currentObject.name;
    	currentDescription = currentObject.description;
    	currentPicList = currentObject.pics;
    	numPics = currentPicList.length;
    	photoNumber = 0;
    	currentPicName = currentPicList[photoNumber];
    	currentShopLink = currentObject.shop;
		
    	// photo and nav
    	///////////
    	myHtml += '<div id="object-image-' + objectName + '" class="object-image">';
		myHtml += '<img height=300 id="the-photo-' + objectName + '" class="the-photo" src="images/loading.gif">';
		myHtml += '</div>';
    	myHtml += '<div id="object-nav-' + objectName + '" class="object-nav"></div><br>';
		if (numPics > 1) myHtml += '<br>';

    	// name
    	///////////
    	myHtml += '<div id="object-name' + objectName + '" class="object-name">' + currentName + '</div>';

    	// description
    	///////////	
    	myHtml += '<div id="object-description' + objectName + '" class="object-description">' + currentDescription + '</div>';

		myHtml += '<br>';

		// shop link
		if (currentShopLink != "" ) {
		   myHtml += '<div class="object-buy"><a style="text-decoration:underline" href="javascript:showShop(' + "'" + currentShopLink + "'" + ');">buy</a></div>';
		}
		myHtml += '<br>';
		   
		// grey line
		myHtml += '<img src="images/greypixel.gif" height=1 width=400>';
		myHtml += '<br><br><br><br>';
    }
	myHtml += '<div class="object-buy"><a href="#top">top</a></div><br><br><br>';
	setHTML("left", myHtml);	
	
	window.scrollTo(0,0);
		
	// set photos (can't set until content is written out)
    for (k=0; k<currentCategoryObjects.length; k++) {
    	objectName = currentCategoryObjects[k];
    	showPhoto(objectName, 0);			
	}
}

/////////////////////////////////////////
// show a specific object photo on the object page
/////////////////////////////////////////
function showPhoto(objectName, photoNumber) {
	// get photo info	
	currentObject = allObjects[objectName];
	currentDir = currentObject.dir;
	currentPicList = currentObject.pics;
	numPics = currentPicList.length;

	if (photoNumber < 0) {
		photoNumber = numPics-1;
	}
	else if (photoNumber >= numPics) {
		photoNumber = 0;
	}

	currentPicName = currentPicList[photoNumber];

	if (!isGif(currentPicName)) {
		currentPicName += ".jpg";
	}

  	// create nav bar
  	currentObjectNav = "";
	if (numPics > 1) {
	  	// left arrow
	  	currentObjectNav += "<span class='l-text'><a href='javascript:showPhoto(" + '"' + objectName + '",' + (photoNumber-1) + ");'>&lt;&nbsp</a></span>";
	  	// loop through pictures
	  	for (i=0; i < numPics; i++) {
	  		currentObjectNav += "<a href='javascript:showPhoto(" + '"' + objectName + '",' + i + ");'>";
			if (i == photoNumber) {
  			   currentObjectNav += "<b>";
			}
   			currentObjectNav += (i+1)
  			if (i == photoNumber) {
  			   currentObjectNav += "</b>";
  			}
  			currentObjectNav += "</a> ";
  		}
  		// right arrow
  		currentObjectNav += "<span class='l-text'><a href='javascript:showPhoto(" + '"' + objectName + '",' + (photoNumber+1) + ");'>&gt;&nbsp;</a></span>";
	}
		
	// display nav bar
	setHTML('object-nav-' + objectName, currentObjectNav);

	// update image	
	myImage = "objects/" + currentDir + "/" + currentPicName;

	loadImage('the-photo-' + objectName, myImage, false);

}


/////////////////////////////////////////
/////////////////////////////////////////
// news
/////////////////////////////////////////
/////////////////////////////////////////

/////////////////////////////////////////
// fill year list of news on news page
/////////////////////////////////////////
function loadNews() {
	myHtml = "";
	for (i=0; i<newsYearsList.length; i++) {
		currentNews = newsYearsList[i];
		myHtml += '<div id="news-' + i + '"><a href="javascript:showNews(' + i + ');">' + currentNews + '</a></div>';
	}
	setHTML("submenu", myHtml);
	showNews(0);
}




/////////////////////////////////////////
// show news for a given year
/////////////////////////////////////////
function showNews(categoryNumber) {
	hilite('news-' + categoryNumber);
	
   	myHtml = "";
	currentYear = newsYearsList[categoryNumber];
	currentYearNews = allYears[currentYear];
    
	// go through all news in year
    for (k=0; k<currentYearNews.length; k++) {
    	newsName = currentYearNews[k];
		
       	// get info
       	currentNews = allNews[newsName];
		currentDescription = currentNews.description;
		currentDate = currentNews.date;
		currentDir = currentNews.dir;
		currentPic = currentNews.pic;
		currentText = currentNews.text;
		
    	// photo
    	///////////
		if (currentPic != "") {
	      	myHtml += '<div id="news-image-' + newsName + '" class="news-image">';
			if (currentPic) {
			   //myImage = currentDir + "/" + currentPic + ".jpg";
			   myHtml += '<img border="0" id="the-photo-' + newsName + '" class="the-photo" src="images/loading_l.gif">';
			}
  			myHtml += '</div>';
		}
	
	   	// description
    	///////////	
    	myHtml += '<br><div id="news-description-' + newsName + '" class="news-description">' + currentDescription + '</div>';
		if (currentDate) {
		   myHtml += '<div id="news-description-' + newsName + '" class="news-description">' + currentDate + '</div>';
		}
		myHtml += '<br><br>';

		// text
		if (currentText != "") {
	      	myHtml += '<div id="news-text-' + newsName + '" class="news-description">';
			myHtml += currentText;
  			myHtml += '</div>';
			myHtml += '<br><br>';
		}

		// grey line
		myHtml += '<img src="images/greypixel.gif" height=1 width=400>';
		myHtml += '<br><br><br>';
    }
	myHtml += '<div class="object-description"><a href="#top">top</a></div><br><br><br>';
	setHTML("left", myHtml);	

	// set photos (can't set until content is written out)
    for (k=0; k<currentYearNews.length; k++) {
    	newsName = currentYearNews[k];
       	currentNews = allNews[newsName];
		currentPic = currentNews.pic;
		if (currentPic) {
				currentDir = currentNews.dir;
				currentPic = 
				myImage = "news/" + currentDir + "/" + currentPic + ".jpg";
				loadImage('the-photo-' + newsName, myImage, true);
		}
	}
	window.scrollTo(0,0);
}


/////////////////////////////////////////
// misc helper
/////////////////////////////////////////


function isGif(text) {
	return text.match(/\.gif$/i);
}

/////////////////////////////////////////
// helper methods for dhtml updates
/////////////////////////////////////////
function setHTML(element, text) {
	$(element).innerHTML = text;
}

function getHTML(item) {
	text = $(item).innerHTML;
	return text;
}
	

/////////////////////////////////////////
// image loading
/////////////////////////////////////////
var dImage = new Array();
var dImgPreloader = new Array();
 
function loadImage(imageElement, imageName, large) {

	dImage[imageElement] = $(imageElement);
	
	if (!large) {
	   loadingImage = 'images/loading.gif';
	}
	else {
	   loadingImage = 'images/loading_l.gif';
	}
	  
	dImage[imageElement].src = loadingImage;

	dImgPreloader[imageElement] = new Image();
	dImgPreloader[imageElement].onload=function(){
		dImage[imageElement].src = imageName;
		dImgPreloader[imageElement] = null;
	}
	dImgPreloader[imageElement].src = imageName;
}

/////////////////////////////////////////
// get html element
/////////////////////////////////////////
function $(el) {
	if (typeof el == 'string') el = document.getElementById(el);
		return el;
}

/////////////////////////////////////////
// show/hide divs
/////////////////////////////////////////
function show(element) {
	$(element).style.display = 'block';
    $(element).style.visibility = 'visible';
}

function hide(element) {
		$(element).style.display = 'none';
		$(element).style.visibility = 'hidden';
}


