/* Author: Eric Bjella, BIG Folio Inc.

*/

var curPath = '';
var serverPath = document.URL.split("#")[0]; //
var curThumbSet = 0;

var body_color;
var app_color;
var app_border_color;
var page_color;
var title_color;
var text_color;
var custom_color_1;
var custom_color_2;
var custom_color_3;


$(document).ready(function(){
	
	// assign settings
	body_color         = $("#body_color").css('color');
	app_color          = $("#app_color").css('color');
	app_border_color   = $("#app_border_color").css('color');
	page_color         = $("#page_color").css('color');
	title_color    	   = $("#title_color").css('color');
	text_color         = $("#text_color").css('color');
	custom_color_1     = $("#custom_color_1").css('color');
	custom_color_2     = $("#custom_color_2").css('color');
	custom_color_3     = $("#custom_color_3").css('color');
	
	adjustFontSize("#menu", nav_font_mod);
	
	handleSubMenuWidth();
	handleRedirects();
	$("ul.topnav li.menuItem").mouseover(handleMenuItemOver);
	$("ul.topnav li.menuItem").mouseout(handleMenuItemOut);
	
	var rootTitle = $.address.title();
	$.address.change(function(event) {
		//alert(event.value);
	    handleContent(event.value);
		if (event.value != 'home' && event.value != '' && event.value != '/') {
			$.address.title(rootTitle + ' - ' + event.value.substr(1).split('/').join(' - ')); 
		} else {
			$.address.title(rootTitle) 
		}
		$.address.tracker();
    });
	adjustFontSize("#logo", title_font_mod);
	if (music_files.length > 0) {
		handleAudio($.address.value());
	}
	$(window).resize();
});

$(window).load(function(){
	handleSubMenuWidth();
	handleRedirects();
	// handle texture
	if (texture_file_name != "") {
		$('#textureLoader').backgrounder({element : '#texture'}); //{element : 'body'}
	}
	$(window).resize();
});

function handleContent(v) {
	//
	hideLoadBar();
	clearTimeout(introInterval);
	pauseSlideshow();
	
	//
	if ($("#audioPlayer").hasClass("autoPaused") && (music_files.length > 0)) {
		$("#audioPlayer").removeClass('autoPaused');
		$("#audioIcon").show();
		toggleAudio();
	}
	//
	if (v.substr(0,4) == '/%21') {
		v = v.substr(4);
	}
	$.log('v: '+v);
	var paths = v.split('/');
	//
	if (paths[4] != undefined) {
		$("#thumbIcon").fadeIn('fast');
		$("#slideshowBar").fadeIn('fast');	
	} else {
		$("#thumbIcon").fadeOut('fast');
		$("#slideshowBar").fadeOut('fast');
	}
	//
	if (v == '/' || v == '' || paths[1] == 'home') {
		$("#main").stop().fadeTo('fast', 0, loadHome);
	} else if (paths[1] == makeNiceURL($("#portfolioItem").find("a.toplink").html())) {
		$.log("PORTFOLIO selected");
		if (paths[2] == undefined) {
			$.log("paths2 undefined");
			$("#main").stop().fadeTo('fast', 0, function() {loadPortfolio(paths)});
		} else if (paths[3] == undefined) {
			$.log("paths3 undefined");
			$("#main").stop().fadeTo('fast', 0, function() {loadCategory(paths)});
		} else {
			var lastPaths = curPath.split('/');
			if (lastPaths[2] == paths[2] && lastPaths[3] == paths[3]) {
				$.log("lastpaths = paths"+$("#mainGalleryHolder").length);
				$("#mainGalleryHolder").fadeTo('fast', 0);//, function() {loadGalleryImage(paths)});
				loadGalleryImage(paths);
			} else {
				$.log("lastpaths != paths");
				$("#main").stop().fadeTo('fast', 0, function() {loadGallery(paths)});
			}
		}
	} else {
		if ($("#contactItem").find("a.toplink").html() != undefined && paths[1] == makeNiceURL($("#contactItem").find("a.toplink").html())) { 
			if (v.indexOf(curPath) == -1 || curPath == "/") {
				$("#main").stop().fadeTo('fast', 0, function() {loadContact(paths)}); 
			}
		} else {
			$("#main").stop().fadeTo('fast', 0, function() {loadPage(paths)});
		}
	}
	//
	curPath = v;
	//
	handleMenuSelection(paths);
}

