/**
 * @author dylan
 */

var isPaused = 0; //true if the gallery slideshow is paused
var imgCount = 0;  // # of images in a gallery
var pageNumber = 1;  // current page of thumbnails in gallery

$(function(){$(document).pngFix();});

function myOnload(){  // only called from default.php
	document.getElementById('menuLink_home').style.backgroundColor="black";
    startFrontpageSlideshow();
	$('#content').bind('mouseover',showPauseInfo);
	$('#content').bind('mouseout',hidePauseInfo);
	
	loadMenu();
}

function myGalleryOnload(){
    startGallerySlideshow();
	var myUrl = parsedURL();
	styleMenu(myUrl['gallery']);
}

function showGalleryThumbnailsScrollbar() {
	document.getElementById('galleryThumbnailsScrollbar').style.display = "block";
	setupScrollButtons();
}

function setupScrollButtons() {
	$('#galleryThumbnailsScrollbarUp').css('display','block');
	$('#galleryThumbnailsScrollbarDown').css('display','block');
	
	$('#galleryThumbnailsScrollbarUp').bind('click',moveUp);
	$('#galleryThumbnailsScrollbarDown').bind('click',moveDown);
	if (pageNumber==1) {
		$('#galleryThumbnailsScrollbarUp').unbind('click');
		$('#galleryThumbnailsScrollbarUp').css('display','none');
	}
	
	if (pageNumber==Math.ceil(imgCount/18)){
		$('#galleryThumbnailsScrollbarDown').unbind('click');
		$('#galleryThumbnailsScrollbarDown').css('display','none');
	}
}

function moveUp(moveTo) {
	$('#galleryThumbnailsScrollbarUp').unbind('click');
	$('#galleryThumbnailsScrollbarDown').unbind('click');
	var newTop = $('#galleryThumbnails').position().top + $('#galleryThumbnailsBg').height();
	pageNumber-=1;
	if (moveTo=="top") {
		newTop = 0;
		pageNumber=1;
	}
	$('#galleryThumbnails').animate({top:newTop+"px"},800,setupScrollButtons);
}

function moveDown() {
	$('#galleryThumbnailsScrollbarUp').unbind();
	$('#galleryThumbnailsScrollbarDown').unbind();
	var newTop = $('#galleryThumbnails').position().top - $('#galleryThumbnailsBg').height();
	//alert($('#galleryThumbnails').position().top);
	pageNumber+=1;
	$('#galleryThumbnails').animate({top:newTop+"px"},800,setupScrollButtons)
}

function myMaterialsOnload() {
	document.getElementById('menuLink_materials').style.backgroundColor="black";
	startMaterialsSlideshow();
}

function myGreenOnload() {
	document.getElementById('menuLink_green').style.backgroundColor="black";
}

function myAboutOnload() {
	document.getElementById('menuLink_about').style.backgroundColor="black";
}

function myContactOnload() {
	document.getElementById('menuLink_contact').style.backgroundColor="black";
}

function startFrontpageSlideshow(){
    $('#content').cycle({
        fx: 'scrollRight',
        easing: 'easeOutSine',
        pause: 1,
        timeout: 5000,
        speed: 1200
    });
}

