// JavaScript Document
/*(function($) {
	$.dequeue = function(a,b){
		return $(a).dequeue(b);
	};
})(jQuery);*/

var picChanging = false;
$(function() {
	$('.close_update_alert').live('click',function() {
		var exdate=new Date();
		exdate.setDate(exdate.getDate()+90);
		document.cookie = 'no_update_alert=true;expires='+exdate.toGMTString()+';path=/';
		$('.update_alert').fadeOut('fast');
	});
	if($.browser.msie)
       	$(document).pngFix();
    	/*if($.browser.webkit && $.client.os.toLowerCase() == 'mac') {
       	$('#navigation').css('padding-top','19px');
    	}*/
	$('a.notAvailable').die('click').live("click",function() {
		alert('Coming soon!'); 
		return false; 
	});
	$('.albumThumbnail').die('click').live("click",function() {
		$('#slideShowPaused').val("1");
		$('.activeThumb').removeClass("activeThumb");
		$(this).addClass("activeThumb");
		var image = $(this).children("img").attr("src");
		var imagePathArray = image.split("/").reverse();
		var albumName = imagePathArray[1];
		var photoNumArray = $(this).attr("href").split("_").reverse();
		var photoNum = photoNumArray[0];
		var oldImageCSSurl = $('#photo_slideshow').css("background-image");
		var oldImage = oldImageCSSurl.substr(4,(oldImageCSSurl.length-5))
		$('#newAlbumImage').html('<img src="'+oldImage+'" />');
		$('#newAlbumImage').show();
		$('#photo_slideshow').css("background-image","url("+image+")");
		$.get("getCaption.php?album="+albumName+"&photoNum="+photoNum, function(data) {
			if(data.substr(0,1) == '<' || data.substr(0,1) == '&lt;') {
				$('#photoCaption').html($(data));
			} else {
				$('#photoCaption').html(data);
			}											  
		});
		$('#newAlbumImage').fadeOut(500);
		$('#navigation').ScrollTo(300);
	});
	
	$('.left').die('mouseover').live("mouseover", function() {
		var activePictureArray_r = $('.activeThumb').attr("href").split("_").reverse();
		if(parseInt(activePictureArray_r[0]) > 1) {
			$(this).css("cursor","pointer");
			$(this).stop();
			$(this).unbind('click').bind("click", function() {
				//console.log("picChanging is: "+picChanging);
				if(picChanging == false) { 
					//console.log("picChanging Should be FALSE, it is: "+picChanging);
					$('#slideShowPaused').val("1"); 
					traversePics("left",false);
				}
			});
			$(this).fadeTo(200,1);	
		}
	}).die('mouseout').live("mouseout",function() {
		$(this).css("cursor","default");
		$(this).stop();
		$(this).fadeTo(200,0.8, function() { $(this).unbind("click"); });						 
	});
	
	$('.right').die('mouseover').live("mouseover", function() {
		// If we haven't reached the end of the images...
		var activePictureArray_r = $('.activeThumb').attr("href").split("_").reverse();
		if(parseInt(activePictureArray_r[0]) < parseInt($('#numberImages').val())) {
			// Allow the right arrow to function.
			$(this).css("cursor","pointer");
			$(this).stop();
			// When it's clicked...
			$(this).unbind('click').bind("click", function() {
				//console.log("picChanging is: "+picChanging);
				if(picChanging == false) { 
					//console.log("picChanging Should be FALSE, it is: "+picChanging);
					$('#slideShowPaused').val("1"); 
					traversePics("right",false);
				}
			});
			// Fade in the arrow
			$(this).fadeTo(200,1);
		}
	}).die('mouseout').live("mouseout",function() {
		// Fade out the arrow...
		$(this).css("cursor","default");
		$(this).stop();
		$(this).fadeTo(200,0.8, function() { $(this).unbind("click"); });						 
	});

	$('.slideshowNav').fadeTo(1,0.8);
	if($('#slideShowPaused').val()) {
		setInterval(function() {
			if($('#slideShowPaused').val().toString() == "0") {
				traversePics("right",true);
			}
		},4500);
	}
	$.get("images/albumShiftLeft.jpg");
	$.get("images/albumShiftRight.jpg");
	$.get("images/photos_over.png");
	$.get("images/slideshow_over.png");
});

function empty( mixed_var ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philippe Baumann
    // +      input by: Onno Marsman
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: LH
    // +   improved by: Onno Marsman
    // +   improved by: Francesco
    // *     example 1: empty(null);
    // *     returns 1: true
    // *     example 2: empty(undefined);
    // *     returns 2: true
    // *     example 3: empty([]);
    // *     returns 3: true
    // *     example 4: empty({});
    // *     returns 4: true
    
    var key;
    
    if (mixed_var === ""
        || mixed_var === 0
        || mixed_var === "0"
        || mixed_var === null
        || mixed_var === false
        || mixed_var === undefined
    ){
        return true;
    }
    if (typeof mixed_var == 'object') {
        for (key in mixed_var) {
            if (typeof mixed_var[key] !== 'function' ) {
              return false;
            }
        }
        return true;
    }
    return false;
}

