// General Functions Start
document.getElementsByClassName = function(clsName){
    var retVal = new Array();
    var elements = document.getElementsByTagName("*");
    for(var i = 0;i < elements.length;i++){
        if(elements[i].className.indexOf(" ") >= 0){
            var classes = elements[i].className.split(" ");
            for(var j = 0;j < classes.length;j++){
                if(classes[j] == clsName)
                    retVal.push(elements[i]);
            }
        }
        else if(elements[i].className == clsName)
            retVal.push(elements[i]);
    }
    return retVal;
}

function browserOutOfDate() {
	alert("Get a new Browser");
}
// General Functions End

// AJAX Functions Start
function createHttpRequest() {
	ajax = false;
	if (window.XMLHttpRequest) {
		ajax = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		if (ActiveXObject("Msxml2.XMLHTTP")) {
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
		} if (ActiveXObject("Microsoft.XMLHTTP")) {
			ajax = new ActiveXObject("Microsoft.XMLHTTP");
		} 
	}
	if (!ajax) {
		browserOutOfDate();
	}
}

function handleAjaxResponse() {
	if ( (this.readyState == 4) && (this.status == 200) ) {
	} 
}

function openAjaxRequest(type, url) {
	url1 = 'ajax.php?type='+type+"&amp;stupidIe="+Math.round(new Date().getTime()/1000.0);
	url2 = url1+url;
	ajax.open('get', url2, true);
	ajax.send(null);
	ajax.onreadystatechange = handleAjaxResponse;
}

function initAjaxRequests() {
	createHttpRequest()
}

// AJAX Functions End

// Nav Functions Start
function mouseOffA() {
	this.style.color = "#494949";
}

function mouseOverA() {
	var randomColors = Array("#0405fb","#fd0201","#34c936");
	this.style.color = randomColors[Math.floor(Math.random()*randomColors.length)];
}

function mouseOverLink() {
	for (var h = 0; h < document.getElementsByClassName("subMenuDiv").length; h++) {
		document.getElementsByClassName("subMenuDiv")[h].style.display = "none";
	}
	document.getElementById("d"+this.id+"subMenuDiv").style.display = "block";
	for (var j = 0; j < document.getElementsByClassName("navItem").length; j++) {
		document.getElementsByClassName("navItem")[j].style.borderBottom = "0px none #CCCCCC";
	}
	this.style.borderBottom = "6px solid #CCCCCC";
}

function setNavEvents() {
	for (var i = 0; i < document.getElementsByClassName("navItem").length; i++) {
		document.getElementsByClassName("navItem")[i].onmouseover = mouseOverLink;
	}
	for (var k = 0; k < document.getElementsByClassName("link").length; k++) {
		document.getElementsByClassName("link")[k].onmouseover = mouseOverA;
		document.getElementsByClassName("link")[k].onmouseout = mouseOffA;
	}
	for (var k = 0; k < document.getElementsByClassName("contactUs_link").length; k++) {
		document.getElementsByClassName("contactUs_link")[k].onmouseover = mouseOverA;
		document.getElementsByClassName("contactUs_link")[k].onmouseout = mouseOffA;
	}
	document.getElementById("a1").style.borderBottom = "6px solid #CCCCCC";
}
// Nav Functions End

// News Functions Start
function scaleDownNews() {
	scaleSizeD = document.getElementById(div_id).offsetHeight + 40;
	if (scaleSizeD >= newsDivSizes[div_id]+40) {
		clearInterval (scaleDown);
		document.getElementById(div_id).style.height = newsDivSizes[div_id]+"px";
	} else {
		document.getElementById(div_id).style.height = scaleSizeD+"px";
	}	
}
	
function scaleUpNews() {
	scaleSizeU = document.getElementById(div_id).offsetHeight - 40;
	if (scaleSizeU <= "70") {
		clearInterval (scaleUp);
		document.getElementById(div_id).style.height = "30px";
		document.getElementById("tri_"+div_id).style.background = "url(images/news_tri.jpg) no-repeat center bottom";
	} else {
		document.getElementById(div_id).style.height = scaleSizeU+"px";
	}
}

