Cufon.replace('h1');

var bookmarks = function(){ // loads works or pages directly in case of direct URL input (i.e. a bookmark)
	//alert(document.URL);
	var currentURL = document.URL;
	var URLParts = 	currentURL.split('#');
	//alert(URLParts[1]);
	if(URLParts[1] != '' && URLParts[1] != undefined){
		$.ajax({
		    url: 'includes/check-url.php',
		    type: 'GET',
		    data: 'urlPart='+URLParts[1],
		    dataType: 'text',
		    timeout: 1000,
		    error: function(){
		        alert('Error loading data');
		    },
		    success: function(text){
		    	//alert(text);
		        if(text == 'success'){
		        	var postcardY = ($(window).height() / 2) - ($('#postcardContainer').height() / 2) + 10;
					$('#postcardContainer').css('top', postcardY);
					$('#overlay').css('display', 'block');
					$('#overlay').animate({opacity:0.65}, 'slow');
					$('#postcardContainer').fadeIn('slow', function() {
						$('#loaderImg').show();
						$('#loaderCopy').show();
						$('#slideshow').load('includes/images.php?work=' + URLParts[1]);
						$('#copy').load('includes/copy.php?work=' + URLParts[1]);
					});
		        } else if(text == 'successPage'){
		        	var postcardY = ($(window).height() / 2) - ($('#postcardContainer').height() / 2) + 10;
					$('#postcardContainer').css('top', postcardY);
					$('#overlay').css('display', 'block');
					$('#overlay').animate({opacity:0.65}, 'slow');
					$('#postcardContainer').fadeIn('slow', function() {
						$('#loaderImg').show();
						$('#loaderCopy').show();
						$('#slideshow').load('includes/images.php?page=' + URLParts[1]);
						$('#copy').load('includes/copy.php?page=' + URLParts[1]);
					});
		        }
		    }
		});
	}
}

var closeIt = function(){ // closes overlay in case of a non-existing reference
	$('#loaderImg').hide();
	$('#loaderCopy').hide();
	$('#overlay, #postcardContainer').fadeOut('slow', function(){
		$('#slideshowControls, #slideshow, #copy').empty();
	});
}

var reset = function() { // return cards to original positions
	$("div.cardShadow").stop().animate({
		"left": $("div.cardShadow").data("Left") + 6, 
		"top": $("div.cardShadow").data("Top") + 2
	}, "fast", function(){$('#logo').css('cursor', 'default');});
}

function externalLinks() {  
 if (!document.getElementsByTagName) return;  
 var anchors = document.getElementsByTagName("a");  
 for (var i=0; i<anchors.length; i++) {  
   var anchor = anchors[i];  
   if (anchor.getAttribute("href") &&  
       anchor.getAttribute("rel") == "external")  
     anchor.target = "_blank";  
 }  
}

$(document).ready(function() {
	bookmarks();
	if(!$.browser.msie){
		$('div.cardShadow div.copy').css('cursor', 'move');
		$("div.cardShadow").data("Left", $("div.cardShadow").offset().left - 262).data("Top", $("div.cardShadow").offset().top - 14);

		$('div.cardShadow').draggable({
			stack: '.cardShadow',
			zIndex: 2700,
			opacity: 0.75,
			handle: 'div.copy',
			containment: 'window',
			start: function(){$('#logo').css('cursor', 'pointer');}
			//grid: [248, 348]
		});
		$('#postcardShadow').draggable({
			handle: '#leftCol'
		});
		$(window).resize(reset);
		$("#logo").click(reset);
	}
	
	$('#slideshow').ajaxComplete(function(e, xhr, settings) { // check for ajax requests completion
		if (settings.url.indexOf('images') >= 0) { // if image request complete
			$('#loaderImg').hide();
			$(this).cycle({fx:'fade', speed:1000, timeout:0, pager:'#slideshowControls'});
			Cufon.replace('#slideshowControls a');
			$('#slideshowControls a').click(function(){
				Cufon.replace('#slideshowControls a');
			});
		} else if (settings.url.indexOf('copy') >= 0) { // if copy request complete
			$('#loaderCopy').hide();
			Cufon.replace('h1');
			Cufon.replace('label');
		}
	});
	
	$('a.showWork').click(function(e) { // load postcard with work
		var hrefVal = $(this).attr('href');
		var work = hrefVal.replace('/#', '');
		var postcardY = ($(window).height() / 2) - ($('#postcardContainer').height() / 2) + 10;
		$('#postcardContainer').css('top', postcardY);
		$('#overlay').css('display', 'block');
		$('#overlay').animate({opacity:0.65}, 'slow');
		$('#postcardContainer').fadeIn('slow', function() {
			$('#loaderImg').show();
			$('#loaderCopy').show();
			$('#slideshow').load('includes/images.php?work=' + work);
			$('#copy').load('includes/copy.php?work=' + work);
		});
	});
	
	$('a.sectionLink').click(function() { // load postcard with section
		var section = $(this).attr('id');
		var postcardY = ($(window).height() / 2) - ($('#postcardContainer').height() / 2) + 10;
		$('#postcardContainer').css('top', postcardY);
		$('#overlay').css('display', 'block');
		$('#overlay').animate({opacity:0.65}, 'slow');
		$('#postcardContainer').fadeIn('slow', function() {
			$('#loaderImg').show();
			$('#loaderCopy').show();
			$('#slideshow').load('includes/images.php?page=' + section);
			$('#copy').load('includes/copy.php?page=' + section);
		});
	});
	
	$('#closeIt').click(function() { // unload postcard
		$('#loaderImg').hide();
		$('#loaderCopy').hide();
		$('#overlay, #postcardContainer').fadeOut('slow', function(){
			$('#slideshowControls, #slideshow, #copy').empty();
		});
	});
	
	
	
	externalLinks();
});
