// Adds console for browsers without Firebug
if (!("console" in window) || !("firebug" in console)) {
	var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
	window.console = {};
	for (var i = 0; i < names.length; ++i)
		window.console[names[i]] = function(){
	};
} 

/* ADD EVENT HANDLERS */
 
function home_initHandlers(){
	
	$("#homeNav > ul.navLinks > li > a").mouseover(function(event){
	
		event.preventDefault();
		if ($(this).parent().hasClass('hover') 
			|| $(this).parent().hasClass('active') ) {
			return false;
		}
		else 
			$(this).parent().addClass('hover');
		
	}).mouseout(function(event){
	
		event.preventDefault();
		if ($(this).parent().hasClass('active')) {
		   return false;
		}
		else 
		   $(this).parent().removeClass('hover');
		
	}).click(function(event){
	
		event.preventDefault();
		
		if ($(this).attr("href").charAt(0) == "#") {
		    
		    if ($(this).parent().hasClass('hover')) {
		        $(this).parent().removeClass('hover');
		    }
		
		    $(this).parent().siblings().removeClass('active');
		    //$(".subNavLinks").removeClass('active');
			
		    //var subNavLinks = $(this).attr('href');
		    $(this).parent().toggleClass('active')
		    $($(this).attr('href')).toggleClass('active');
		    return false;
		}
		else {
		    document.location.href = this.href;
		    return true;
		}
		
	});
}
 
function subpage_initHandlers(){
	
	$("#subpageNav > ul.navLinks > li").mouseover(function(event){
	
		event.preventDefault();
		if ($(this).siblings().hasClass('hover')) {
			return false;
		}
		else 
			$(this).addClass('hover');
		
	}).mouseout(function(event){
	
		event.preventDefault();
		if ($(this).siblings().hasClass('hover')) {
			$(this).siblings().removeClass('hover');
		}
			$(this).removeClass('hover');
		
	});
	
	if($(".eventGallery").length > 0){
		eventGallery.init();
	}
}
 
var eventGallery = {
	itemCount : 0,
	pageCount : 0,
	itemLimit: 14, //itemLimit : 21,
	currentPage : 1,
	pageHeight: 439, //pageHeight : 606,
	
	paginate : function(linkEl){
		console.info("GOING TO PAGE: " + linkEl.href.split("_")[1]);
				
		//Set currentPage
		eventGallery.currentPage = linkEl.href.split("_")[1];
		
		//Animate page
		var setY = eventGallery.pageHeight * (eventGallery.currentPage - 1) * (-1);
		$(".tableThumbnails").animate({top: setY+"px"},2000);
		
		//$(".tableThumbnails").fadeOut().animate({top: setY}, 250, "linear", function(){$(".tableThumbnails").fadeIn();} );

		
		$("#galleryPagination > a").removeClass("active");
		$(linkEl).addClass("active");
		$("#galleryPageNum").text("Page " + eventGallery.currentPage + " of " + eventGallery.pageCount);
	},
	
	setPageCounter : function(){
		eventGallery.pageCount = Math.ceil(eventGallery.itemCount / eventGallery.itemLimit);
		console.info("eventGallery.itemCount: " + eventGallery.itemCount + "\neventGallery.pageCount: " + eventGallery.pageCount);
		if(eventGallery.pageCount > 0){
			for(var i=1; i<=eventGallery.pageCount; i++){
				if(i === 1) {
					$("<a title=\"Page 1\" href=\"#page_1\" class=\"active\">1</a>").appendTo("#galleryPagination");
				} else {
					$("<a title=\"Page " + i + "\" href=\"#page_" + i + "\">" + i + "</a>").appendTo("#galleryPagination");
				}
			}
			$("<span id=\"galleryPageNum\">Page 1 of " + eventGallery.pageCount + "</span>").appendTo("#galleryPagination");
		
			$("#galleryPagination > a").click(function(event){ 
				event.preventDefault(); 
				eventGallery.paginate(this); 
			});
		
		}
	},
	
	hideSpinner : function(useForce){
		console.log("BEGIN hideSpinner()");
		
		var totalImages = $("a.zoomPhoto > img").length;
		
		if(typeof(useForce) != "undefined"){
			console.log("IF useForce != 'undefined'");
			$(".galleryLoading").css("display","none");
			$(".tableThumbnails").css("visibility", "visible");
			return true;
		}
		
		//if (totalImages == eventGallery.itemCount || byForce === true ) {
		if(totalImages > eventGallery.itemLimit){
			console.log("hideSpinner :: total == itemCount")
			$(".galleryLoading").hide("fast");
			$(".tableThumbnails").css("visibility", "visible");
		}
		else {
			console.log("ELSE setTimeout");
			setTimeout("eventGallery.hideSpinner(true)", 1000);
		}
	},
	
	init : function(){
		
		$(document).ready(function() {
		
			//CALCULATE NUMBER OF PAGES OF IMAGES
			if(eventGallery.itemCount > 0){
				eventGallery.setPageCounter();
				//setTimeout("eventGallery.hideSpinner()",2000);
			} else {
				console.log("INFO :: eventGallery.setPageCount timer invoked for 5-seconds");
				setTimeout("eventGallery.setPageCounter()",4000);
			}
			
			$("a.zoomPhoto").fancybox({
		        'zoomSpeedIn': 300,
		        'zoomSpeedOut': 300,
		        'overlayShow': true,
				'hideOnContentClick': true
			});
			
			/*$("#galleryCart_viewLink").fancybox({
				'overlayShow': false,
				'hideOnContentClick' : false,
				'showCloseButton' : true,
				'centerOnScroll' : true
			});*/
			
			$("#galleryCart_viewLink").click(function(event){
				event.preventDefault();
				$("#galleryCart").slideToggle("slow");
			});
			
			$("select.item_type").change(function(event){
			
				var selectEl = event.target;
				var selectVal = $(selectEl).val();
				
				$(selectEl).siblings(".item_price").text("$"+galleryCart.itemOptions[selectVal]);
				$(selectEl).siblings(".item_description").text($(selectEl).children("option:selected").text());
				
				console.log(selectVal + " :: " + $(selectEl).children("option:selected").text());
				
			});
			
			var aTimer = setTimeout("eventGallery.hideSpinner(true)",1500);
			
			//Add listeners to all "add to cart" buttons to launch options panel
			//$("a.btn1_addToCart").fancybox({
			//	'overlayShow':true
			//});
		});	
	}
};

var galleryCart = {
	
	itemOptions : { "5x7" : 24.99, "8.5x11" : 34.99, "digital" : 24.99, "shipping" : 0.00, "intlship" : 4.99 },
	
	selectOptions : function(){ return true; },
	
	cloneItemImage : function(frameId){
		var frame = frameId.split("-")[1];
		$("#selectedFrame").html($("#thumb-"+frame).clone());
		return;
	}
	
	
}