function startGallerySlideshow(){
	var stack=[];
	var myGets=parsedURL();
	var myImgs = new Array();
	
	$.ajax({
		type: "GET",
		cache: "false",
		url: "enumImages.php?gallery="+myGets['gallery'],
		success: function(data){
			myImgs = data.split(",");
			imgCount = myImgs.length;
			for (i = 0; i < myImgs.length; i++) {
				if (i != 0 && i!=1 && i!=2 && i!=3 && i!=4 && i!=5) {
					var myImage = myImgs[i];
					var myGallery = myGets['gallery'];
					var img = new Image(500, 300);
					img.src = "./images/galleries/" + myGets['gallery'] + "/" + myImgs[i];
					if (img.complete) {
						stack.push(img);
					}
					else {
						$(img).bind('load', function(){
							stack.push(this);
						});
					}
				}
			}
			if (myImgs.length > 17) {
				showGalleryThumbnailsScrollbar();
			}
		}
	});
    $('#gallery').cycle({
        fx: 'fade',
        easing: 'easeOutSine',
        prev: '#prev',
        next: '#next',
        timeout: 5000,
        speed: 800,
		before: onBefore,
        pager: '#galleryThumbnails',
        pagerAnchorBuilder: function(idx, slide){
			var mySplit = slide.firstChild.src.split("/");
			var count = mySplit.length;
			var mySrc ="";
			for (i=4; i>=1; i--) {
				if (i!=4) {mySrc=mySrc+"/"}
				mySrc = mySrc+mySplit[mySplit.length-i];
			}
			return '<a href="#"><img src="./phpThumb.php?src=' + mySrc + '&h=38" /></a>';
        }
    });

	var myIndex=2;
	var myNext;
	var firstLoad = 1;
	function onBefore(curr, next, opts){
		var slideCount = opts.elements.length;
		var nextSlideIndex = 0;
		var prevSlideIndex = 0;
		var i = 0;
		jQuery.each(opts.elements, function(){
			if (this==next) {
				nextSlideIndex = i;
			} else if (this==curr) {
				prevSlideIndex = i;
			}
			i++;
		});
		if (nextSlideIndex != 0 && is_int(nextSlideIndex/18)) {
			if ((Math.ceil(slideCount/18) != Math.ceil(nextSlideIndex/18)) && (nextSlideIndex > prevSlideIndex)) {
				moveDown();
			}
		} else if (slideCount>18 && nextSlideIndex==0) {
			moveUp("top");
		} else if (is_int(prevSlideIndex/18) && prevSlideIndex > nextSlideIndex) {
			moveUp();
		}
		
		if (opts.addSlide) { // <-- important!
			while (stack.length) {
				var img = stack.pop();
				var a = document.createElement('a');
				var myHref = img.src.split("galleries/");
				a.href = "./images/galleries/large/" + myHref[1];
				a.rel = "lightbox";
				a.appendChild(img);
				$(a).attr('class','thickbox');
				opts.addSlide(a);
				if (is_int(myIndex/18)) {
					myNext = a.href;
				}
				myIndex++;
			}
		}
		$('a[rel*=lightbox]').lightBox();
	}
	$('#pause').click(function() {
		$('#gallery').cycle('pause');
		isPaused=1;
		controlMouseover('pause');
		controlMouseout('play');
	});
	$('#play').click(function() {
		$('#gallery').cycle('resume');
		isPaused=0;
		controlMouseover('play');
		controlMouseout('pause');
	});
	document.getElementById('play').firstChild.style.left="-75px";
}

var materialsTimeout;
function startMaterialsSlideshow(){
    $('#materialsGallery').cycle({
        speed: 1,
		timeout: 0,
		fx: 'fade',
        pager: '#materialsThumbnails',
        pagerAnchorBuilder: function(idx, slide){
			var mySplit = slide.firstChild.src.split("/");
			var count = mySplit.length;
			var mySrc ="";
			for (i=3; i>=1; i--) {
				if (i!=3) {mySrc=mySrc+"/"}
				mySrc = mySrc+mySplit[mySplit.length-i];
			}
            return '<div onmouseover="javascript:setMaterialsTimeout('+idx+');" onmouseout="javascript:clearTimeout(materialsTimeout);"><a href="#"><img src="./phpThumb.php?src=' + mySrc + '&h=50" /></a></div>';
        }
    });
}

function setMaterialsTimeout(idx) {
	materialsTimeout=setTimeout(function(){
		$("#materialsGallery").cycle(idx)
	}
	,300);
}

function controlMouseover(elId) {
	if (isPaused || elId=='pause' || elId=='prev' || elId=='next') {
		document.getElementById(elId).firstChild.style.left="-75px";
	}
}

function controlMouseout(elId) {
	if ((isPaused && elId!='pause') || (!(isPaused) && (elId=='pause')) || elId=='prev' || elId=='next'){
		document.getElementById(elId).firstChild.style.left="0";
	}
}

function showPauseInfo(){
    $('#mainSlideshowHover').animate({
		left: "672px"
	},600);
}

function hidePauseInfo(){
	$('#mainSlideshowHover').animate({
		left: "612px"
	},600);
}

function styleMenu(galleryId) {
	document.getElementById('galleryLink_'+galleryId).style.backgroundColor="black";
}

// contact form validation
function contactFormValidation(){
	document.getElementById('contactButton').disabled = true;
	if (document.getElementById('name').value == "") {
		document.getElementById('name').focus();
		alert("We'd like to know your name.");
		document.getElementById('contactButton').disabled = false;
		return true;
	}
	if (document.getElementById('email').value == "" &&
		document.getElementById('phone').value == "") {
		document.getElementById('email').focus();
		alert("Please tell us how to best contact you.");
		document.getElementById('contactButton').disabled = false;
		return true;
	}
	if (document.getElementById('message')) {
		if (document.getElementById('message').value == "") {
			document.getElementById('message').focus();
			alert("Please tell us a little bit about why you are contacting us today.");
			document.getElementById('contactButton').disabled = false;
			return true;
		}
	}
	document.myForm.submit();
}

function parsedURL() {
	var searchString = document.location.search;
	searchString = searchString.substring(1);
	var nvPairs = searchString.split("&");
	var result = new Array();
	for (i = 0; i < nvPairs.length; i++)
		{
		     var nvPair = nvPairs[i].split("=");
		     var name = nvPair[0];
		     var value = nvPair[1];
			 result[nvPair[0]] = nvPair[1]
		}
	return result;
}

function is_int(value){
  if((parseFloat(value) == parseInt(value)) && !isNaN(parseInt(value))) {
      return true;
 } else {
      return false;
 }
}
