// JavaScript Document

function pageSwitch() {
	
	var pHeight = new Array();
	var curPage = 0;
	var curMargin = 0;
	
	$("#navie a.switch").each(
		function( intIndex ){
			$(this).attr('rel','item-'+intIndex);
		}
	);
	
	$("#toSwitch li.ps_page").each(
		function( intIndex ){
			$(this).attr('id','item-'+intIndex);
		}
	);

	$("#toSwitch").attr('style','overflow:hidden; height:' + $("#item-0").height() + 'px; margin:0 0 0 0' )
	
	$("#navie a.switch").click( function() {
		var thisPage = $(this).attr('rel').substr(5, 1);
		var curHeight = $('#item-'+curPage).height()+5;
		var thisHeight = $('#item-'+thisPage).height()+5;
		if( curPage!=thisPage ) {
			if( thisPage==0 ) {
				$("#toSwitch").height( $("#item-"+thisPage).height() );
				$("#toSwitch .video").attr('style','display:none;');
				$("#toSwitch li:first").animate( {"marginTop": 0 + "px"},700 );
				$("#toSwitch .video").attr('style','display:block;');
			} else {
				var neg = -1;
				var temp = 0;
				$("#toSwitch li.ps_page").each(
					function( intIndex ){
						var x = $(this).attr('id').substr(5, 1);
						if( (thisPage>x) ) {
							temp = temp+$("#item-"+x).height()+50;
						}
					}
				);
				curMargin = temp*neg+5;
				$("#toSwitch").height( $("#item-"+thisPage).height() );
				$("#toSwitch .video").attr('style','display:none;');
				$("#toSwitch li:first").animate( {"marginTop": (curMargin) + "px"},700 );
				$("#toSwitch .video").attr('style','display:block;');
			}
			curPage = thisPage;
		}
	});
}

function pollSubmit() {
	$("#pollSubmit").livequery('click',function() {
		$('#pollContent, #pollSubmit').attr('style','display:none');
		var data = 'answer='+$('input[name=answer]:checked').val();
		data = data + '&id='+$('#pollId').val();
		$.get("poll.php?a=2&"+data, function(data){
			$('#pollContent').html(data);
		});
		$('#pollContent').attr('style','display:block');
	});
}

function getSponsors() {
	if( $('#midsponsor').length > 0 ) {
		var adList = new Array();
		adList[0] = '<a href="http://www.lorealparisexclusif.ph/"><img src="images/sponsors/mid/loreal.gif" alt="sponsor" /></a>';
		adList[1] = '<a href="http://www.lorealprofessionnel.com/"><img src="images/sponsors/mid/lorealpro.jpg" alt="sponsor" /></a>';
		adList[2] = '<a href="http://www.bli.com/"><img src="images/sponsors/mid/rayban.jpg" alt="sponsor" /></a>';
		adList[3] = '<a href="http://www.michelisjewellery.com/"><img src="images/sponsors/mid/michelis.jpg" alt="sponsor" /></a>';
		
		if( adList.length>0 )  {
			var theAd = Math.floor(Math.random()*adList.length);
			theAd = '<center>' + adList[theAd] + '</center>';
			$('#midsponsor').html(theAd);
		}
	}

	if( $('#rightsponsor').length > 0 ) {
		var adList = new Array();
		adList[0] = '<a href="http://ponds.com.ph"><img src="images/sponsors/right/prp-ponds.jpg" alt="sponsor" /></a>';
		
		if( adList.length>0 )  {
			var theAd = Math.floor(Math.random()*adList.length);
			theAd = '<center>' + adList[theAd] + '</center>';
			$('#rightsponsor').html(theAd);
		}
	}
}

function fanfaveError(msg) {
	$("#facebox .ffmessage").html(msg).addClass('error').removeClass('success').removeClass('hide');
}