//*************************************************************************
// You can use this to display a notice after an ajax request is completed
// @param String zone - the id the element the notice should be placed near or inside of.
// @param String placement - where to place the element in relation to 'zone'
// @param String reason - whether to show an error or success notice ('success' or 'error')
// @param String msg - the message to display in the notice.
// @param String no_auto_remove - if false, this will prevent the message from auto-removing.
//*************************************************************************
function ajax_notice(zone,placement,reason,msg,no_auto_remove) {
	if(empty(msg)) { 
		if(reason == 'success') {
			msg = 'Success!' 
		} else {
			msg = 'There was an error, please let Kyle know about the incident.';
		}
	}
	switch(placement) {
		case 'after':
			$('#'+zone).after('<div class="'+reason+'">'+msg+'</div>');
			break;
		case 'before':
			$('#'+zone).before('<div class="'+reason+'">'+msg+'</div>');
			break;
		case 'prepend':
			$('#'+zone).prepend('<div class="'+reason+'">'+msg+'</div>');
			break;
		case 'append':
			$('#'+zone).append('<div class="'+reason+'">'+msg+'</div>');
			break;
	}
	switch(reason) {
		case 'success':
			//success =  $('#'+zone).parent().find('.success');
			//success.effect("highlight",{easing:'easeInExpo'},3500, function() {
				if(!no_auto_remove) {
					setTimeout(function() { 
						$('.success').fadeOut('slow',function() {
							$(this).remove();
						});
					}, 3000);
				}
			//});
			break;
		case 'error':
			//error = $('#'+zone).parent().find('.error');
			//error.effect("highlight",{color:'red',easing:'easeInExpo'},3500, function() {
				if(!no_auto_remove) {
					setTimeout(function(error) { 
						$('.error').fadeOut('slow',function() {
							$(this).remove();
						});
					}, 3000);
				}
			//});
			break;
	}
}

//*************************************************************************
// This will auto remove the text from a text field when focus is given to it. 
// When it loses focus again, if the field is empty, the original value will
// be placed back and the font will be changed to what ever class name is 
// specified by the "addClass" variable (optional)
// @param String fieldName 	- the id of the field being interacted with
// @param String addClasses - comma-seperated list of classes to add/remove.
//*************************************************************************
function form_focus(fieldName,addClasses) {
	$('#'+fieldName).attr("rel",$('#'+fieldName).val());
	var fieldLabel = $('#'+fieldName).attr("rel");
	// For instance, clear the default 'username' in the signin field adn change the text to be black
	if($('#'+fieldName).val() != fieldLabel) {  $('#'+fieldName).css("color","#000"); } 
	else { $('#'+fieldName).css("color","#999"); }
	$('#'+fieldName)
	.bind('focus', function() {
		if($(this).val() == fieldLabel) { 
			$(this).val('');
			$(this).css("color","#000");
			var thisObject = $(this);
			if(addClasses != null) {  
				var allClasses = addClasses.split(',');
				$.each(allClasses,function() {
					$('.'+this).removeClass(this);
					$('.'+this).unbind('focus');
				});
				$.each(allClasses,function() { 
					thisObject.addClass(this);
					thisObject.bind('focus',function() { });
				});
			}
		}
	})
	.bind('blur', function() {
		if($(this).val() == '') { 
			$(this).val(fieldLabel); 
			$(this).css("color","#999");
		}
	});
}

function traversePics(direction,slideshow) {
	//$('#newAlbumImage').stop().hide();
	picChanging = true;
	//console.log("picChanging Should be TRUE, it is: "+picChanging);
	var activePictureArray_r = $('.activeThumb').attr("href").split("_").reverse();
	var activePictureArray = $('.activeThumb').attr("href").split("_");	
	var newActiveID = "";
	
	if(direction == "left") {
		if(parseInt(activePictureArray_r[0]) > 1) {
			photoNum = parseInt(activePictureArray_r[0]) - 1;
			newActiveID = "#"+activePictureArray[1]+photoNum;
		}
	} 
	else if(direction == "right") {
		if(parseInt(activePictureArray_r[0]) < parseInt($('#numberImages').val())) {
			photoNum = parseInt(activePictureArray_r[0]) + 1;
			newActiveID = "#"+activePictureArray[1]+photoNum;
		} else {
			if(slideshow) {
				newActiveID = "#"+activePictureArray[1]+1;		
			}
		}
	}
	if(newActiveID != "") {
		$('.activeThumb').removeClass("activeThumb");
		$(newActiveID).addClass("activeThumb");
		// Get the new image src...
		var image = $('.activeThumb').children("img").attr("src");
		var imagePathArray = image.split("/").reverse();
		// Get's the album name based on the second-to-last element in the src path
		var albumName = imagePathArray[1];
		// Get's the old image from the CSS background-image property
		var oldImageCSSurl = $('#photo_slideshow').css("background-image");
		// Get's the real imge src from the background-image property (it's path is different in 
		// CSS since the directory is different, therefore we need to remove the'../' part)
		var oldImage = oldImageCSSurl.substr(4,(oldImageCSSurl.length-5))
		// Sets the #newAlbumImage element's HTML to be the OLD image... (basically sets the background 
		// of the element the image is in to be the same as the image itself).
		$('#newAlbumImage').html('<img src="'+oldImage+'" />');
		// Makes the old image come up above everything else
		$('#newAlbumImage').show();
		// Changes the element behind it to have the NEW image in it...
		$('#photo_slideshow').css("background-image","url("+image+")");
		$.get("getCaption.php?album="+albumName+"&photoNum="+photoNum, function(data) {
			if(data.substr(0,1) == '<' || data.substr(0,1) == '&lt;') {
				$('#photoCaption').html($(data));
			} else {
				$('#photoCaption').html(data);
			}
		});
		// Fades out the old imag
		if(slideshow) {
			$('#newAlbumImage').fadeOut(1000, function() { picChanging = false; /*console.log("picChanging FINAL Should be FALSE, it is: "+picChanging);*/ });
		} else {
			$('#newAlbumImage').fadeOut(500, function() { picChanging = false; /*console.log("picChanging FINAL Should be FALSE, it is: "+picChanging);*/ });
		}
	} else {
		alert("Slideshow Error!");
	}
}