/* 
 * LSA is the scope of all custom objects created for Cherokee Construction.
 * This is so any outside JS code won't conflict with the functions and variables.
 */
if (!LSA) {
	var LSA = {
		
		
		galleryXmlPath: "/@res/xml/galleries.xml",
		galleryPath: "/@res/img/gallery/",
		
		gallerySize: "",
		
		/* 
		 * The following variables are tracked to manage the mouseover states
		 * of the primary navigation. Default is what is marked when the page loads
		 * and Over is what is marked as the user is interacting with the page.
		 */
		navMainDefaultLI: "",
		navMainOverLI: "",
		
		/* 
		 * This hides/shows the second level navigation (the drop down menus).
		 * This function is called onMouseOver and onMouseOut of the LI and UL in the main nav.
		 */
		navMainHideShow: function(){
			if (this.navMainOverLI) {
				/* Remove the 'on' state from the LI (hard coded CLASS) because we don't want to show two LIs as being 'on'. */
				jQuery("#" + this.navMainDefaultLI).removeClass("active");
				
				/* Remove the 'on' state from the LI that last had the CLASS applied by this function. */
				jQuery("#navMain li.hover").removeClass("hover");
				jQuery("#" + this.navMainOverLI).addClass("hover");
			}
			else {
				jQuery("#navMain li.hover").removeClass("hover");
				jQuery("#" + this.navMainDefaultLI).addClass("active");
			}
		},
		
		/*
		 * This hides/shows the slidedown layer for the Sign In form.
		 * Fading in/out the other objects below isn't necessary, but it looks nicer
		 * this way because of the background gradients.
		 */
		navSigninHideShow: function(){
			if (jQuery("#navSignin").css("display") == "none") {
				jQuery("#jsImgClose, #navSigninRegister, #navSigninAccount").fadeIn();
				jQuery("#navSignin").slideDown("fast");
			}
			else {
				jQuery("#jsImgClose, #navSigninRegister, #navSigninAccount").fadeOut();
				jQuery("#navSignin").slideUp("slow");
			}
		},
		
		
		/*
		galleryClick: function(obj) {
			$("#contentGalleryPhoto img").attr("src", $(obj).attr("href"));
			$("#contentGalleryNav a.on").removeClass("on");
			$(obj).addClass("on");
		},
		galleryNext: function() {
			var objOld = $("#contentGalleryNav a.on");
			var objNew = $("a", $(objOld).parent().next());
			if($(objNew).parent().attr("id") != "galleryLInext") {
				$("#contentGalleryPhoto img").attr("src", $(objNew).attr("href"));
				$("#contentGalleryNav a.on").removeClass("on");
				$(objNew).addClass("on");
			}
		},
		galleryPrev: function() {
			var objOld = $("#contentGalleryNav a.on");
			var objNew = $("a", $(objOld).parent().prev());
			if($(objNew).parent().attr("id") != "galleryLIprev") {
				$("#contentGalleryPhoto img").attr("src", $(objNew).attr("href"));
				$("#contentGalleryNav a.on").removeClass("on");
				$(objNew).addClass("on");
			}
		},
		activateGalleryNav: function() {
			LSA.gallerySize = $("#contentGalleryNav li").size();
			$("#contentGalleryNav li").each(function(i){
				if (i == 0) {
					$(this).attr("id", "galleryLIprev");
					$("a", $(this)).click(function(){
						LSA.galleryPrev();
						return false;
					});
				}
				else if(i == LSA.gallerySize-1) {
					$(this).attr("id", "galleryLInext");
					$("a", $(this)).click(function(){
						LSA.galleryNext();
						$(this).blur();
						return false;
					});
				}
				else {
					$(this).attr("id", "galleryLI" + i);
					$("a", $(this)).click(function(){
						LSA.galleryClick($(this));
						$(this).blur();
						return false;
					});
					if(i == 1) {
						$("a", $(this)).click();
					}
				}
			});
		},
		setGalleryActive: function(obj, galleryNumber) {
			$("#contentGalleryList span").remove();
			$(obj).parent().prepend("<span>&nbsp;</span>");
			var tmp = "";
			$(LSA.xmlFile).find("gallery[@id=" + galleryNumber + "]").each(function(i){
				$("#contentGalleryDescription h1").html($(this).attr("title"));
				$("#contentGalleryDescription p").html($(this).attr("description"));
				$("img", $(this)).each(function(j){
					tmp = tmp + "<li><a href=\"" + LSA.galleryPath + $(this).attr("src") + "\">&nbsp;</a></li>"
				});
			});
			tmp = tmp + "<li><a>&nbsp;</a></li></ul>";
			tmp = "<ul><li><a>&nbsp;</a></li>" + tmp;
			
			$("#contentGalleryNav").html(tmp);
			LSA.activateGalleryNav();
			return false;
		},*/
		
		initalizeGallery: function() {
			var tmp = "";
			$("#contentGalleryList a[@rel]").each(function(i) {
				tmp = tmp + "<li><a rel='" + $(this).attr("rel") + "'>&nbsp;</a></li>";
			});
			tmp = tmp + "<li><a>&nbsp;</a></li></ul>";
			tmp = "<ul><li><a>&nbsp;</a></li>" + tmp;
			
			$("#contentGalleryNav").html(tmp);
			LSA.initalizeGalleryNav();
		},
		initalizeGalleryNav: function() {
			LSA.gallerySize = $("#contentGalleryNav li").size();
			$("#contentGalleryNav li").each(function(i){
				if (i == 0) {
					$(this).attr("id", "galleryLIprev");
					$("a", $(this)).click(function(){
						LSA.galleryPrev();
						return false;
					});
				}
				else if(i == LSA.gallerySize-1) {
					$(this).attr("id", "galleryLInext");
					$("a", $(this)).click(function(){
						LSA.galleryNext();
						$(this).blur();
						return false;
					});
				}
				else {
					$(this).attr("id", "galleryLI" + i);
					$("a", $(this)).click(function(){
						LSA.galleryClick($(this));
						$(this).blur();
						return false;
					});
				}
				if(i == 1) {
					$("a", $(this)).click();
				}
			});
			$("#contentGalleryList li a[@rel]").click(function(){
				LSA.galleryClick($(this));
				$(this).blur();
				return false;
			});
		},
		galleryXXX : function(rel) {
			$("#contentGalleryNav a.on").removeClass("on");
			$("#contentGalleryList li span").remove();
			$("#contentGalleryNav a[@rel=" + rel +"]").addClass("on");
			$("#contentGalleryList a[@rel=" + rel +"]").parent().prepend("<span></span>");
		},
		galleryClick: function(obj) {
			$("#contentGalleryPhoto img").attr("src", LSA.galleryPath + $(LSA.xmlFile).find("#" + $(obj).attr("rel")).attr("src"));
			$("#contentGalleryDescription h1").html($(LSA.xmlFile).find("#" + $(obj).attr("rel")).attr("title"));
			$("#contentGalleryDescription p").html($(LSA.xmlFile).find("#" + $(obj).attr("rel")).attr("description"));
			LSA.galleryXXX($(obj).attr("rel"));
		},
		galleryNext: function() {
			var objOld = $("#contentGalleryNav a.on");
			var objNew = $("a", $(objOld).parent().next());
			if($(objNew).parent().attr("id") != "galleryLInext") {
				$(objNew).click();
			}
		},
		galleryPrev: function() {
			var objOld = $("#contentGalleryNav a.on");
			var objNew = $("a", $(objOld).parent().prev());
			if($(objNew).parent().attr("id") != "galleryLIprev") {
				$(objNew).click();
			}
		}
	};
}
	
