if(location.hash.length > 0) {
	window.location=location.hash.substr(1);
}

$(document).ready(function() {
	$("a[href^=http://]").click(function() {
		if(!$(this).attr("href").match(/gussdrivein.com/)) {
			extUrl=$(this).attr("href").replace("http://", "");
			if(typeof(pageTracker)!="undefined") {
				pageTracker._trackPageview("/external-links/"+extUrl);
			}
		}
	});
	
	$("a[href$=pdf]").click(function() {
		if(typeof(pageTracker)!="undefined") {
			pageTracker._trackPageview($(this).attr("href"));
		}
	});
	
	$("a[rel=colorbox]").colorbox({iframe: true, width: 800, height: 500});
	
	swfobject.registerObject("timeToEatFlash");
	
	$("#photosPrev").click(function() {
		photos.scrollPrev();
	});
	
	$("#photosNext").click(function() {
		photos.scrollNext();
	});
	
	$("#photoThumbsInner img").live("click", function() {
		clickedID=$(this).attr("id").split("_")[1];
		photos.loadMain(clickedID);
		flickrID=photos.photoData.photos[clickedID].flickrID;
		location.hash="cruise-nights.php?photoID="+flickrID;
	});
	
	$(".ccImageThumb").live("click", function() {
		newHref=$("#ccBigImage").attr("src");
		$("#ccBigImage").attr("src", $(this).attr("href"));
		newSrc=newHref.substr(0, (newHref.length - 4));
		newSrc+="_thumb.jpg";
		$(this).attr("href", newHref);
		$(this).children("img").attr("src", newSrc);
		return false;
	});
});

var photos = {
	photoData: {photoCount: 0, photos: []},
	perPage: 5,
	perLoad: 10,
	start: 0,
	baseShareUrl: "http://gussdrivein.com/cruise-nights.php?photoID=",
	
	init: function() {
		$.getJSON("ajax/photosList.php", {start: photos.start}, function(data) {
			photos.photoData.photoCount=data.photoCount;
			for(var i in data.photos) {
				photos.photoData.photos[data.photos[i].id]=(data.photos[i]);
			}
			if($("#mainPhoto img").attr("src")=="") {
				photos.loadMain(0);
			}
			photos.loadThumbs();
		});
	},
	
	loadThumbs: function() {
		$("#photoThumbsInner").empty();
		for(i=photos.start ; i < (photos.start + photos.perPage) ; i++) {
			if(i > (photos.photoData.photoCount - 1)) {
				iNum=(i - photos.photoData.photoCount);
			} else {
				iNum=i;
			}
			if(typeof(photos.photoData.photos[iNum])=="undefined") {
				//console.log("Unable to load: "+iNum);
			} else {
				loadingImg=$("<img />").attr("src", "images/loading.gif").attr("id", "photo_" + iNum).css({"display": "block", "margin": "30px auto 0"});
				imgDiv=$("<div />").append(loadingImg);
				$("#photoThumbsInner").append(imgDiv);
				
				$("<img />")
					.attr("src", photos.photoData.photos[iNum].thumb)
					.attr("data-id", iNum)
					.load(function() {
						$("#photo_" + $(this).attr("data-id")).replaceWith($(this));
						$(this).attr("id", "#photo_" + $(this).attr("data-id"));
					});
			}
		}
	},
	
	loadData: function(prevNext) {
		if(prevNext=="prev") {
			startFrom=photos.start - photos.perPage;
		} else {
			startFrom=photos.start + photos.perPage - 1;
		}
		$.ajax({async: false, url: "ajax/photosList.php", data: {start: startFrom}, success: function(data) {
			for(var i in data.photos) {
				photos.photoData.photos[data.photos[i].id]=(data.photos[i]);
			}
		}, dataType: "json"});
	},
	
	loadMain: function(mainID) {
		$("#mainPhoto img").remove();
		$("#mainPhoto").append($("<img />").attr("src", "images/loading.gif"));
		
		if(photos.photoData.photos[mainID].height > photos.photoData.photos[mainID].width) {
			topMargin="-" + ((photos.photoData.photos[mainID].height - 350) / 2) + "px";
		} else {
			topMargin=0;
		}
		flickrID=photos.photoData.photos[mainID].flickrID;
		
		$("<img />")
			.attr("src", photos.photoData.photos[mainID].medium)
			.css("top", topMargin)
			.load(function() {
				$("#mainPhoto img").replaceWith($(this));
			});
			
		$("#mainPhoto a").attr("href", photos.photoData.photos[mainID].url);
		$("#photoShareFacebook").attr("href", "http://www.facebook.com/sharer.php?u="+photos.baseShareUrl+flickrID);
		$("#photoShareTwitter").attr("href", "http://twitter.com/?status=Check out this cruise night picture at "+photos.baseShareUrl+flickrID);
		$("#photoShareEmail").attr("href", "mailto:?subject=Gus's Drive In Cruise Night&amp;body=Check out this cruise night picture at "+photos.baseShareUrl+flickrID);
		if(typeof(pageTracker)!="undefined") {
			pageTracker._trackPageview("/cruise-nights.php?photoID="+flickrID);
		}
	},
	
	scrollPrev: function() {
		photos.start--;
		if(photos.start < 0) {
			photos.start=(photos.photoData.photoCount - 1);
		}
		if(typeof(photos.photoData.photos[photos.start]) == "undefined") {
			photos.loadData("prev");
		}
		photos.loadThumbs();
	},
	
	scrollNext: function() {
		photos.start++;
		if(photos.start > photos.photoData.photoCount) {
			photos.start=0;
		}
		if((photos.start + photos.perPage) >= photos.photoData.photos.length) {
			photos.loadData("next");
		}
		photos.loadThumbs();
	}
};

function checkCarForm() {
	var requiredFields=["name", "email", "car_info", "content"];
	returner=true;
	$(".error").remove();
	$.each(requiredFields, function(e, val) {
		fieldID="#form_"+val;
		if($.trim($(fieldID).val())=="") {
			addError(fieldID, "This field is required!");
			returner=false;
		}
	});
	return returner;
}

function addError(fieldID, errorValue) {
	console.log("adding error");
	$(fieldID).after('<div class="error">'+errorValue+'</div>');
	$(fieldID).focus();
}

function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}