// globals
var preloaderImage = null;
var g_cycleTransitionDone = true;
var g_cycleIdx = 0;
var g_nIntervalControl = 0;
var g_nIntervalSlideShowID = 0;
var m_btnPause = 'btn-gallery-stop-trans.png';
var m_btnPlay = 'btn-gallery-play-trans.png';
var m_nSlideShowRest = 3000;
var m_nControlTop = 20;
var m_nControlLeft = 0;

$(document).ready(function() {
	try { init(); } catch(e) {}
	initWindow();
});

function initWindow(bAjax)
{
	if($.fn.cycle!=undefined) {
		$('#banner').cycle({
		    fx: 'fade',
		    speed: 1000,
		    timeout: 5000,
		    easing: 'swing',
		    before: function(imgcur, imgnext, opts) {
		    	if(!g_cycleTransitionDone) return false;
		    	g_cycleTransitionDone = false;
		    	g_cycleIdx = $(this).parent().children().index(this);
		    },
		    after: function(imgprev, imgafter, opts) {
		    	g_cycleTransitionDone = true;
		    	g_cycleIdx = $(this).parent().children().index(this);
		    }
		});
	}
	
	try{
		$.ajax({url: 'js/accordion/'+g_includeAccordion,
			type: 'GET',
			cache: false,
			dataType: 'text',
			timeout: 10000,
			error: function() {
				alert('Unable to load accordion menu. Please refresh. Thank you.');
				g_ajaxLoading = false;
			},
			success: function(data){
				$('#sidenav').html('');
				$('#sidenav').append(data);
				ddaccordion.init({
					headerclass: "lv1", //Shared CSS class name of headers group
					headerclass2: "lv0", //non expandable header
					contentclass: "submenu", //Shared CSS class name of contents group
					revealtype: "click", //Reveal content when user clicks or onmouseover the header? Valid value: "click" or "mouseover
					mouseoverdelay: 200, //if revealtype="mouseover", set delay in milliseconds before header expands onMouseover
					collapseprev: true, //Collapse previous content (so only one open at any time)? true/false
					defaultexpanded: [], //index of content(s) open by default [index1, index2, etc] [] denotes no content
					onemustopen: false, //Specify whether at least one header should be open always (so never all headers closed)
					animatedefault: false, //Should contents open by default be animated into view?
					persiststate: false, //persist state of opened contents within browser session?
					toggleclass: ["", "selected"], //Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
					togglehtml: ["", "", ""], //Additional HTML added to the header when it's collapsed and expanded, respectively  ["position", "html1", "html2"] (see docs)
					animatespeed: "normal", //speed of animation: integer in milliseconds (ie: 200), or keywords "fast", "normal", or "slow"
					oninit:function(headers, expandedindices){ //custom code to run when headers have initalized
						//do nothing
					},
					onopenclose:function(header, index, state, isuseractivated){ //custom code to run whenever a header is opened or closed
						//initScroller();
					}
				});
			}
		});
	}catch(e) { }
	
	try{
		// set mouseover effect
		$('img[rollover]').each(function(index){
			if($(this).parent().attr('href')==CLASSID) {
				$(this).attr('norollover', $(this).attr('src'));
				$(this).attr('src', $(this).attr('rollover'));
			} else {
				$(this).attr('norollover', $(this).attr('src'));
				$(this).hover(function () {
						$(this).attr('src', $(this).attr('rollover'));
					},
					function () {
						$(this).attr('src', $(this).attr('norollover'));
					}
				);
			}
		});
		
	}catch(e) {}
	
	try {
		$("ul.gallery").prettyGallery({
		 itemsPerPage : 10,
		 animationSpeed : 'normal',  
		 navigation : 'top',  
		 of_label: ' of ',  
		 previous_title_label: 'Previous page',  
		 next_title_label: 'Next page',  
		 previous_label: '<img src="i/arrow-left.gif" border="0" />',  
		 next_label: '<img src="i/arrow-right.gif" border="0" />'  
		});
		$('#gallery-pic').attr('prevIndex', -1);
		$('#gallery-pic').click(function() {
			if($('ul.gallery').attr('myTotal')<=1) return;
			var nIdx = $(this).attr('prevIndex');
			nIdx++;
			if(nIdx+1>parseInt($('ul.gallery').attr('myItemsShown'))) {
				if(nIdx+1>parseInt($('ul.gallery').attr('myTotal'))) {
					for(var i=0;i<parseInt($('ul.gallery').attr('myPage'));i++) {
						$('#arrow-left a').dblclick();
					}
					$('ul.gallery a:eq(0)').click();
				}else {
					$('#arrow-right a').click();
					$('ul.gallery a:eq('+nIdx+')').click();
				}
			} else {
				$('ul.gallery a:eq('+nIdx+')').click();
			}
		});
		$('ul.gallery').find('a').each(function(index) {
			$(this).unbind();
			$(this).attr('myIndex', index);
			imageUnSelected($(this).find('img:first'));
			// preload
			$('#preload-image').append('<img src="'+$(this).attr('href')+'" border="0" isloaded="0" onload="$(this).attr(\'isloaded\', 1);" />');
			
			$(this).click(function() {
				if($('#gallery-pic').attr('prevIndex')==-1) {
					imageSelected($('ul.gallery a:eq('+$(this).attr('myIndex')+') img'));
				}else if($('#gallery-pic').attr('prevIndex')!=$(this).attr('myIndex')) {
					imageUnSelected($('ul.gallery a:eq('+$('#gallery-pic').attr('prevIndex')+') img'));
					imageSelected($('ul.gallery a:eq('+$(this).attr('myIndex')+') img'));
				} else return false;
				$('#gallery-pic').attr('nextImage', $(this).attr('href'));
				$('#gallery-pic').attr('prevIndex', $(this).attr('myIndex'));
				$('#gallery-pic').attr('title', $(this).attr('title'));
				
				// overlay
				removeLoading();
				if($('#preload-image').find('img[src$="'+$(this).attr('href')+'"]').attr('isloaded')==0)
					showLoading();
				
				// check preloader
				if($('#soul_overlay').html()==null) {
					loadPic($('#gallery-pic'));
				}else {
					$('#gallery-pic').css('display', 'none');
					$('#soul_overlay').css('display', 'block');
					preloaderImage = setTimeout(function() { checkPreloader($('#gallery-pic')); }, 100);
				}
				return false;
			});
		});
		$('.playpause').css('position', 'absolute');
		$('.playpause').css('cursor', 'pointer');
		$('.playpause').css('display', 'none');
		var objPos = $('#img-holder').offset();
		objPos.left+=((isNaN(parseInt($('#img-holder').css('margin-left'))))?0:parseInt($('#img-holder').css('margin-left')))+parseInt($('#img-holder').css('padding-left'));
		objPos.left+=m_nControlLeft;
		objPos.top+=((isNaN(parseInt($('#img-holder').css('margin-top'))))?0:parseInt($('#img-holder').css('margin-top')))+parseInt($('#img-holder').css('padding-top'))+parseInt($('#img-holder').height()-$('.playpause').height());
		objPos.top+=m_nControlTop;
		$('.playpause').css('left', objPos.left+'px');
		$('.playpause').css('top', objPos.top+'px');
		if(false) {
		$('#img-holder').hover(function () {
				if(g_nIntervalControl>0) {
					clearTimeout(g_nIntervalControl);
					$('.playpause').stop(true, true);
					g_nIntervalControl = setTimeout(function() { $('.playpause').fadeOut(); }, 2000);
				}
				$('.playpause').fadeIn();
			},
			function () {
				clearTimeout(g_nIntervalControl);
				$('.playpause').stop(true, true);
				g_nIntervalControl = setTimeout(function() { $('.playpause').fadeOut(); }, 2000);
			}
		);}
		$('.playpause').click(function() {
			$('.playpause').css('display', 'block');
			if($(this).find('img:first').attr('src').indexOf(m_btnPause)>=0) {
				$(this).find('img:first').attr('src', $(this).find('img:first').attr('src').replace(m_btnPause,m_btnPlay));
				if(g_nIntervalSlideShowID!=0) {
					clearTimeout(g_nIntervalSlideShowID);
					g_nIntervalSlideShowID = 0;
				}
				g_nIntervalSlideShowID = setTimeout(function() {slideShow();}, m_nSlideShowRest);
			} else {
				$(this).find('img:first').attr('src', $(this).find('img:first').attr('src').replace(m_btnPlay,m_btnPause));
				if(g_nIntervalSlideShowID!=0) {
					clearTimeout(g_nIntervalSlideShowID);
					g_nIntervalSlideShowID = 0;
				}
			}
		});
		$('.playpause').click();
		
	} catch(e) { }
}
function slideShow() {
	clearTimeout(g_nIntervalSlideShowID);

	$('#gallery-pic').click();
	g_nIntervalSlideShowID = setTimeout(function() {slideShow();}, m_nSlideShowRest);
}
function updateFooter()
{
	return '';
}
function showLoading(szMsg)
{
	var nWidth = $('#gallery-pic').width();
	var nHeight = $('#gallery-pic').height();
	var nLeft = nWidth/2-100;
	var nTop = nHeight/2-60;
	if(szMsg==''||szMsg==undefined) szMsg = 'Please wait while we load your image...';
	$('<div id="soul_overlay" style="display:none;width:'+nWidth+'px;height:'+nHeight+'px;"></div><div id="soul_message" style="left:'+nLeft+'px;top:'+nTop+'px;"><b>'+szMsg+'</b></div>').appendTo('#img-holder');
}
function removeLoading()
{
	$('#soul_overlay').remove();
	$('#soul_message').remove();
	$('#gallery-pic').css('display', 'block');
}
function loadPic(myObj)
{
	removeLoading();
	$('#gallery-bg').remove();
	$('#img-holder').prepend('<div id="gallery-bg" style=\'background:#ffffff '+$(myObj).css('background-image')+' no-repeat center center;\'></div>');
	$(myObj).css('background-image', 'url("'+$(myObj).attr('nextImage')+'")');
	$('#gallery-bg').fadeOut(500, function() {
		$('#gallery-bg').remove();
	});
}
function checkPreloader(myObj)
{
	clearTimeout(preloaderImage);
	if($('#preload-image').find('img[src$="'+$(myObj).attr('nextImage')+'"]').attr('isloaded')==0)
		preloaderImage = setTimeout(function() { checkPreloader(myObj); }, 100);
	else
		loadPic(myObj);
}
function imageSelected(myObj)
{
	myObj.css('opacity', '1');
	myObj.css('filter', 'alpha(opacity=100)');
	myObj.css('-moz-opacity', '1');
	myObj.css('-khtml-opacity', '1');
}
function imageUnSelected(myObj)
{
	myObj.css('opacity', '0.5');
	myObj.css('filter', 'alpha(opacity=50)');
	myObj.css('-moz-opacity', '0.5');
	myObj.css('-khtml-opacity', '0.5');
}