// JavaScript Document

$(document).ready(function(){
	$('#mask').fadeOut("fast");
	$('#mask').fadeTo("fast",0.8);
	$('#mask').click(function(){
		if(isForm)
		{
			hideForm();
		}
		else
		{
			hideCard();
		}
	});
	//
	if($(window).width() < $('#cards_inner').width())
	{
		$('#arrows').fadeIn('fast');
	}
	else
	{
		$('#arrows').fadeOut('fast');
	}
	//
	MM_preloadImages('images/window_form_bg.png','images/formBg.png','images/window_card_bg1.png','images/window_card_bg2.png','images/window_card_bg3.png');
});

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

/////////////////////////////////////////////////
// CARDS
function showMaskCard(card)
{
	nextCard = card;
	//
	$(document).ready(function(){ 
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();  
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		$('#mask').fadeIn(600,onMaskCard);
	});
}

function onMaskCard()
{
	$(document).ready(function(){  
		var winH = $(window).height();   
		var winW = $(window).width();  
		//
		if(lastCard>0)
		{
			document.getElementById('content'+lastCard).style.display='none';
		}
		document.getElementById('content'+nextCard).style.display='block';
		lastCard = nextCard;
		//
		$('#windowCardBg').fadeIn("slow");
		$('#windowCard').fadeIn("slow");
		//
		var textHeight = document.getElementById('contentText'+nextCard).clientHeight;
		if(textHeight < 36)
		{
			textHeight=36;
		}
		//
		$('#windowCardBg').css('height', textHeight + 362);
		$('#windowCardBg2').css('height', textHeight + 362 - 398); 
		//
		$('#windowCardBg').css('top', (winH-$('#windowCard').height())/2);   
		$('#windowCardBg').css('left', (winW-$('#windowCard').width())/2); 
		//
		$('#windowCard').css('top', (winH-$('#windowCard').height())/2);   
		$('#windowCard').css('left', (winW-$('#windowCard').width())/2); 
		//
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();  
		$('#mask').css({'width':maskWidth,'height':maskHeight});
	});
}

function hideCard()
{
	$(document).ready(function(){  
		$('#windowCardBg').fadeOut(600);
		$('#windowCard').fadeOut(600,onCardHidden);
	});
}

function onCardHidden()
{
	$(document).ready(function(){  
		$('#mask').fadeOut(600);
	});
	//
	if(lastCard>0)
	{
		document.getElementById('content'+lastCard).style.display='none';
	}
	lastCard=0;
}

function moveCard(d)
{
	var cur=0;
	for(var i=0; i<idArray.length; i++)
	{
		if(idArray[i] == lastCard.toString())
		{
			cur=i;
			break;
		}
	}
	cur += d;
	if(cur<0)
	{
		cur = idArray.length-1;
	}
	else if(cur>idArray.length-1)
	{
		cur = 0;
	}
	//
	nextCard = idArray[cur];
	//
	$(document).ready(function(){  
		$('#windowCardBg').fadeOut(600);
		$('#windowCard').fadeOut(600,onMaskCard);
	});
}

/////////////////////////////////////////////////
// FORM
function showMaskForm()
{
	isForm=true;
	$(document).ready(function(){ 
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();  
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		$('#mask').fadeIn(600,onMaskForm);
	});
}

function onMaskForm()
{
	$(document).ready(function(){  
		var winH = $(window).height();   
		var winW = $(window).width();  
		//
		$('#windowForm').fadeIn("slow");
		//
		if($('#windowForm').height() > winH)
		{
			$('#windowForm').css('top', ($(document).height()-$('#windowForm').height())/2);
		}
		else
		{
			$('#windowForm').css('top', (winH-$('#windowForm').height())/2);
		}
		$('#windowForm').css('left', (winW-$('#windowForm').width())/2);
		//
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();  
		$('#mask').css({'width':maskWidth,'height':maskHeight});
	});
}

function hideForm()
{
	$(document).ready(function(){
		$('#windowForm').fadeOut(600,onFormHidden);
	});
}

function onFormHidden()
{
	$(document).ready(function(){  
		$('#mask').fadeOut(600);
	});
	isForm=false;
	//
	var ffm = document.getElementById('formFrame');
	ffm.src = 'form.asp';
}

/////////////////////////////////////////////////
// SLIDE
function moveSlide(dir)
{
	if(cardsWidth > $(window).width())
	{
		endPos = endPos + (dir*-148)
		if(endPos > 0)
		{
			endPos = 0;
		}
		else if(endPos < $(window).width() - cardsWidth)
		{
			endPos = $(window).width() - cardsWidth;
		}
		//
		$(document).ready(function(){
			$("#cards_inner").animate({ 
				left: endPos
			}, 600 );
		});
	
	}
	else
	{
		endPos=0;
		//
		$(document).ready(function(){
			$("#cards_inner").animate({ 
				left: endPos
			}, 600 );
		});
	}
}

/////////////////////////////////////////////////
// RESIZE
function doResize()
{
	$(document).ready(function(){  
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();  
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		//
		var winH = $(window).height();   
		var winW = $(window).width(); 
		//
		if(lastCard!=0)
		{
			var textHeight = document.getElementById('contentText'+lastCard).clientHeight;
			if(textHeight < 36)
			{
				textHeight=36;
			}
			//
			$('#windowCardBg').css('height', textHeight + 362);
			$('#windowCardBg2').css('height', textHeight + 362 - 398);
			//
			$('#windowCardBg').css('top', (winH-$('#windowCard').height())/2);
			$('#windowCardBg').css('left', (winW-$('#windowCard').width())/2);
			//
			$('#windowCard').css('top', (winH-$('#windowCard').height())/2);
			$('#windowCard').css('left', (winW-$('#windowCard').width())/2);
		}
		//------------
		if($(window).width() < $('#cards_inner').width())
		{
			$('#arrows').fadeIn('fast');
		}
		else
		{
			$('#arrows').fadeOut('fast');
		}
	});
	//
	moveSlide(0);
}
