//variables
var ic=0;
var tH;
var tW;
var key=1;
var fp;
var fw;
var fH;
var bH;
var margins;
var thumbcon_width=0;
var subcatname;

var mCtimout=5000; //5 sec
var mCanispeed=1000; //400 ms
var Mci=0;

$(document).ready(function(){
	
	$('#fdeimgs img').hide().eq(0).show();
	
	$(window).bind("load",function(){
								   
		//subcatname
		
		//$('#fdeimgs').MyCycle();
		
			$('#fdeimgs').height($(window).height()-160);
			hp = getStaticValues(1153, 800);
			hpp=hp[0];hpw=hp[1];
			$('#fdeimgs').children().css({left: hpp, top:"2%", width: hpw, height: fH});
		
		$(window).bind("resize", function(){
			$('#fdeimgs').height($(window).height()-160);
			hp = getStaticValues(1153, 800);
			hpp=hp[0];hpw=hp[1];
			$('#fdeimgs').children().css({left: hpp, top:"2%", width: hpw, height: fH});
		});
		
		
		$('#contactform').submit(function(){
			fA=new Array();
			$('#contactform .txtbox').each(function(){
				fieldstring=$(this).val();
				fA.push(fieldstring);										
			});
			$.post("media/php/sndmail.php",{f1:fA[0],f2:fA[1],f3:fA[2],f4:fA[3],f5:fA[4],f6:fA[5]},function(data){
				alert(data);
			});
			return false;
		});
		
		
		$('#nav>li>ul>li').each(function(){
			cattext=$(this).children('a').text();
			if(cattext==subcatname)
			{
				$(this).children('ul').customFadeIn(300);	
			}
		});
		
		$('#nav li ul li').click(function(){
			thiselement=$(this).children('ul');
			if(thiselement.css("display")=="none")
			{
				
				$('#nav li ul li').each(function(){
					if($(this).children('ul').css("display")=="block")
					{
						$(this).children('ul').fadeOut(300);
					}
				});
				
				thiselement.customFadeIn(300);	
			}						  
		});
	});
	
	
	if($('.display').length>0){
	
	$('.thumbs span img').each(function(i){
		$(this).thumbSetup(images, i);
	});
	$('.thumbs').focusThumb(key);
	$().selectedImage(key);
	
	resetPositions();
	
	$(window).bind("resize",function(){
			resetPositions();							 
	});
	
	$('.thumbs span img').click(function(){
		thisindex = $('.thumbs span img').index(this);
		key = thisindex;
		$('.thumbs').focusThumb(thisindex);
		$().selectedImage(thisindex);
		$('.dImage img').rightClick(function(){return false;});
	});
	
	$('#left').click(function(){
		key--
		$('.thumbs').focusThumb(key);
		$().selectedImage(key);	
		$('.dImage img').rightClick(function(){return false;});
	});
	$('#right').click(function(){
		key++
		$('.thumbs').focusThumb(key);
		$().selectedImage(key);	
		$('.dImage img').rightClick(function(){return false;});
	});
	}

	
});

//functions



$.fn.selectedImage = function(i) {	
	k = key-1;
	mx = images.length-1;
	var kleft;
	var kright;
	$('.display').children().hide().empty().each(function(){
		if(k>=0&&k<=mx){
			$('.arrow').fadeIn(300);		
			$(this).show().append("<img src='"+images[k]['s']+"'/>");
		} 
		else{
			if(k<=0){kleft=1;}
		else if(k>=mx){kright=1;}$(this).hide();}
		k++;
	});
	if(kleft>0){$('#left').fadeOut(300);}
	if(kright>0){$('#right').fadeOut(300);}
	getPositions(images);
}

$.fn.focusThumb = function(i){
	w = $(this).children().eq(0).width();
	pos = ($(window).width()/2)-(w*i)-(w/2);
	$(this).css({width: w*$(this).children().length+100}).animate({left: pos},{queue: false});
}


function resetPositions(){
	$('.display').height($(window).height()-160);
	getPositions(images);
}

