/* Web Defined
*  08/01/10
*/


/* Vars *****************************************************************/
var imgSwapCount = 0;
var swapCounter = 0;
var swapInterval;
var cNum = null;

var disOpen = new Boolean();
var fadeSpeed;
var offset;
var scrolltop;

disOpen = false;
fadeSpeed  = "fast";



/** ON READY **********************************************************************/
$(document).ready(function() {
	if($('div#img-swap').length){ //off 031711
		imgSwapController();
	}
	
	/** Widgets **************************************************/
	
	//home-content
	//img-swap
	$('div#img-swap-nav ul li').live('click', function() {
		 // clearInterval(swapInterval); //off 031711
		  var tt  = parseInt(this.id) - 1;
		  cNum = tt;
		  imgSwapTrigger();
		});
	$('div.more-next').live('click', function() { //added 031711
		  var tt  = parseInt(this.id);
		 // alert(tt);
		  cNum = tt;
		  imgSwapTrigger();
		});
	//gallery
	$('div.gallery-wrapper').live("click", function(){
		var id = $(this).attr('id');
	  	offset = $(this).offset();
		scrolltop = $(window).scrollTop();
		callData('gal',id);
		if(window.event) event.returnValue = false;
		return false;
	});
	$('#gallery_thumbs div.thumb img').live("click", function(){
      	var getFile = $(this).attr('id');
		var getTitle = $(this).attr('alt');
		getLargeImage(getFile, getTitle);
    });
	$('div.interior-gallery-link').live("click", function(){
		var id = $(this).attr('id');
	  	offset = $(this).offset();
		scrolltop = $(window).scrollTop();
		callData('gal',id);
		if(window.event) event.returnValue = false;
		return false;
	});

	//dialog - close
	$('div#dialog .close').click(function () { //hide video player
		dialogClose();
		if(window.event) event.returnValue = false;
		return false;
	});
	
	$('.bgCover').click(function () { //hide video player
		dialogClose();
		if(window.event) event.returnValue = false;
		return false;
	});


});

/* Functions *****************************************************************/
function imgSwapController(){
	imgSwapCount = $('div#img-swap-wrap ul').children().length;
	$('div#img-swap-wrap ul').children().css('display', 'none');
	$('div#img-swap-wrap ul li').eq(0).css('display', 'block');
	
	$('div#img-swap-info-wrap div.section').css('display', 'none');
	$('div#img-swap-info-wrap div.section').eq(0).css('display', 'block');
	
	//swapInterval = setInterval(imgSwapTrigger, 4500); //off 031711
	return true;
}

function imgSwapTrigger(){
	//clearInterval(swapInterval); //off 031711
	var tmp = swapCounter;
	
	if(cNum != null){
		swapCounter = cNum;
	}
	
	swapCounter++;
	if(swapCounter > imgSwapCount - 1){
		swapCounter = 0;
	}
	
	$('div#img-swap-wrap ul li').eq(tmp).fadeIn('slow', function() {
        $('div#img-swap-wrap ul li').eq(tmp).css('display', 'none');
		imgSwapInTrigger()
      });
	
	$('div#img-swap-info-wrap div.section').eq(tmp).css('display', 'none');
	
	//swapInterval = setInterval(resetImgSwapInterval, 4500); //off 031711
	cNum = null;
	return false;
}

function imgSwapInTrigger(){
	$('div#img-swap-wrap ul li').eq(swapCounter).fadeIn("slow");
	$('div#img-swap-info-wrap div.section').eq(swapCounter).fadeIn("slow");
	
	$('div#img-swap-nav ul li').removeClass('selected');
	$('div#img-swap-nav ul li').eq(swapCounter).addClass('selected');
	return false;
}
	
function resetImgSwapInterval(){
	clearInterval(swapInterval);
	swapInterval = setInterval(imgSwapTrigger, 4500);
}
	
function initGallery(){
	getFile = $("#gallery_thumbs a:first").attr('id');
	getLargeImage(getFile);
}

function getLargeImage(file, title){
	var img = 'assets-images/gallery/lrg/' + file;
	$('#gallery_photo img').attr('src',img);
	$('#dialog-content #title').html(title);
	
}
	
/** DIALOG *************************************************************************/
function showOverlayBox() {
	if( disOpen == false ) return;  //if  not set to open -> do nothing
	$('#dialog').css({display:"block",left:( $(window).width() - $('#dialog').width() )/2,top:scrolltop+30,position:"absolute"});	
	//$('.bgCover').css({width: $(window).width(),height:$('#wrap').height() + 40}); 
	$('.bgCover').css({width: $(window).width(),height:2000 + 40}); 
}

function doOverlayOpen() {
	disOpen = true; 
	showOverlayBox(); 
	$('.bgCover').fadeIn(fadeSpeed);
	return false; 
}

function doOverlayClose(){
	disOpen = false;
	$('.bgCover').css({opacity:0.90});
}

function dialogClose(){
	 $('#dialog').hide(1, function () {
		isOpen = false;
		$('.bgCover').fadeTo(400,0, function () {
			$('.bgCover').hide();
			doOverlayClose();
		});
	});
	
}

function openRSDialog(){
	$('.bgCover').css({opacity:0.90}); 
	$(window).bind('resize',showOverlayBox); 
	doOverlayOpen();
} 


/** DATA *************************************************************************/
function callData(type, fid){
	var furl;
	if(type == 'gal'){ 
		furl = 'lib/inc/pop-gallery.php'; 
		//$('#dialog').addClass('dialog-gal');
	}
	$.ajax({
		url: furl
		,cache: false
	   ,type: "POST"
		,data: ({
			id: fid
		})
	   ,dataType: "html"
	   ,error: function(XMLHttpRequest, textStatus, errorThrown){
		// not
	  }
	   ,success: function(data){
			$('div#dialog-content').empty();
			$('div#dialog-content').html(data);	
			openRSDialog();		
		} //eo success
	 }); //eo ajax
}//eo callData
	
	
	
	
	
	
	
	
	