function loadHome() {
	var homeURL = "desperado_home.php";
	if (intro_video != '') {
		homeURL = "desperado_home_video.php";
	}
	$.ajax({
	   type: "GET",
		url: homeURL,
		success: function(content) {
			var html = content;
			$("#main").html(html);
			//
			if ($(".homeImage").length > 0) {
				var homeImgLoaded = false;
				$(".homeImage").load(function(){
					if (homeImgLoaded) {
						return;
					}
					homeImgLoaded = true;
					handleHomeStyles();
					doHomeSlideshow();
				});
				if ($(".homeImage")[0].complete){
					if (homeImgLoaded) {
						return;
					}
					homeImgLoaded = true;
					handleHomeStyles();
					doHomeSlideshow();
				}
			}
			//
			handleHomeStyles();
			if (intro_video != '') {
				//
				var ext = intro_video.substr(intro_video.length-3);
				//
				$.log('INTRO VIDEO: '+serverPath+"videos/"+intro_video);
				//
				handleVideoFunctionality(intro_video);
				$("#main").fadeTo('fast', 1);
			} else {
				$("#main").fadeTo('fast', 1);
				//doHomeSlideshow();
			}
		}
	});	
}

function handleVideoFunctionality(videoFile) {
	$("#videoControls .stop").click(function() {
		stopVideo();
	});
	$("#videoControls .pauseBtn").click(function() {
		toggleVideo();
	});
	$("#videoControls .playBtn").click(function() {
		toggleVideo();
	});
	$("#videoPlayer").jPlayer( {
	    ready: function () {
	      $(this).jPlayer("setMedia", {
	        m4v: serverPath+"videos/"+videoFile
	      }).jPlayer("play");
	    },
		ended: function() { 

	  	},
	    supplied: 'm4v',
	    swfPath: "javascripts/",
		solution: 'html,flash',
		preload: "auto",
		cssSelectorAncestor: '#videoControls',
		cssSelector: {
		  seekBar: '.jp-seek-bar',
		  playBar: '.jp-play-bar'
		 }
	});
	$("#videoControls .pauseBtn").show();
	$("#videoControls .playBtn").hide();
	autoPauseAudio();
}
function stopVideo() {
	$("#videoControls").addClass("paused");
	$("#videoControls .pause").hide();
	$("#videoControls .play").show();
	$("#videoPlayer").jPlayer('stop');
}
function toggleVideo() {
	if ($("#videoControls").hasClass("paused")) {
		$("#videoControls").removeClass("paused");
		$("#videoControls .pauseBtn").show();
		$("#videoControls .playBtn").hide();
		$("#videoPlayer").jPlayer('play');
	} else {
		$("#videoControls").addClass("paused");
		$("#videoControls .pauseBtn").hide();
		$("#videoControls .playBtn").show();
		$("#videoPlayer").jPlayer('pause');
	}
}

function doHomeSlideshow() {
	var lastIntroImage = curIntroImage;
	curIntroImage++;
	if (curIntroImage >= intro_images.length) {
		curIntroImage = 0;
	}
	//
	$(".homeImage").each(function(index, value) {
		if (index == lastIntroImage && lastIntroImage != curIntroImage) {
			var theImg = $(this);
			$(this).animate({
				"opacity":0
			}, 1000, 'linear', function() { theImg.hide(); });
		} else if (index == curIntroImage) { 
			$(this).show();
			$(this).animate({
				"opacity":1
			}, 1000, 'linear');
		}
	});
	if (intro_images.length > 1) {
		introInterval = setTimeout(doHomeSlideshow, slideshow_speed * 1000);
	}
	handleHomeStyles();
}

function handleHomeStyles() {
	var mainPadding = Number($("#main").css("paddingTop").substr(0, $("#main").css("paddingTop").length-2));
	var w = $("#container").width() - mainPadding * 2;
	var h = w * 2 / 3; //$(window).height() - mainPadding * 2; //
	var vw = w;
	var vh = vw * 9 / 16;
	if (browser_scroll_mode != 'none') { 
		/*
		if (h > $(window).height() - mainPadding * 2 - 220) {
			h = $(window).height() - mainPadding * 2 - 220;
			vh = h;
		} 
		*/
	} else {
		h = $(window).height() - mainPadding * 2 - 125;
		vh = h;
	}
	if (browser_scroll_mode == 'none') {
		h -= $("#menu").height() + $("#logo").height();
		vh = h - 20;
		//vw = vh * 16 / 9;
	}
	//
	$(".homeImage").css('maxWidth', w);
	$(".homeImage").css('maxHeight', h);
	//
	var imageHeight = 0;
	$(".homeImage").each(function() {
		if (imageHeight < $(this).height()) {
			imageHeight = $(this).height();
		}
	});
	//
	if (imageHeight < h && imageHeight != 0 && intro_video == undefined && browser_scroll_mode != 'none') { // 
		h = imageHeight;
	}
	$("#mainHomeHolder").height(h);
	//if (browser_scroll_mode != 'none') {
		$(".homeImage").each(function() { 
			if ($(this).height() != 0) {
				var yVal = Math.round((h - $(this).height()) / 2);
				$(this).css("marginTop", yVal);
			}
		});
	//}
	//
	$("#videoPlayer").width(vw);
	$("#videoPlayer").height(vh);
	if (browser_scroll_mode == 'none') {
		$("#videoPlayer").css("marginTop", Math.round((h-vh-20)/2));
	}
	//
	/*
	$("#introVideoPlayer").jPlayer( {
		size:{width:vw,height:vh}
	});
	*/
}