function getPositions(ia)
{
	element=$('.dImage');
	fH=$('.display').height()-percentage(4,$('.display').height());
	bH=$('.display').height()-percentage(40,$('.display').height());
	margins = 80;
	
	fi = getValues(1,ia,key);
	fp=fi[0];fw=fi[1];	
	if(key-1>=0)il = getValues(0,ia,key-1);
	if(key+1<=$('.thumbs span').length-1)ir = getValues(2,ia,key+1);
	
	element.eq(1).css({left: fp, top:"2%", width: fw, height: fH});
	if(key-1>=0)element.eq(0).css({left: il[0], top:"20%", width: il[1], height: bH});	
	if(key+1<=$('.thumbs span').length-1)element.eq(2).css({left: ir[0], top:"20%", width: ir[1], height: bH});
	
	arr_h = ($('.display').height()/2)+(80-22);
	$('#left').css({top: arr_h, left: (fp-45)+2});
	$('#right').css({top: arr_h, left: fp+fw+2});
}


function getValues(t,ia,i){
	a=aspectR(ia[i]['w'], ia[i]['h']);
	if(t==1){if(ia[i]['w'] > ia[i]['h']){w=parseInt(fH*a);}else{w=parseInt(fH/a);}}
	else{if(ia[i]['w'] > ia[i]['h']){w=parseInt(bH*a);}else{w=parseInt(bH/a);}}
	//conditionals if  L / F / R 
	if(t==0) cal=p=parseInt(fp-margins-w);
	if(t==1) cal=($(window).width()-w)/2;
	if(t==2) cal=fp+margins+fw;
	p=parseInt(cal);
	array=[p,w];return array;
}

function getStaticValues(width, height){	
	fH=$('#fdeimgs').height()-percentage(4,$('#fdeimgs').height());
	a=aspectR(width,height);
	if(width > height)
	{
		w=parseInt(fH*a);}
	else
	{
		w=parseInt(fH/a);
	}
	cal=($(window).width()-w)/2;
	p=parseInt(cal);
	array=[p,w];return array;
}


	$.fn.MyCycle = function(){
		imgcount=$(this).children('img').size()-1;
		if(Mci==imgcount)
		{
			mCanimate(Mci,0,$(this));
			Mci=0;
		}
		else
		{
			mCanimate(Mci,Mci+1,$(this));
			Mci++;
		}
	};

$.fn.thumbSetup = function(img_array, i) {	
	//set tw/th if not set
	
	
	if(!tW||!tH) tW=$(this).parent().width();tH=$(this).parent().height();
	imgW = parseInt(img_array[i]['w']);
	imgH = parseInt(img_array[i]['h']);
	
	
	
	aR = aspectR(imgW, imgH);
	$(this).removeAttr('height').removeAttr('width');

	if (imgW > imgH)
	{
		$(this).height(tH).width(tH * aR);
		repos = parseInt("-"+(tW*(imgW / imgH)-tW) / 2);				
		$(this).css({left: repos});
				
	} else
	{	
		$(this).height(tW).height(tW * aR);
		repos = parseInt("-"+(tH*(imgH / imgW)-tH) / 2);	
		$(this).css({top: repos});
	}

}


$.fn.sequential = function(img_array, i){
	if(i<=img_array.length-1)
	{
		$(this).imageload(img_array, i);
	}
}

$.fn.imageload = function(img_array, i){
	if(i<=img_array.length-1)
	{
		selectedelement = $(this);
		$('<img />').attr('src', img_array[i]['s']);
		selectedelement.eq(i).append("<img src=\""+img_array[i]['s']+"\" alt=\"\" />");			
		selectedelement.imgCallback(img_array, i);
		selectedelement.eq(i).children('img').thumbSetup(img_array, i);
		selectedelement.eq(i).children('img').fadeIn();	
	}
}

$.fn.imgCallback = function (img_array, i){
	$('.thumbs span').eq(i).removeClass('tLoading');
	i++;
	$(this).sequential(img_array, i);
}
function aspectR(w,h){

	if(w > h)
	{
		a = w / h;
	}
	else
	{
		a = h / w;
	}
	return a;
	}

function percentage(p, f){o=f / 100;p=parseInt(p*o);return p;}

	$.fn.customFadeIn = function(speed, callback) {
		$(this).fadeIn(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	
function mCanimate(mCout, mCin, mCelement)
{
	mCelement.animate({
		opacity:1				  
	},mCtimout,function(){
		$(this).children('img').eq(mCout).fadeOut(mCanispeed);
		$(this).children('img').eq(mCin).fadeIn(mCanispeed);
		mCelement.MyCycle();
	});
}