function fanfave() {
	// default values
	var theWidth = 500;
	var theHeight = 500;
	var ContainerWidth = 0;
	var currentPage = 0;
	var neg = -1;
	
	// generate form style
	$("#fanfave").attr('style','width:'+theWidth+'px;overflow:hidden;position:relative;');
	$("#fanfave ul li").each(function( intIndex ){
		$(this).attr('style','width:'+theWidth+'px;height:'+theHeight+'px;float:left;position:relative;')
		.append('<div class="ffbtnwrapper" style="text-align:right;position:absolute;bottom:0;right:0;"><img src="images/back.jpg" alt="back" class="ffback" style="cursor:pointer" /><img src="images/next.jpg" alt="next" class="ffnext" style="cursor:pointer" /></div>');
		ContainerWidth = ContainerWidth + theWidth;
	});
	$("#fanfave ul li:first-child .ffback").attr('style','cursor:pointer;display:none;');
	$("#fanfave ul li:last-child .ffnext").attr('style','cursor:pointer;display:none;');
	$("#fanfave ul li:last-child .ffbtnwrapper").append('<img src="images/ffsubmit.jpg" alt="next" class="ffsubmit" style="cursor:pointer" />')
	$("#fanfave ul").attr('style','width:'+ContainerWidth+'px;margin:'+(currentPage*theWidth)+';');
		
	// generate events
	$(".ffnext").livequery('click',function() {
		currentPage++;
		$("#fanfave ul").animate( {"marginLeft": ((currentPage*theWidth)*neg) + "px"},300 );								
	});

	$(".ffback").livequery('click',function() {
		currentPage--;
		$("#fanfave ul").stop().animate( {"marginLeft": ((currentPage*theWidth)*neg) + "px"},300 );	
		$(".ffmessage").addClass('hide');
	});
	
	$("#fanfave .designer").livequery('click',function() {
		$("#fanfave .designer").removeClass('selected');
		$(this).addClass('selected');
		$("#chosenOne").val($(this).children('img').attr('title'));
	});
	
	// submit function
	$("#facebox .ffsubmit").livequery('click',function() {
		$(".ffmessage").addClass('hide');
		// validate
		var chosenOne = $("#chosenOne").val();
		var why = $("#facebox .ffwhy").val();
		var yourName = $("#facebox .ffname").val();
		var yourAddress = $("#facebox .ffaddress").val();
		var yourContact = $("#facebox .ffcontact").val();
		var yourEmail = $("#facebox .ffemail").val();
		var yourAge = $("#facebox .ffage").val();
		var yourGender = $("#facebox .ffgender").val();
		
		if( chosenOne=='none' ) {
			fanfaveError('Error found: Please choose your favorite designer');
		} else if( why=='' ) {
			fanfaveError('Error found: Please tell us why do you like '+chosenOne);
		} else if( yourName=='' || yourAddress=='' || yourContact=='' || yourEmail=='' ) {
			fanfaveError('Error found: Please input the required datas above');
		// no errors found. perform submit		
		} else {
			$("#facebox #fanfave").remove();
			$("#facebox .content").html('<div class="ffmessage success" style="width:'+theWidth+'px;"><center>Please wait...<br/><img src="images/loading.gif" /></center></div>');
			var data = "designer=" + chosenOne
					 + "&why=" + why
					 + "&name=" + yourName
					 + "&address=" + yourAddress
					 + "&contact=" + yourContact
					 + "&email=" + yourEmail
					 + "&age=" + yourAge
					 + "&gender=" + yourGender;
			$.get("process/fanfaveSubmit.php?"+data, function(data){
				$("#facebox .ffmessage").html(data);
			});
		}
	});
}

$(document).ready(function(){
	//pageSwitch()	
	$('a[rel*=facebox]').facebox();
	
	//pollSubmit();
	getSponsors();
	
	/*if( $('#fanfave').length > 0 ) {
		fanfave();
	}*/
	
	$('#sideImage').innerfade({
		speed: 2000,
		timeout: 7000,
		type: 'sequence',
		containerheight: '504px',
		transition_interval: 10000
	});

	
	if( $('#designers').length > 0 ) {
		$('#designers').jcarousel({
			scroll: 4
		});
	}
	
	if( $('#gallery').length > 0 ) {
		$('#gallery').galleryView({
			panel_width: 460,
			panel_height: 306,
			frame_width: 63,
			frame_height: 63,
			nav_theme: 'light'
		});
	}
	
	if( $('.slider').length > 0 ) {
		$('.slider').jcarousel({
			scroll: 4
		});
	}
	
	/*$.get("poll.php?id=7", function(data){
		$('#thePoll').html(data);
	});*/
});