function loadPortfolio(paths) {
	var path = makeNiceURL(paths[1]);
	//alert('data: '+data);
	$.ajax({
	   type: "GET",
		url: "desperado_category.php",
		data: "path="+path,
		success: function(content) {
			var html = content;
			$("#main").html(html);
			handleCategoryFunctionality();
			handleCategoryStyles();
			adjustFontSize("h1", page_font_mod);
			adjustFontSize(".galLabel", page_font_mod);
			$("#main").fadeTo('fast', 1);
		}
	});
}

function loadCategory(paths) {
	var path = makeNiceURL(paths[1]) + '/' + makeNiceURL(paths[2]);
	$.ajax({
	   type: "GET",
		url: "desperado_category.php?path="+path,
		success: function(content) {
			var html = content;
			$("#main").html(html);
			handleCategoryFunctionality();
			handleCategoryStyles();
			adjustFontSize("h1", page_font_mod);
			adjustFontSize(".galLabel", page_font_mod);
			$("#main").fadeTo('fast', 1);
		}
	});
}

function handleCategoryFunctionality() {
	var numThumbs = $(".portfolio img.portfolioThumb").length;
	var totalWidth = numThumbs * Number($(".portfolio img.portfolioThumb").css('maxWidth').substr(0, $(".portfolio img.portfolioThumb").css('maxWidth').length-2));
	//$('.thumbsInner').width(totalWidth);
	$(".portfolio img.portfolioThumb").hide();
	$(".portfolio img.portfolioThumb").load(function(){
		$(this).fadeIn('slow');
	});
	if($(".portfolio img.portfolioThumb")[0].complete){
		$(this).fadeIn('slow');
	}
	$(".portfolio img.portfolioThumb").mouseover(function(){
		$(this).stop().animate({
			"opacity":.6
		}, 500, 'easeOutExpo');
	});
	$(".portfolio img.portfolioThumb").mouseout(function(){
		$(this).stop().animate({
			"opacity":1
		}, 1000, 'easeOutExpo');
	});
}

function handleCategoryStyles() {
	//
	if (browser_scroll_mode == 'none') {
		var mainPadding = Number($("#main").css("paddingTop").substr(0, $("#main").css("paddingTop").length-2));
		var	h = $(window).height() - mainPadding * 2 - $("#menu").height() - $("#logo").height() - 95;
		$(".portfolio").height(h);
		$(".portfolio").jScrollPane();
	}
	//
}

function loadGallery(paths) {
	showLoadBar();
	var path = makeNiceURL(paths[1]) + '/' + makeNiceURL(paths[2]) + '/' + makeNiceURL(paths[3]) + '/' + makeNiceURL(paths[4]);
	$.ajax({
	   type: "GET",
		url: "desperado_gallery.php?path="+path,
		success: function(content) {
			var html = content;
			$("#main").html(html);
			handleGalleryStyles();
			handleGalleryFunctionality();
			setupSlideshow(paths);
			var imageDoneLoading = false;
			$("#currentGalleryImage").hide();
			$("#currentGalleryImage").load(function(){
				if (imageDoneLoading) {
					return;
				}
				imageDoneLoading = true; 
				if (auto_start_slideshow) {
					slideshow_on = true;
					doSlideshow(paths);
				}
				handleGalleryStyles();
				//handleGalleryHeight("#currentGalleryImage");
				//$("#currentGalleryImage").fadeIn('slow');
				$("#currentGalleryImage").show();
				$("#main").fadeTo('fast', 1);
				hideLoadBar();
			});

			if($("#currentGalleryImage")[0].complete){
				if (imageDoneLoading) {
					return;
				} 
				imageDoneLoading = true;
				if (auto_start_slideshow) {
					slideshow_on = true;
					doSlideshow(paths);
				}
				handleGalleryStyles();
				//handleGalleryHeight("#currentGalleryImage");
				//$("#currentGalleryImage").fadeIn('slow');
				$("#currentGalleryImage").show();
				$("#main").fadeTo('fast', 1);
				hideLoadBar();
			}
			//loadGalleryImage(paths);
		}
	});
}