function onclickNewsItem() {
	//document.getElementById("div_"+this.id).style.height = document.getElementById("div_"+this.id).offsetHeight;
	step = 0;
	div_id = "div_"+this.id;
	if (document.getElementById(div_id).offsetHeight != "38") {
		if (typeof( window[ 'scaleUp' ] ) != "undefined" ) {
			clearInterval (scaleUp);
		}
		scaleUp = setInterval('scaleUpNews()', 20);
	} else {
		if (typeof( window[ 'scaleDown' ] ) != "undefined" ) {
			clearInterval (scaleDown);
		}
		document.getElementById("tri_"+div_id).style.background = "url(images/news_triRev.jpg) no-repeat center bottom";
		scaleDown = setInterval('scaleDownNews()', 20);
		//document.getElementById("div_"+this.id).style.height = newsDivSizes["div_"+this.id]+"px";
	} 
	//alert(newsDivSizes["div_"+this.id]);
	//alert(document.getElementById("div_"+this.id).offsetHeight);
	return false;
}

function initNewsEvents() {
	newsDivSizes = Array();
	for (var l = 0; l < document.getElementsByClassName("news_title").length; l++) {
		newsDivSizes["div_"+document.getElementsByClassName("news_title")[l].id] = 
		document.getElementById("div_"+document.getElementsByClassName("news_title")[l].id).offsetHeight;
		document.getElementsByClassName("news_title")[l].onclick = onclickNewsItem;
		document.getElementById("div_"+document.getElementsByClassName("news_title")[l].id).style.height = "30px";
	}
}
// News Functions End

// Browser Recom Functions Start
function fireFoxMouseOver() {
	document.getElementById("firefox").src = "images/firefox.png";
	document.getElementById("firefoxTe").style.color= "rgba(0,0,0,0.8)";
}

function safariMouseOver() {
	document.getElementById("safari").src = "images/safari.png";
	document.getElementById("safariTe").style.color= "rgba(0,0,0,0.8)";
}

function fireFoxMouseOut() {
	document.getElementById("firefox").src = "images/firefox_gray.png";
	document.getElementById("firefoxTe").style.color= "rgba(0,0,0,0.52)";
}

function safariMouseOut() {
	document.getElementById("safari").src = "images/safari_gray.png";
	document.getElementById("safariTe").style.color= "rgba(0,0,0,0.52)";
}

function closeBrowserRecomWindow() {
	document.getElementById("browser_recom").style.display = "none";
	var url = 'ajax.php?type=cookie&name=browserRecom&expire=86400'+"&amp;stupidIe="+Math.round(new Date().getTime()/1000.0);
	ajax.open('get', url, true);
	ajax.send(null);
	return false;
}

function initBrowserRecomEvents () {
	if (document.getElementById("firefoxIm")) {document.getElementById("firefoxIm").onmouseover = fireFoxMouseOver;}
	if (document.getElementById("safariIm")) {document.getElementById("safariIm").onmouseover = safariMouseOver;}
	if (document.getElementById("firefoxTe")) {document.getElementById("firefoxTe").onmouseover = fireFoxMouseOver;}
	if (document.getElementById("safariTe")) {document.getElementById("safariTe").onmouseover = safariMouseOver;}
	if (document.getElementById("firefoxIm")) {document.getElementById("firefoxIm").onmouseout = fireFoxMouseOut;}
	if (document.getElementById("safariIm")) {document.getElementById("safariIm").onmouseout = safariMouseOut;}
	if (document.getElementById("firefoxTe")) {document.getElementById("firefoxTe").onmouseout = fireFoxMouseOut;}
	if (document.getElementById("safariTe")) {document.getElementById("safariTe").onmouseout = safariMouseOut;}
	if (document.getElementById("browser_recomClose")) {document.getElementById("browser_recomClose").onclick = closeBrowserRecomWindow;}
}
// Browser Recom Functions End

// Popup Functions Start
function initPopupEvents() {
	document.getElementById("popUp_close").onmouseover = hover_closePopup;
	document.getElementById("popUp_close").onmouseout = hover_closePopup_reset;
	document.getElementById("popUp_close").onclick = closePopup;
	document.getElementById("popUp_close_album").onmouseover = hover_closePopupA;
	document.getElementById("popUp_close_album").onmouseout = hover_closePopup_resetA;
	document.getElementById("popUp_close_album").onclick = closePopup;
	for (var k = 0; k < document.getElementsByClassName("ajax_link").length; k++) {
		document.getElementsByClassName("ajax_link")[k].onclick = openPopup;
	}
}

