/*////////////////////////////////////////////////////////////////////////////
// Author:				Nzime
// Client:				Rare Ltd
// Description: 		A box of magic tricks for transition effects, loading
//						flash content, gallery functionality.
// Version:				1.0
// Last Updated:		01/06/2010
// Required Includes:	jquery.min.js, jquery.easing.js, jquery.cycle.min.js,
//						jquery.swfobject.min.js, jquery.ui.min.js, jquery.jcarousel.min.js
*/////////////////////////////////////////////////////////////////////////////
$(document).ready(function()
{
	var base = '/';
	
	//make sure these bits happen quickly
	$("#game-nav-tab").html('');
	$(".hidden").hide();
	$(".noLabels label").hide();
	var homeFlashImage = $("#homeFlashImage").html();
	var homeFlashVideo = $("#homeFlashVideo").html();	
	
	//flash vars switch to load colour game nav
	if ($("#green").length!=0)
		{
			var color = 'green';	
		}
	if ($("#orange").length!=0)
		{
			var color = 'orange';	
		}
	if ($("#grey").length!=0)
		{
			var color = 'grey';	
		}
	if ($("#pink").length!=0)
		{
			var color = 'pink';	
		}
	if ($("#purple").length!=0)
		{
			var color = 'purple';	
		}
		
	//register ajax
	$('#regSubmit').click(function(){
		var name = $('#regFirstName').attr('value');
		var email = $('#regEmail').attr('value');
		
		
		$('#regFirstName').removeClass('errors');
		$('#regEmail').removeClass('errors');
		
		if (name == "First Name"){
			$("#regFirstName").addClass('errors');
			var eError = 1;
		}
		
		if (email == "Email Address"){
			$("#regEmail").addClass('errors');
			var eError = 1;
		}
		
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,6})$/;
		if(reg.test(email) == false) {
			$("#regEmail").addClass('errors');
			var eError = 1;
		}

		
		if (eError == 1){
			return false;	
		}
		
		var dataString = 'firstName='+ name + '&email=' + email; 
		$.ajax({  
			type: "GET",  
			url: "/ajax/register",  
			data: dataString,  
			success: function() {
				$('#poppupRegister form').hide();
				$('#poppupRegister').addClass('success');
			}
		});  
		return false;
	});
		
	//expander
	$('#gameDescription').expander({
	  slicePoint: 400, 
	  widow: 2,
	  expandPrefix: '',
	  expandEffect: 'fadeIn',
	  expandSpeed: 500,
	  expandText: 'Read More',
	  userCollapseText: 'Read Less'
	});
	
	//home page intro flash
	$('#introFlash').flash({swf:base+'content/flash/home_intro_panel.swf',height:269,width:810,flashvars: {
				source_file: homeFlashVideo,image_file: homeFlashImage,videoIntroPath: base+'content/flash/video_intro3.png',default_resize_mode: 'fillScreen',default_thumb_height: '80',prevent_xml_cache: 'false',auto_start: 'false'
			},params:{wmode:'transparent',scale: 'noScale',allowFullScreen: 'true'}});
	
	//remove the dodgy browser dotted line box from links
	$('a', this).click(function(){
		$(this).blur();
	});
	
	//load the game navigation
	//embed script and vars, params, attributes
	if(swfobject.hasFlashPlayerVersion("8.0.0")){
	
		$(".navGame").click(function(){
		if ($('#gameNavClose').is(':visible')){
			$('#game-nav-tab').html('');
			$('#introFlash').stop(true, true).show(1000);
			$("#gameNavClose").hide();
			return false;
		} else {
			var bgImgUrl = base+'templates/rare/swf/gamespanel'+color+'.png';
			img = new Image();
			img.src = bgImgUrl;
			$('#game-nav-tab').flash({swf: base+'templates/rare/swf/carousel.swf',height:175,width:785,flashvars: {
				dataPath: base+'templates/rare/swf/carousel.xml',carouselBackgroundPath: bgImgUrl},params:{wmode:'transparent'}});
			$('#introFlash').stop(true, true).hide();
			$("#gameNavClose").fadeIn(100);
			return false;
		}
	});
	
	} else {

		//Do nothing

		} 
	
	
	
	//toggle the register panel at the top of the page
	$("#registerToggle").toggle(function(){
		$("#poppupRegister").fadeIn(100);
		$("#register").addClass('active');
		return false;
	}, function(){
		$("#poppupRegister").fadeOut(100);
		$("#register").removeClass('active');
		return false;
	});
	
	/* Game Gallery JS */
	
	//game gallery navigation
	$('#gameGalleryNav').jcarousel({
		easing: 'easeInOutExpo',
		scroll: 1,
		auto: 0,
		animation: 500
	});
	
	//johns simple but magical game gallery function
	$('#gameGalleryNav a').click(function(){
		var href = $(this).attr('href');
		var detail = $('#gameGalleryImage img');
		img = new Image();
		img.src = href;
		if (detail.attr('src')!=href)
			{
				$('#gameGalleryNav a').fadeTo(0, 1);
				$(this).fadeTo(0, 0.7);
				detail.stop(true, true).fadeOut(500);
				detail.attr('src', href);
				detail.stop(true, true).fadeIn(500);
			}
		return false;
	});
	
	$("#gameGalleryNav a").each(function() {
		img = new Image();
		img.src = $(this).attr('href');
	});

	
	/* Game Gallery JS End */
	
	//home page sliding panels carousel
	$('#slide').jcarousel({
		easing: 'easeInOutExpo',
		scroll: 1,
		auto: 5,
		animation: 500,
		wrap: 'circular'
	});
	
	$('.tweet').cycle({
		fx: 'fade',
		pause: 1,
		delay: 0,
		timeout: 6000
	});
	
	$('#companyIntro').cycle({
		fx: 'fade',
		pause: 1,
		delay: 0,
		timeout: 6000
	});
	
	
	$(".accordion").accordion({
		active: false,
		autoHeight: false,
		collapsible: true
	});
	
	
	/* News zoom */
	$().piroBox({
		my_speed: 300, //animation speed
		bg_alpha: 0.5, //background opacity
		slideShow : 'false', // true == slideshow on, false == slideshow off
		close_all : '.piro_close' // add class .piro_overlay(with comma)if you want overlay click close piroBox
	});

	
	//external link
	$('a[rel="external"]').click( function() {
        window.open( $(this).attr('href') );
        return false;
	});
	
	//form input handeling hidden labled	
	$(".noLabels input[type='text']").focus(function() {
		if ($(this).attr("value") == $(this).attr("rel")){
			$(this).attr("value", "");
		}
	});
	
	$(".noLabels input[type='text']").blur(function() {
		if ($(this).attr("value") == "")
			{
				var val = $(this).attr("rel");
				$(this).attr("value", val);
			}
	});
	
	$(".noLabels input[type='text']").load(function() {
		if ($(this).attr("value") == "")
			{
				var val = $(this).attr("rel");
				$(this).attr("value", val);
			}
	});
	
	$(".noLabels textarea").focus(function() {
		$(this).text("");
	});
	
	//password field handeling hidden lables
	$(".password input[type='password']").hide();
	$(".password input[type='text']").show();
	
	$(".password input[type='text']").focus(function(){
		$(this).hide();
		$(this).next("input[type='password']").show().focus();
	});
	
	$(".password input[type='password']").blur(function(){
		if ($(this).attr("value") == "")
			{
				$(this).hide();
				var pass = $(this).prev("input[type='text']").show();
				var value = pass.attr("rel");
				pass.attr("value", value)
			}
	});
	
});