function loadGalleryImage(paths) {
	showLoadBar();
	var path = makeNiceURL(paths[1]) + '/' + makeNiceURL(paths[2]) + '/' + makeNiceURL(paths[3]) + '/' + makeNiceURL(paths[4]);
	$.ajax({
	   type: "GET",
		url: "desperado_gallery_image.php?path="+path,
		success: function(content) {
			var html = content;
			$("#mainGalleryHolder").html(html);
			handleGalleryStyles();
			handleGalleryNavLinks();
			setupSlideshow(paths);
			var imageDoneLoading = false;
			$("#currentGalleryImage").hide();
			$("#currentGalleryImage").load(function(){
				if (imageDoneLoading) {
					return;
				}
				imageDoneLoading = true; 
				if (slideshow_on) {
					doSlideshow(paths);
				}
				handleGalleryStyles();
				//handleGalleryHeight("#currentGalleryImage");
				$("#currentGalleryImage").fadeIn('slow');
				hideLoadBar();
			});

			if($("#currentGalleryImage")[0].complete){
				if (imageDoneLoading) {
					return;
				} 
				imageDoneLoading = true;
				if (slideshow_on) {
					doSlideshow(paths);
				}
				handleGalleryStyles();
				//handleGalleryHeight("#currentGalleryImage");
				$("#currentGalleryImage").fadeIn('slow');
				hideLoadBar();
			}
			$("#mainGalleryHolder").stop().fadeTo('fast', 1);
		}
	});
}
var lastHeight = 0;
function handleGalleryStyles() {
	$('.scroll-pane').jScrollPane(); //{showArrows: true}
	$('.galleryImage').bind('dragstart', function(event) { event.preventDefault(); });
	var mainPadding = Number($("#main").css("paddingTop").substr(0, $("#main").css("paddingTop").length-2));
	var imgPadding = 0;
	if ($(".galleryImage").css("paddingTop") != null) {
		imgPadding = Number($(".galleryImage").css("paddingTop").substr(0, $(".galleryImage").css("paddingTop").length-2));
	}
	var w = $("#container").width() - mainPadding * 2 - imgPadding * 2;
	var h = $(window).height() - mainPadding * 2 - imgPadding * 2;
	if ($(".galleryHeader").offset() != null) {
		//h -= $(".galleryHeader").offset().top;
	}
	if ($("#galleryThumbnails").hasClass("showing")) {
		h -= 80;
	}
	if (browser_scroll_mode == 'none') {
		h -= $("#menu").height() + $("#logo").height() + 105; //$("#menu").height() + $("#logo").height() + $(".galleryHeader").height() + 180;
		h -= $("#galleryNav").height();
		//h -= $("#menu").height() + $("#logo").height() + $(".galleryHeader").height();
		//h -= Number($("#logo").css("marginTop").substr(0, $("#logo").css("marginTop").length-2));
		if ($(".galleryCaption").length > 0) {
			h -= $(".galleryCaption").height();
		}
		//h -= 20;
	} else {
		h -= 40;
	}
	//
	$(".galleryImage").css('maxWidth', w);
	$(".galleryImage").css('maxHeight', h);
	//$(".galleryImageHolder").css('minHeight', minH);//height(h + imgPadding * 2);
	
	/*
	var minH = h;
	if ($(".galleryImage").height() > 0 && $(".galleryImage").height() != undefined) {
		minH = $(".galleryImage").height();
		minH += Number($(".galleryImage").css("paddingTop").substr(0, $(".galleryImage").css("paddingTop").length-2)) * 2;
	} else if (w * 2 / 3 > minH) {
		minH = w * 2 / 3;
	}
	$(".galleryImageHolder").height(minH); //
	*/
	if ($(".galleryImage").height() > 0 && $(".galleryImage").height() != undefined) {
		lastHeight = $(".galleryImage").height();
		lastHeight += Number($(".galleryImage").css("paddingTop").substr(0, $(".galleryImage").css("paddingTop").length-2)) * 2;
	}
	$(".galleryImageHolder").height(lastHeight);

	$("#currentGalleryImage").click(function(e) {
		var x = e.pageX - this.offsetLeft;
		if (x > $(this).width()/2) {
			$(".nextNav").find("a").click();
		} else {
			$(".prevNav").find("a").click();
		}
	});
	$("#currentGalleryImage").mousemove(function(e) {
		var x = e.pageX - this.offsetLeft;
		if (x > $(this).width()/2) {
			$(".nextNav").find("a").addClass("navHover");
			$(".prevNav").find("a").removeClass("navHover");
		} else {
			$(".nextNav").find("a").removeClass("navHover");
			$(".prevNav").find("a").addClass("navHover");
		}
	});
	$("#currentGalleryImage").mouseout(function() {
		$(".nextNav").find("a").removeClass("navHover");
		$(".prevNav").find("a").removeClass("navHover");
	});
	//
	//var thumbWidth = w - $("#thumbScrollLeft").width() * 2 - 12;
	//$("#galleryThumbnails").width(thumbWidth);
	//$("#galleryThumbnails .inner").width(thumbWidth + 100);
	
	//$('.scroll-pane').jScrollPane();
}