function initPopupCloseBtnEvents() {
	document.getElementById("popUp_close").style.display = "";
	document.getElementById("popUp_close").onmouseover = hover_closePopup;
	document.getElementById("popUp_close").onmouseout = hover_closePopup_reset;
	document.getElementById("popUp_close").onclick = closePopup;
}

function initPopup_album_CloseBtnEvents() {
	document.getElementById("popUp_close_album").style.display = "";
	document.getElementById("popUp_close_album").onmouseover = hover_closePopupA;
	document.getElementById("popUp_close_album").onmouseout = hover_closePopup_resetA;
	document.getElementById("popUp_close_album").onclick = closePopup;
}

function hover_closePopup() {
	document.getElementById(this.id+"_img").src = "images/close_popUp_hov.png";
}

function hover_closePopup_reset() {
	document.getElementById(this.id+"_img").src = "images/close_popUp.png";
}

function hover_closePopupA() {
	document.getElementById(this.id+"_img").src = "images/close_popUp_hov.png";
}

function hover_closePopup_resetA() {
	document.getElementById(this.id+"_img").src = "images/close_popUp.png";
}

function closePopup() {
	document.getElementById("popUp_bg").style.display = "none";
	document.getElementById("popUp").style.display = "none";
	document.getElementById("popUp_album").style.display = "none";
	document.getElementById("popUp_close").style.display = "none";
	document.getElementById("popUp_close_album").style.display = "none";
	for (var n = 0; n< document.getElementsByTagName("html").length; n++) {
		document.getElementsByTagName("html")[n].style.overflow = "auto";
	}
	window.scrollBy(0,-1);
	return false;
}

function openPopup() {
	for (var m = 0; m< document.getElementsByTagName("html").length; m++) {
		document.getElementsByTagName("html")[m].style.overflow = "hidden";
	}
	document.getElementById("popUp_close").style.display = "block";
	document.getElementById("popUp_bg").style.display = "block";
	document.getElementById("popUp").style.display = "block";
	document.getElementById("popUp").innerHTML = "<span class='loading'>loading...</span>";
	var url = 'ajax.php?type='+this.name+"&amp;stupidIe="+Math.round(new Date().getTime()/1000.0);
	ajax.open('get', url, true);
	ajax.send(null);
	ajax.onreadystatechange = function () {
		if ( (this.readyState == 4) && (this.status == 200) ) {
		document.getElementById("popUp").innerHTML = genCloseBtn_popUp();
		document.getElementById("popUp").innerHTML += this.responseText;
		initPopupCloseBtnEvents();
		initFormEvents();
		} 
	}
	return false;
}

function openPopup_album(id) {
	for (var m = 0; m< document.getElementsByTagName("html").length; m++) {
		document.getElementsByTagName("html")[m].style.overflow = "hidden";
	}
	document.getElementById("popUp_bg").style.display = "block";
	document.getElementById("popUp_album").style.display = "block";
	document.getElementById("popUp_album").innerHTML = "<span class='loading'>loading...</span>";
	var url = 'ajax.php?type=photo_album'+"&id="+id+"&amp;stupidIe="+Math.round(new Date().getTime()/1000.0);
	ajax.open('get', url, true);
	ajax.send(null);
	ajax.onreadystatechange = function () {
		if ( (this.readyState == 4) && (this.status == 200) ) {
		document.getElementById("popUp_album").innerHTML = genCloseBtn_popUp_album();
		document.getElementById("popUp_album").innerHTML += this.responseText;
		resizeImageRoll();
		initPopup_album_CloseBtnEvents();
		initPhotoThumbEvents();
		} 
	}
	return false;
}

function genCloseBtn_popUp() {
	return '<div class="close_popUp"><a href="#" id="popUp_close"><img id="popUp_close_img" src="images/close_popUp.png" /></a></div>';
}

function genCloseBtn_popUp_album() {
	return '<div class="close_popUp_album"><a href="#" id="popUp_close_album"><img id="popUp_close_album_img" src="images/close_popUp.png" /></a></div>';
}

// Popup Funcitons End