/*
 * jQuery DOMdocument.ready
 */
jQuery(document).ready(function($){
	/* define that the browser has JS enabled (stands for "With JavaScript"), used in the CSS */
	$("body").addClass("wjs");
	
	if($('#contentHomePhotos').size() > 0){
		var randomNumber = Math.floor(Math.random()*3+1);
		$('#contentHomePhotos').attr('src', '@res/img/homePhotos' + randomNumber + '.png');
	}
	
	$('#contentHomePhotosMore').hover(
		function(){
			var tmp = $('img', $(this));
			$(tmp).attr('src', $(tmp).attr('src').replace('Off.png', 'On.png'));
		},
		function(){
			var tmp = $('img', $(this));
			$(tmp).attr('src', $(tmp).attr('src').replace('On.png', 'Off.png'));
		}
	);

	$("#content hr").wrap('<div class="hr"></div>');

	/* set all the IDs for the nav so they can be styled uniquely. this is because we are using image replacement. */
	$("#navMain>ul>li").each(function(i){
		$(this).attr("id", "navMainLI" + i);
		if(i == $("#navMain>ul>li").size()-1) {
			$("#navMain>ul>li>ul>li").each(function(j){
				$(this).attr("id", $(this).parent().parent().attr("id") + j);
				if(j == $("#navMain>ul>li>ul>li").size()-1) {
					$("#navMain>ul>li>ul>li>ul>li").each(function(k){
						$(this).attr("id", $(this).parent().parent().attr("id") + k);
					});
				}
			});
		}
	});
	
	/*
	 * 
	 */
	if($("#contentGalleryList li").size()) {
		$("#content").addClass("gallery");
		
		$.ajax({
			url: LSA.galleryXmlPath,
			dataType: "xml",
			async: false,
			success: function(html){
				LSA.xmlFile = html;
				LSA.initalizeGallery();
			}
		});
		
		/*
		$("#contentGalleryList a[@rel]").each(function(i) {
			$(this).click(function(){
				LSA.setGalleryActive($(this), $(this).attr("rel"));
			});
			if (i == 0) {
				$(this).click();
			}
		});
		*/
	}
	
	$("div.testimonial:last").addClass("last");
	
	
	/* 
	 * set the default LI, this will be used to reset the nav back to the default state
	 * after all mouseover/out events have fired.
	 */
	LSA.navMainDefaultLI = $("#navMain ul li.active").attr("id");
	
	/* sets the onMouseOver/out events for the main nav. This will hide/show the dropdown 2nd level nav */
	$("#navMain>ul>li").hover(function(){
		LSA.navMainOverLI = $(this).attr("id");
		LSA.navMainHideShow();
	}, function(){
		LSA.navMainOverLI = "";
		LSA.navMainHideShow();
	});
	$("#navMain>ul>li>ul").hover(function(){
		LSA.navMainOverLI = $(this).parents("li").attr("id");
		LSA.navMainHideShow();
	}, function(){
		LSA.navMainOverLI = "";
		LSA.navMainHideShow();
	});
	
	$("#siteinfoTrex img").hover(function(){
		$(this).attr("src", $(this).attr("src").replace(".png", "Over.png"));
	}, function(){
		$(this).attr("src", $(this).attr("src").replace("Over.png", ".png"));
	});
	
	$("#contentHomeTrex img").hover(function(){
		$(this).attr("src", $(this).attr("src").replace(".png", "Over.png"));
	}, function(){
		$(this).attr("src", $(this).attr("src").replace("Over.png", ".png"));
	});
	
	if ($("a.lightbox").size()) {
		$("a.lightbox").lightbox();
	}
});