function handleGalleryHeight(sel) {
	/*
	var h = $(sel).height();
	h += Number($(sel).css("paddingTop").substr(0, $(sel).css("paddingTop").length-2)) * 2;
	//$(".galleryImageHolder").height(h);
	
	$(".galleryImageHolder").animate({
		'height':h+'px'
	}, 600, 'easeInOutExpo');
	*/
	
}

function handleGalleryFunctionality() {
	/*
	$("#galleryThumbnails .thumbnail").unbind("mouseover");
	$("#galleryThumbnails .thumbnail").unbind("mouseout");
	$("#galleryThumbnails .thumbnail").mouseover(function(){
		//$(this).addClass('.thumbnailHover');
		//$(this).stop().fadeTo('fast', .6);
	});
	$("#galleryThumbnails .thumbnail").mouseout(function(){
		//$(this).removeClass('.thumbnailHover');
		//$(this).stop().fadeTo('fast', 1);
	});
	*/
	
	//
	$("#thumbIcon").unbind("click");
	$("#thumbIcon").click(function() {
		$('.scroll-pane').jScrollPane();
		if ($("#galleryThumbnails").hasClass("showing")) {
			$("#galleryThumbnails").removeClass("showing");
			//$("#galleryThumbnails").hide();
			$("#galleryThumbnails").css("opacity", 0);
			$("#galleryThumbnails").height(0);
			$("#galleryThumbnails").css("marginTop",0);
		} else {
			$("#galleryThumbnails").addClass("showing");
			//$("#galleryThumbnails").show();
			$("#galleryThumbnails").css("opacity", 1);
			$("#galleryThumbnails").height(76);
			$("#galleryThumbnails").css("marginTop",10);
			if (slideshow_on) {
				toggleSlideshow();
			}
		}
		handleGalleryStyles();
		//$("#galleryThumbnails").fadeToggle(0, 'easeInOutExpo', function(){});//handleGalleryStyles
	});
	//
	$("#galleryThumbnails img").each(function(i, v) {
		//$(this).hide();
		$(this).addClass('loading');
		$(this).load(function(){
			if (!$(this).hasClass('loading')) {
				return;
			}
			//$(this).fadeIn('slow');
			$(this).removeClass('loading');
			//
			// center thumb
			/*
			var tHeight = $(".thumbnail").height();
			var tWidth = $(".thumbnail").width();
			$(this).css("marginTop",(tHeight - $(this).height())/2);
			$(this).css("marginLeft",(tWidth - $(this).height())/2);
			*/
			//
			// adjust width of holder
			var curWidth = $("#galleryThumbnails .inner").width();
			var imgWidth = $(this).width();
			if (imgWidth == 0) {
				imgWidth = 100;
			}
			curWidth += imgWidth + 4;
			$.log('complete1-: '+imgWidth+', '+curWidth);
			$("#galleryThumbnails .inner").width(curWidth);
			$('.scroll-pane').jScrollPane();
		});

		if($(this)[0].complete){
			if (!$(this).hasClass('loading')) {
				return;
			}
			$(this).fadeIn('slow');
			$(this).removeClass('loading');
			//
			// center thumb
			/*
			var tHeight = $(".thumbnail").height();
			var tWidth = $(".thumbnail").width();
			$(this).css("marginTop",(tHeight - $(this).height())/2);
			$(this).css("marginLeft",(tWidth - $(this).height())/2);
			*/
			//
			// adjust width of holder
			var curWidth = $("#galleryThumbnails .inner").width();
			var imgWidth = $(this).width();
			if (imgWidth == 0) {
				imgWidth = 100;
			}
			curWidth += imgWidth + 4;
			$.log('complete2-: '+imgWidth+', '+curWidth);
			$("#galleryThumbnails .inner").width(curWidth);
			$('.scroll-pane').jScrollPane();
		}
	});
	//
	//$("#galleryThumbnails").slideUp('fast');
	handleGalleryNavLinks();
}

function handleGalleryNavLinks() {
	$("a.galNavLink").click(function() {
		window.location = $(this).attr("href");
		if (slideshow_on) {
			toggleSlideshow();
		}
		return false;
	});
}

function setupSlideshow(paths) {
	$("#slideshowBar").unbind("click");
	//$("#slideshowBar").unbind("mouseover");
	//$("#slideshowBar").unbind("mouseout");
	$("#slideshowBar").click(function() {
		toggleSlideshow(paths);
	});
}