// Form Handler Functions Start
function initFormEvents() {
	for (var o=0; o < document.getElementsByClassName("req_formText").length; o++) {
		document.getElementsByClassName("req_formText")[o].onfocus = focusText;
		document.getElementsByClassName("req_formText")[o].onblur = blurText_req;
		document.getElementsByClassName("req_formText")[o].onmouseover = overText_req;
		document.getElementsByClassName("req_formText")[o].onmouseout = outText_req;
		
	}
	for (var d=0; d < document.getElementsByClassName("formText").length; d++) {
		document.getElementsByClassName("formText")[d].onfocus = focusText;
		document.getElementsByClassName("formText")[d].onblur = blurText;	
		document.getElementsByClassName("formText")[d].onmouseover = overText;
		document.getElementsByClassName("formText")[d].onmouseout = outText;
	}
	for (var e=0; e < document.getElementsByTagName("button").length; e++) {
		if (document.getElementsByTagName("button")[e].name == "submit") {
			document.getElementsByTagName("button")[e].onclick = submitForm;
		}
	}
	for (var f=0; f < document.getElementsByTagName("form").length; f++) {
		document.getElementsByTagName("form")[f].onsubmit = function () {return false};
	}
}

function submitForm() {
	for (var t=0; t < document.getElementsByClassName("req_formText").length; t++) {
		document.getElementsByClassName("req_formText")[t].style.background = "";
		document.getElementsByClassName("req_formText")[t].style.backgroundColor = "#ffb2ad";
		document.getElementsByClassName("req_formText")[t].style.color = "#fff";
		document.getElementsByClassName("req_formText")[t].style.border = "1px solid #ff735b";
	}
	formHtml = "";
	formErrors=0;
	for (var f=0; f < document.getElementsByClassName("form").length; f++) {
		if (document.getElementsByClassName("form")[f].tagName == "INPUT") {
			formHtml += "&"+document.getElementsByClassName("form")[f].name + "=" + document.getElementsByClassName("form")[f].value;
		} else if (document.getElementsByClassName("form")[f].tagName == "TEXTAREA") {
			formHtml += "&"+document.getElementsByClassName("form")[f].name + "=" + document.getElementsByClassName("form")[f].value;
		}
	}
	for (var r=0; r < document.getElementsByClassName("req_formText").length; r++) {
		if ((document.getElementsByClassName("req_formText")[r].value == "") || 
		(document.getElementsByClassName("req_formText")[r].value == document.getElementsByClassName("req_formText")[r].name)) {
			formErrors ++;
			alert(document.getElementsByClassName("req_formText")[r].name+" is required");
			errorText(document.getElementsByClassName("req_formText")[r]);
		}
	}
	if (formErrors == 0) {
		openAjaxRequest("form", formHtml);
		closePopup()
	}
	
}

function errorText(element) {
	element.style.backgroundColor = "";
	element.style.background= "#ff9f9d url(images/error.png) no-repeat scroll right center";
}

function focusText() {
	this.style.backgroundColor = "#fff";
	this.style.color = "#313131";
	this.style.border = "1px solid #9c9c9c";
	this.onmouseover = null;
	this.onmouseout = null;
	if (this.tagName != "TEXTAREA") {
		if (this.value == this.name) {
			this.value = "";
		}
	} else {
		if (this.innerHTML == this.name) {
			this.innerHTML = "";
		}
	}
}

function blurText() {
	this.style.backgroundColor = "#ddd";
	this.style.color = "#fff";
	this.style.border = "1px solid #9c9c9c";
	this.onmouseover = overText;
	this.onmouseout = outText;
	if (this.tagName != "TEXTAREA") {
		if (this.value == "") {
			this.value = this.name;
		}
	} else {
		if (this.innerHTML == "") {
			this.innerHTML = this.name;
		}
	}
	
}

function blurText_req() {
	this.style.backgroundColor = "#ffb2ad";
	this.style.color = "#fff";
	this.style.border = "1px solid #ff735b";
	this.onmouseover = overText_req;
	this.onmouseout = outText_req;
	if (this.tagName != "TEXTAREA") {
		if (this.value == "") {
			this.value = this.name;
		}
	} else {
		if (this.value == "") {
			this.innerHTML = this.name;
		}
	}
}

function overText_req() {
	this.style.backgroundColor = "#fcc3be";
	this.style.color = "#fff";
	this.style.border = "1px solid #ff735b";
}

function outText_req() {
	this.style.backgroundColor = "#ffb2ad";
	this.style.color = "#fff";
	this.style.border = "1px solid #ff735b";
}

