$(function(){
	
	//music player
	var flashvars = {};
	flashvars.playlist = "/_xml_music.php";
	var params = {};
	var attributes = {};
	attributes.id = "pi1";
	attributes.wmode="transparent";
	attributes.quality="high";
	swfobject.embedSWF("/swf/pi.swf", "music", "360", "400", "9.0.0", false, flashvars, params, attributes);


	//initialize everything
	init();

	//redirect external deeplinks
	var pathname = window.location.pathname;
	if(pathname != "/"){
		var newurl = pathname.substring(1);
		window.location = "http://www.georgefoto.com/#" + newurl;
		//$.history.load(newurl);
	}

    $.history.init(function(hash){
        if(hash == "") {
            getPage("Home");
        } else {
            getPage(hash);
        }
    },
    { unescape: ",/" });
	
});

function init(url){

	$(".fancybox").fancybox({
		easingIn: "easeInQuint",
		easingOut: "easeOutQuint",
		'onStart': pauseCycle,
		'onClosed': resumeCycle,
		transitionIn: "fade",
		transitionOut: "fade",
		overlayColor: "#000000"
	});

	$(".ajax").each(function(){
		if($(this).attr("href")){
			var href = $(this).attr("href");
			$(this).attr("rel",href.substring(1));	
		}
	});
		
	$("#menu a, a.ajax").unbind().click(function(){
		var url = $(this).attr("rel");
		$(this).removeAttr("href");
		window.location.hash = "#" + url;
		$.history.load(url);
		return false;
	});
	
	$("#content.home .splash").cycle({
		fx: 'scrollLeft',
		timeout: 4000,
		easing: "easeInOutQuint",
		pause: 1
	});
	
	
	$(".moreitems .accordion li").hover(function(){
		$(this).find(".description").slideDown();
	}, function(){
		$(this).find(".description").slideUp();
	});
	
	$(".marquee .item, .splash .item").hover(function(){
		$(this).find(".description").slideDown();
		$(this).find(".next").fadeIn();
		$(this).find(".previous").fadeIn();
	}, function(){
		$(this).find(".description").slideUp();
		$(this).find(".next").fadeOut();
		$(this).find(".previous").fadeOut();
	});

	$("#gallery").cycle({
		fx: 'fade',
		timeout: 4000,
		speed: 2000,
		easing: "easeInOutQuint",
		pause: 1,
		next: ".next",
		prev: ".previous"
	});
	
	if($("#content.albums_detail").length){
		var id = url.replace("Albums/","");
		var pos = id.indexOf("/");
		id = id.substr(0, pos);

		/*
		var flashvars = {};
		flashvars.pagesXML = "/_xml_album.php?id=" + id;
		flashvars.settingsXML = "/xml/settings.xml";
		var params = {};
		var attributes = {};
		attributes.wmode="transparent";
		attributes.quality="high";
		swfobject.embedSWF("/swf/album.swf", "albumplayer", "950", "600", "9.0.0", false, flashvars, params, attributes);
		*/

	}

	$(".accordion").kwicks({
		min: 50,
		sticky: "true",
		easing: "easeInOutQuint",
		duration: 1000,
		spacing:  1
	});


	$("#comments h4").hoverIntent(function(){
		$(".commentform").slideDown();
	}, function(){
	
	});

	//comment submission
	$(".commentform .input_button").click(function(){
		var name = $("input[name='name']").val();
		var email = $("input[name='email']").val();
		var comment = $("textarea[name='comment']").val();
		var post_id = $("input[name='post_id']").val();
		
		if(name == "" || email == "" || comment == ""){
			alert("Please fill in all the fields");
		} else {
			$.ajax({
				type: "POST",
				url: "/_ajax_comment.php",
				data: ({
					name: name,
					email: email,
					comment: comment,
					post_id: post_id
				}),
				success: function(msg){
					$("#comments .comments").append(msg);
					$("input[name='name']").val("");
					$("input[name='email']").val("");
					$("textarea[name='comment']").val("");
					$(".nocomments").fadeOut();
					$(".commentform").slideUp();
				}
			});			
		}
	});
	
	//contact us submission
	$(".contactform .questions_submit").click(function(){
		var name = $("input[name='name']").val();
		var email = $("input[name='email']").val();
		var comment = $("textarea[name='comment']").val();
		
		if(name == "" || email == "" || comment == ""){
			alert("Please fill in all the fields.");
		} else {
			$.ajax({
				type: "POST",
				url: "/_ajax_contact.php",
				data: ({
					name: name,
					email: email,
					comment: comment
				}),
				success: function(msg){
					$(".contactform").html("<p>Your message has been sent. We'll be in touch shortly!</p>");
				}
			});			
		}		
	});

	
}

function pauseCycle(){
	$("#gallery").cycle("toggle");
}

function resumeCycle(){
	$("#gallery").cycle("resume");
}

function getPage(url){
		
	$.ajax({
		type: "POST",
		url: url,
		data: {ajaxcall: "true"},
		success: function(msg){
			$("#container").html(msg);			
			init(url);
		}
	});			

}