function doSlideshow(paths) {
	var t = 1000 * slideshow_speed
	$("#slideshowBar .dynamic").width(1);
	$("#slideshowBar .dynamic").animate({
		"width":$("#slideshowBar .static").width()+"px"
	}, t, 'linear', function(){
		autoAdvanceImage(paths);
	});
	//galleryInterval = setTimeout(function(){autoAdvanceImage(paths)}, t);
	
}
function pauseSlideshow() {
	clearTimeout(galleryInterval);
	$("#slideshowBar .dynamic").stop();
}
function toggleSlideshow(paths) {
	if (slideshow_on) {
		slideshow_on = false;
		pauseSlideshow();
	} else {
		slideshow_on = true;
		doSlideshow(paths);
	}
}
function autoAdvanceImage(paths) {
	var str = "#!";
	for (var i=1; i<paths.length; i++) {
		if (i < paths.length-1) {
			str += "/" + paths[i];
		} else {
			if (Number(paths[i]) >= numGalleryImages) {
				paths[i] = 0;
			}
			str += "/" + (Number(paths[i]) + 1);
		}
	}
	//alert(str);
	window.location = str;
}

function loadPage(paths) {
	showLoadBar();
	$.log('paths: '+paths);
	var path = '/' + makeNiceURL(paths[1]);
	if (paths[2] != undefined) {
		path += '/' + makeNiceURL(paths[2]);
	}
	$.log('path: '+path);
	$.ajax({
	   type: "GET",
		url: "desperado_page.php?path="+path,
		success: function(content) {
			var html = content;
			$("#main").html(html);
			handlePageImageLoad();
			handlePageStyles();
			adjustFontSize(".page h1", page_font_mod);
			adjustFontSize(".pageText", page_font_mod);
			//$("#main").show();
			//$("#main").fadeTo('fast', 1);
		}
	});
}

function handlePageImageLoad() {
	if($(".pageImage img").length > 0) {
		$(".pageImage img").load(function(){
			$(this).fadeIn('slow');
			handlePageStyles();
			handleContactStyles();
			$("#main").fadeTo('fast', 1);
			hideLoadBar();
		});
		if($(".pageImage img")[0].complete){
			$(this).fadeIn('slow');
			handlePageStyles();
			handleContactStyles();
			$("#main").fadeTo('fast', 1);
			hideLoadBar();
		}
	} else {
		$("#main").fadeTo('fast', 1);
		hideLoadBar();
	}
}

function handlePageStyles() {
	if (browser_scroll_mode == 'none') {
		var mainPadding = Number($("#main").css("paddingTop").substr(0, $("#main").css("paddingTop").length-2));
		var w = $("#container").width() - mainPadding * 2;
		var	h = $(window).height() - mainPadding * 2 - $("#menu").height() - $("#logo").height() - 95;
		//$(window).height() - mainPadding * 2 - $("#menu").height() - $("#logo").height() - $(".page h1").height() - 160;
		//
		if ($(".pageImage").length > 0) {
			$(".pageImage img").css('maxWidth', w/2);
			var pageImagePadding = Number($(".pageImage").css("paddingTop").substr(0, $(".pageImage").css("paddingTop").length-2));
			$(".pageImage img").css('maxHeight', h - pageImagePadding * 2);
		} else {
			//$(".pageText").width(w - 20);
		}
		//
		$(".pageText").height(h);
		//
		$('.pageText').jScrollPane();
		//$('.pageText').css("float", "left");
		if ($(".pageImage").length > 0 && $(".pageImage").width() > 0) {
			var pageWidth = w - $(".pageImage").width();
			var minus = 20; /*75;*/
			//$(".pageTextInner").width(pageWidth - 20);
			$(".pageText").width(pageWidth - minus);
			$(".pageText").find(".jspContainer").width(pageWidth - minus);
			$(".pageText").find(".jspPane").width(pageWidth - minus - 20);
			$(".pageText").find(".jspContainer").css("overflowX", "visible");
			$.log("jspVert: "+$(".pageText").find(".jspVerticalBar").length);
			$(".pageText").find(".jspVerticalBar").css("marginLeft", -minus+"px");
		}
	}
}

function loadContact(paths) {
	showLoadBar();
	$.ajax({
	   type: "GET",
		url: "desperado_contact.php",
		success: function(content) {
			var html = content;
			$("#main").html(html);
			adjustFontSize(".page h1", page_font_mod);
			adjustFontSize(".pageText", page_font_mod);
			handleContactStyles();   
			handlePageStyles(); 
			handlePageImageLoad();
			handleContactFunctionality();
			//$("#main").show();
			//$("#main").fadeTo('fast', 1);
		}
	});
	
}