function overText() {
	this.style.backgroundColor = "#eaeaea";
	this.style.color = "#fff";
	this.style.border = "1px solid #9c9c9c";
}

function outText() {
	this.style.backgroundColor = "#ddd";
	this.style.color = "#fff";
	this.style.border = "1px solid #9c9c9c";
}
// Form Handler Functions End

// Photo Album Functions Start
function initPhotoEvents() {
	for (var k = 0; k < document.getElementsByClassName("albumThumb").length; k++) {
		document.getElementsByClassName("albumThumb")[k].onmouseover = mouseOverAlbumThumb;
		document.getElementsByClassName("albumThumb")[k].onmouseout = mouseOffAlbumThumb;
		document.getElementsByClassName("albumThumb")[k].onclick = mouseClickAlbumThumb;
	}
}

function initPhotoThumbEvents() {
	for (var j = 0; j < document.getElementsByClassName("photoThumb").length; j++) {
		document.getElementsByClassName("photoThumb")[j].onmouseover = mouseOverPhotoThumb;
		document.getElementsByClassName("photoThumb")[j].onmouseout = mouseOffPhotoThumb;
		document.getElementsByClassName("photoThumb")[j].onclick = switchMainPhoto;
	}
}

function mouseOverAlbumThumb() {
	this.style.background = "url(images/albumThumb_hover.jpg) repeat-x";
	//document.getElementById(this.id+"Image").style.opacity = "1";
	
}

function mouseOffAlbumThumb() {
	this.style.background = "url(images/albumThumb.jpg) repeat-x";	
	//document.getElementById(this.id+"Image").style.opacity = ".6";
}

function mouseClickAlbumThumb() {
	openPopup_album(this.id);
	return false;
}

function mouseOverPhotoThumb() {
	this.style.backgroundColor = "#e0e0e0";
}

function mouseOffPhotoThumb() {
	this.style.backgroundColor = "#c1c1c1";
}

function mouseOutSelectPhotoThumb() {
	this.style.backgroundColor = "#898989";
}

function switchMainPhoto() {
	for (var j = 0; j < document.getElementsByClassName("photoThumb").length; j++) {
		document.getElementsByClassName("photoThumb")[j].style.backgroundColor = "#c1c1c1";
		document.getElementsByClassName("photoThumb")[j].onmouseout = mouseOffPhotoThumb;
	}
	
	document.getElementById("select_photo").innerHTML = "<span class='loading'>loading...</span>";
	var url = 'ajax.php?type=photo&id='+this.id+"&amp;stupidIe="+Math.round(new Date().getTime()/1000.0);
	ajax.open('get', url, true);
	ajax.send(null);
	ajax.onreadystatechange = function () {
		if ( (this.readyState == 4) && (this.status == 200) ) {
		document.getElementById("select_photo").innerHTML = this.responseText;
	
		} 
	}
	this.style.backgroundColor = "#898989";
	this.onmouseout = mouseOutSelectPhotoThumb;
	return false;
}

function resizeImageRoll() {
	/*PhotoRollWidth = 0;
	noPhotoThumbsLoaded = 0;
	noPhotoThumbs = 0;
	for (li=0; li<document.getElementsByClassName("photoThumb").length; li++) {
		PhotoRollWidth += document.getElementsByClassName("photoThumb")[li].offsetWidth;
		PhotoRollWidth += 20;
		alert(document.getElementsByClassName("photoThumb")[li].offsetWidth);
		noPhotoThumbsLoaded += 1;
		setWidth();
	}*/
	var photoWidth = 0;
	photoWidth += ((document.getElementsByClassName("photoThumb").length)*180)
	document.getElementById("image_roll").style.width = photoWidth+"px";
	photoWidth += 20;
	document.getElementById("imageRoll_dropShadow").style.width = photoWidth+"px";
}
/*
function setWidth() {
	//setInterval(50);
	if (noPhotoThumbsLoaded == document.getElementsByClassName("photoThumb").length) {
		document.getElementById("image_roll").style.width = PhotoRollWidth+"px";
		alert(PhotoRollWidth+"px");
	}
}*/
// Photo Album Functions End

// Initial Funcitons Start
function initAll() {
	setNavEvents();
	initNewsEvents();
	initBrowserRecomEvents();
	initAjaxRequests();
	initPopupEvents();
	initPhotoEvents();
}
window.onload = initAll;
//Initial Functions End
