var g_ajaxLoading = false;
var g_ajaxTarget = '#bodycopy div.img img';
var g_ajaxTarget2 = '#bodycopy div.txt';
var g_prettyPopin = false;
var g_cycleTransitionDone = true;
var g_cycleIdx = 0;
$(document).ready(function() {
	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{	
		ddaccordion.init({
			headerclass: "lv1", //Shared CSS class name of headers group
			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) {}
	if($.fn.prettyPhoto!=undefined)
		$("a[rel^='prettyOverlay'],a[rel^='prettyPhoto']").prettyPhoto({
			animationSpeed: 'normal', /* fast/slow/normal */
			padding: 40, /* padding for each side of the picture */
			opacity: 0.35, /* Value betwee 0 and 1 */
			showTitle: true, /* true/false */
			allowresize: true /* true/false */
		});
	if($.fn.prettyGallery!=undefined) {
	 $("ul.gallery").prettyGallery({
			itemsPerPage : 6,
			animationSpeed : 'normal', /* fast/normal/slow */
			navigation : 'top',  /* top/bottom/both */
			of_label: ' of ', /* The content in the page "1 of 2" */
			previous_title_label: 'Previous page', /* The title of the previous link */
			next_title_label: 'Next page', /* The title of the next link */
			previous_label: 'Previous', /* The content of the previous link */
			next_label: 'Next' /* The content of the next link */
		});
		var objTmp = null;
		// update links
		// reset all the links
		$('ul.gallery a').each(function() {
			var szHREF = $(this).attr('href');
			if(szHREF.indexOf('#')==0||$(this).attr('ajaxIgnore')==1||$(this).attr('target')=='_blank'||szHREF.indexOf('http://')==0) return;
			$(this).attr('ajaxURL', szHREF);
			$(this).attr('href', 'javascript:void(0);');
			$(this).attr('ajaxIgnore', 1)
			$(this).click(function() {linkUp(this);});
			if(objTmp==null) objTmp = $(this);
		});
		if(objTmp!=null) objTmp.click();
	}
	if($.fn.prettyPopin!=undefined) {
		g_prettyPopin = true;
		initPopin();
	}
	if($.fn.cycle!=undefined) {
		
		$('#col-banner').cycle({
		    fx: 'scrollHorz',
		    speed: 500,
		    timeout: 0,
		    easing: 'swing',
		    before: function(imgcur, imgnext, opts) {
		    	if(!g_cycleTransitionDone) return false;
		    	g_cycleTransitionDone = false;
		    	g_cycleIdx = $(this).parent().children().index(this);
		    	resetThumb(g_cycleIdx);
		    },
		    after: function(imgprev, imgafter, opts) {
		    	g_cycleTransitionDone = true;
		    	g_cycleIdx = $(this).parent().children().index(this);
		    }
		});
		$('#col-thumb img').each(function(index){
			$(this).css('cursor', 'pointer');
			$(this).attr('idx', index);
			$(this).mouseover(clickIndexThumb);
			if(index==0) {
				$(this).unbind('mouseenter mouseleave');
				$(this).attr('src', $(this).attr('rollover'));
			}
		});
	}
});
function clickIndexThumb()
{
	if(!g_cycleTransitionDone) return;
	var nIdx = new Number($(this).attr('idx'));
	if(nIdx==g_cycleIdx) return;
	g_cycleTransitionDone = false;
	g_cycleIdx = nIdx
	resetThumb(nIdx);
	$('#col-banner').cycle(nIdx);
}
function resetThumb(nIdx)
{
	// reset all
	$('#col-thumb img').each(function(index){
		$(this).attr('src', $(this).attr('norollover'));
		$(this).unbind('mouseenter mouseleave');
		$(this).hover(function () {
				$(this).attr('src', $(this).attr('rollover'));
			},
			function () {
				$(this).attr('src', $(this).attr('norollover'));
			}
		);
		if(index==nIdx) {
			$(this).unbind('mouseenter mouseleave');
			$(this).unbind('mouseenter mouseleave');
			$(this).attr('src', $(this).attr('rollover'));
		}
	});
}
function initPopin()
{
	return;
	$("a[rel^='prettyPopin']").prettyPopin({
		background: '#000',
		modal: false, /* true/false */
		width: false, /* true/false */
		height: false, /* true/false */
		opacity: 0.5, /* value from 0 to 1 */
		animationSpeed: 'fast' /* slow/medium/fast */
	});
}
function linkUp(objLink) {
	if(g_ajaxLoading) return;
	var szURL = $(objLink).attr('ajaxURL');
	if(szURL==undefined) return;
	
	if($(g_ajaxTarget).attr('src')==undefined) return;
	if($(g_ajaxTarget2).html()==null) return;
	
	g_ajaxLoading = true;
	$(g_ajaxTarget).attr('src', 'i/white.gif');
	$.ajax({url: szURL,
		type: 'GET',
		cache: false,
		dataType: 'html',
		timeout: 5000,
		error: function() {
			alert('Network connection error or page not found.');
			g_ajaxLoading = false;
		},
		success: function(data){
			g_ajaxLoading = false;
			var objDiv = document.createElement('div');
			objDiv.innerHTML = data;
			var szSrc = $(objDiv).find('img').attr('src');
			if(szSrc==undefined) return;
			$(g_ajaxTarget).attr('src', szSrc);
			$(g_ajaxTarget2).html($(objDiv).find('div:first').html());
			// set tell your fren
			try {
				var objTmp = $(g_ajaxTarget2).find('a[rel=prettyPopin]');
				var szTmp = this.url;
				szTmp = szTmp.substr(0, szTmp.indexOf('?'));
				szTmp = szTmp.replace(/\//g, '|');
				objTmp.attr('onclick', '');
				objTmp.click(function() {
					window.open("tellyourfren.php?id="+szTmp, "_blank", "width=450,height=500");
				});
			}catch(e){}
			
			if(g_prettyPopin) initPopin();
		}
	});
}