function handleContactStyles() {
	var mainPadding = Number($("#main").css("paddingTop").substr(0, $("#main").css("paddingTop").length-2));
	var w = $("#container").width()/2 - mainPadding * 4;
	//if ($(".contactForm").width() > w) {
		$(".contactForm").width(w-25);
	//}
	$(".contactImg").css("maxWidth", w);
	//$(".contactText").width(w/2);
	//
}

function handleContactFunctionality() {
	$('#bfSubmit').click(function () { 
		var data = '';
		$('.bfFormInput').each(function() {
			if (data != '') {
				data += "&";
			}
			data += $(this).attr('name') + '=' + $(this).val();
		});
		//alert('data: '+data);
	    
		$.ajax({
			//this is the php file that processes the data and send mail
			url: getPath() + "inc/fcontact_form.php",	

			//POST method is used
			type: "POST",

			//pass the data			
			data: data,		

			//Do not cache the page
			cache: false,
               
			//success
			success: function (html) {	
				$.log('contact form sent?: '+html);			
				if (html=='&success=sent&') {					
					//hide the form  
					$('.contactForm').css('display', 'none');
					$('.contactForm').html('Your information has been sent.  Thank you.');  
					$('.contactForm').fadeIn('slow');
					//$.address.tracker('contact-form-submitted');
					$.address.value(curPath + '/submitted');

				} else {
					alert('Sorry, unexpected error. Please try again later.');
				}				
			}		
		});
	});
}

function handleAudio(v) {
	//
	$("#audioIcon").show(); //fadeIn('fast');
	$("#audioIcon").click(function() {
		toggleAudio();
	});
	
	$("#audioIcon").mouseover(function() {
		$("#audioControls").fadeIn('fast');
		$("#container").mouseover(function(event) {
			$("#audioControls").fadeOut('fast');
		});
	});
	
	$("#audioControls .stop").click(function() {
		$("#audioPlayer").removeClass('autoPaused');
		stopAudio();
	});
	$("#audioControls .pause").click(function() {
		$("#audioPlayer").removeClass('autoPaused');
		toggleAudio();
	});
	$("#audioControls .play").click(function() {
		$("#audioPlayer").removeClass('autoPaused');
		toggleAudio();
	});
	//
	if (music_files.length > 1) {
		$("#audioControls .next").click(function() {
			handleNextTrack();
		});
		$("#audioControls .previous").click(function() {
			handlePreviousTrack();
		});
	} else {
		$("#audioControls .next").hide();
		$("#audioControls .previous").hide();
	}
	//
	setupAudioPlayer(music_files[currentTrack], v);
}

function setupAudioPlayer(fileName, v) {
	//
	//
	if (v.substr(0,4) == '/%21') {
		v = v.substr(4);
	}
	var paths = v.split('/');
	//
	var firstAction = "play";
	if (intro_video != '' && (v == '/' || v == '' || paths[1] == 'home')) {
		firstAction = "pause";
		$("#audioPlayer").addClass('autoPaused');
		$("#audioIcon").addClass("paused");
		$("#audioIcon").hide();
		$("#audioControls .pause").hide();
		$("#audioControls .play").show();
	} else {
		$("#audioControls .pause").show();
		$("#audioControls .play").hide();
	}
	$("#audioPlayer").addClass('isActive');
	$("#audioPlayer").jPlayer( {
	    ready: function () {
	      $(this).jPlayer("setMedia", {
	        mp3: "music/"+fileName
	      }).jPlayer(firstAction);
	    },
		ended: function() { 
		  handleNextTrack();
	  	},
	    supplied: "mp3",
	    swfPath: "javascripts/",
		solution: 'html, flash',
		preload: 'metadata',
		cssSelectorAncestor: '#audioControls',
		cssSelector: {
		  seekBar: '.jp-seek-bar',
		  playBar: '.jp-play-bar'
		 }
	});
	animateAudioIcon();
	$.log('setupAudioPlayer: '+fileName);
}

function loadNewAudio(fileName) {
	$.log('loadNewAudio: '+fileName);
	$("#audioPlayer").jPlayer("setMedia", {
	    mp3: "music/"+fileName
	}).jPlayer("play");
	//
	if ($("#audioIcon").hasClass("paused")) {
		$("#audioIcon").removeClass("paused");
		$("#audioControls .pause").show();
		$("#audioControls .play").hide();
		$("#audioPlayer").jPlayer('play');
		animateAudioIcon();
	}
}

function handleNextTrack() {
	currentTrack++;
	if (currentTrack >= music_files.length) {
		currentTrack = 0;
	}
	loadNewAudio(music_files[currentTrack]);
}

function handlePreviousTrack() {
	currentTrack--;
	if (currentTrack < 0) {
		currentTrack = music_files.length - 1;
	}
	loadNewAudio(music_files[currentTrack]);
}

