// JavaScript Document
//Holder Image
var img = new Image(); 
$(function(){ //Make Sure DOM is loaded

	//Inital Loading of First Gallery who has a calls of 'active' assigned to it. 
	var initObj = $('li.active a');
	var pageHeight = $('.footer').offset();
	
	//Load Initial Gallery (Rafts)
	loadSub(initObj);
	
	
	//Setup Event Listener for any Ajax call
	$('.sub_main_container').ajaxComplete(function(e, xfer, settings) {
	  //Check for HTML Data Type not Jason or Null so that the click events are only reset when a new gallery is loaded not just an image.
	  if(settings.dataType == 'html'){
		clickSetup();
	  }
	});
	
	//Setup Events
	$('.pop_nav_container').mouseover(function (){
		//alert($('.prev_button').css('display'));
		if($('.prev_button').css('display') != ''){
			$('.prev_button').fadeIn('fast');
			$('.next_button').fadeIn('fast');	
		}
	});
	
	$('.pop_nav_container').mouseleave(function (){
		$('.prev_button').fadeOut('fast');
		$('.next_button').fadeOut('fast');		
	});
	
	$('.prev_button').click(function (){
		movePhotoTo($(this).attr('ref'));
	});
	$('.next_button').click(function (){
		movePhotoTo($(this).attr('ref'));
	});
	
	$('#popbox_close').click(hidePopUp);
	
	
	//Setup Image Load Event Listener
	$(img).load(function(){
			//alert('image loaded');	
			$('.loader_screen').hide();
			$('.pop_nav_container').fadeIn('fast');
			$('.prev_button').hide();
			$('.next_button').hide();	
	});
	
	//Handle Sroll
	$(window).scroll(adjustWindow);
});

function clickSetup(){
	
	//Unbind previous
	$('a.galleryThumbnail').unbind('click');
	$('a.galleryThumbnail').unbind('mousedown');
	//alert('Click Setup Called');
	//Bing New Links
	$('a.galleryThumbnail').click(function (){	
		return false;
	});
	$('a.galleryThumbnail').mousedown(function (){				
		showPopUp($(this));
	});
	
}

function showPopUp(obj){
	var url = obj.attr('href');
	loadImage(url);
	//Get Page Height
	var position = $('.footer').position();
	$('.galleryPop').css('height',position.top + 150);
	$('.galleryPop').fadeIn('fast');
	adjustWindow();
	return false;
}

function hidePopUp(){
	$('.galleryPop').fadeOut('fast', function(){
		$('.pop_nav_container').hide();								  
	});
}

function movePhotoTo(url){
	var url = url;
	//Hide Current Image
	$('.pop_nav_container').fadeOut('fast', function(){
		//alert('Move to: ' + url );
		loadImage(url);								 
	});	
	
}

function loadImage(url){
	
	//Show Loading Screen
	$('.loader_screen').show();
	
	//Setup URL to append /JSON/ this will make page return Json code instead of HTML
	url = url + '/JSON/';
	$.getJSON(url, function(dat) {
	  windowAnimate(dat);
	});
}

function windowAnimate(jobj){
	//Populate Image Window
	$('.galleryPop .pop_nav_container .prev_button').attr('id','prev_' + jobj.prevLinkID);
	$('.galleryPop .pop_nav_container .next_button').attr('id','next_' + jobj.nextLinkID);
	$('.galleryPop .pop_nav_container .prev_button').attr('ref',jobj.prevLink);
	$('.galleryPop .pop_nav_container .next_button').attr('ref',jobj.nextLink);
	$('.galleryPop .img_box_discription_container').html(jobj.description);
	$('#ajaxresult img').attr('src',jobj.src);
	
	//Resize Image Window
	$('.img_box_pop_container').animate({
		width: jobj.width + 'px',
		height: jobj.height + 'px'
		}, 1000, function() {
			//alert('Animation Done');
			//Start Image Loading
			$(img).attr('src',jobj.src);
			$('.galleryPop .pop_nav_container').css('backgroundImage','url('+jobj.src+')');
		}); 
}

function adjustWindow(){
	if($('.galleryPop').css('display') != 'none'){
		$('#height_controller').css('height',$(window).scrollTop() + 25);
	}
}