function stopAudio() {
	$("#audioIcon").addClass("paused");
	$("#audioControls .pause").hide();
	$("#audioControls .play").show();
	$("#audioPlayer").jPlayer('stop');
	pauseAudioIcon();
	audioOn = false;
}

function toggleAudio() {
	if ($("#audioIcon").hasClass("paused")) {
		$("#audioIcon").removeClass("paused");
		$("#audioControls .pause").show();
		$("#audioControls .play").hide();
		$("#audioPlayer").jPlayer('play');
		audioOn = true;
		animateAudioIcon();
	} else {
		$("#audioIcon").addClass("paused");
		$("#audioControls .pause").hide();
		$("#audioControls .play").show();
		$("#audioPlayer").jPlayer('pause');
		audioOn = false;
		pauseAudioIcon();
	}
}
var audioOn = true;
function animateAudioIcon() {
	var t = 300;
	$('.audioBar').each(function(index, value) {
		var heightVal = Math.round(1 + Math.random() * 8);
		var marginVal = Math.floor(12 - heightVal);
		if (marginVal > 11) {
			marginVal = 11;
		}
		//$.log('heightVal: '+heightVal);
		$(this).animate({
			"height": heightVal,
			"marginTop": marginVal
		}, t, 'linear'); //t, 'easeOutSine'
	});
	//audioInterval = setTimeout(animateAudioIcon, t);
	if (audioOn) {
		setTimeout(animateAudioIcon, t);
	}
}

function pauseAudioIcon() {
	//$.log('pause');
	$('.audioBar').stop();
	//clearTimeout(audioInterval);
}

function autoPauseAudio() {
	if ($("#audioPlayer").hasClass('isActive') && !$("#audioIcon").hasClass("paused")) {
		$("#audioPlayer").addClass('autoPaused');
		$("#audioIcon").hide();
		toggleAudio();
	}
}

function showLoadBar() {
	if (!$("#loadingIcon").hasClass('showing')) {
		$("#loadingIcon").addClass('showing');
		$("#loadingIcon").fadeIn('fast');
		animateLoadBar();
	}
}

function hideLoadBar() {
	if ($("#loadingIcon").hasClass('showing')) {
		$("#loadingIcon").removeClass('showing');
		$("#loadingIcon").fadeOut('fast');
		$("#loadingIcon .loadBarDynamic").stop();
	}
}

function animateLoadBar() {
	var t = 2000;
	$("#loadingIcon .loadBarDynamic").css("marginLeft", 0);
	$("#loadingIcon .loadBarDynamic").width(0);
	$("#loadingIcon .loadBarDynamic").animate({
		width:$("#loadingIcon .loadBar").width()
	}, t, 'easeInOutExpo',
	function(){
		$("#loadingIcon .loadBarDynamic").animate({
			marginLeft:$("#loadingIcon .loadBar").width()
		}, t, 'easeInOutExpo',
		function(){
			animateLoadBar();
		});
	});
}

$(window).focus(function(){
	if ($("#audioPlayer").hasClass("autoPaused") && !videoPlayerPresent() && (music_files.length > 0)) {
		$("#audioPlayer").removeClass('autoPaused');
		$("#audioIcon").show();
		toggleAudio();
	}
});
$(window).blur(function() {
	autoPauseAudio();
	if (slideshow_on) {
		slideshow_on = false;
		pauseSlideshow();
	}	
});

$(window).resize(handleResize);

function videoPlayerPresent() {
	var vidPlayers = 0;
	vidPlayers += $("#videoPlayer").length;
	return vidPlayers > 0;
}

var resizeDoubleCall = true;
function handleResize() {
	var bodyPadding = Number($('body').css('paddingLeft').substr(0, $('body').css('paddingLeft').length-2));
	if ($(window).width() < content_width + bodyPadding * 2) {
		var w = $(window).width() - bodyPadding * 2;
		$("#container").width(w);
	} else {
		$("#container").width(content_width);
	}
	if (browser_scroll_mode == 'none') {
		$("#container").height($(window).height() - bodyPadding * 2 - $("#footer").height() - 10);
		$("html").css("overflow", "hidden");
		$("#logo img").css("maxHeight", $(window).height()/10);
	}
	$("#footer").width($("#container").width());
	handleHomeStyles();
	handleGalleryStyles();
	handleContactStyles(); 
	handlePageStyles();
	handleCategoryStyles();
	//
	if (resizeDoubleCall) {
		resizeDoubleCall = false;
		setTimeout(handleResize, 100);
	} else {
		resizeDoubleCall = true;
	}
	//
}

//$(function() {
    $(this).bind("contextmenu", function(e) {
        e.preventDefault();
    });
//});




















