/*
Satski jQuery functions.
Websir Ltd www.websir.co.uk
Updated 22nd Feb 2010
*/
var newsSliderSpeed=800;
var newsSliderEasing="easeInOutExpo";
var newsSliderSelectIndex=0;
var newsSliderAniLeft="270px";
var ShowcaseIndex=0;
var mCtimout=4000;
var mCanispeed=800;
var Mci=0;
var ShowcaseMid=0;
var HeadImgSelect=0;
var tic;
var tic_i;

var p_c;
var p_l;
var product;
var captions;


$(document).ready(function(){
						   
	if($.browser.msie)
	{
		$('#buyform .txtbox').css({width:'auto'});
	}

	$('.iphoneBuy, .blackberryBuy').click(function(){
		return false;
	});					   
						   
	tic = $('#top_img_container');
	tic_i = $('#top_img_container img');
						   
	//HOME PAGE
	if(pgeIndex==1)
	{
		$('#quotebox').children('img').hide().eq(0).show();
		
		ShowcaseMid=$('.showcase_container').height()/2;
		$('.showcase_growbox').css({
		height:0,width:0,top:ShowcaseMid}).corner("10px").children('.showcase_section').hide().parent();
	}
	
	//NEWS PAGE
	if(pgeIndex==4)
	{
		$('.newscontainer').html("<p>Refreshing news items...</p>");
	}
	
	//PRODUCT PAGES
	if(pgeIndex==2)
	{
		$('.prodcontent').hide().eq(0).show();
		
		product = $('.pi_thumbs img');
		captions = $('.ss_caption p');
		if(product.length>0){
			p_c=0; p_l=product.length-1;
			product.hide().eq(p_c).show();
			captions.hide().eq(p_c).show();
			if(product.size()>1){
				$().everyTime(5000, function(){
					if(p_c==p_l) p_c=0; else p_c++; 
					product.fadeOut().eq(p_c).fadeIn();
					captions.fadeOut().eq(p_c).fadeIn();
				});
			}
		}
		
		try{
			$('.iPhBbBan').corner('5px cc:#efefef');	
		}
		catch(err){
			
		}
		
	}
	
	//DOWNLOADS
	if(pgeIndex==8 || pgeIndex==2)
	{
		$.post("scripts/php/dl.php",{bfunc:"returnlist"},function(data){
			$('.freedownloadselect').html(data);	
		});
		
		$('.freedownloadselect').change(function(){
			thisselectindex=$(this).val();
			if(thisselectindex>0)
			{
				$.post("scripts/php/dl.php",{bfunc:"returbdl",listindex:thisselectindex},function(data){
					linksplit=data.split("(dl)");
					
					if(linksplit[0]=="#"){$('.mapdownloadcontainer').hide();$('.appUn').show();}
					else{
						$('.mapdownloadcontainer').show();
						$('.appUn').hide();
						$('.dl').attr('href',"mapdownloads/pc/"+linksplit[1]).html("<strong>Download</strong> "+linksplit[2]);
						$('.pda').attr('href',"mapdownloads/mobile/"+linksplit[0]).text(linksplit[2]);
					}

				});
			}
			else
			{
				$('.mapdownloadcontainer').hide();
				$('#dlbutton_mobile').children('.arrow').attr('href',"#");
				$('#dlbutton_pc').children('.arrow').attr('href',"#");	
			}
		});
	}
	
	//FAQ PAGE
	if(pgeIndex==3)
	{
		$('.faq_content').hide().eq(0).show();
	}
	
	//PRODUCT PAGES - //FAQ PAGE
	if(pgeIndex==2 || pgeIndex==3)
	{
		
		$('.productnav li').eq(0).css({backgroundPosition:"0px -66px"}).children('a').css({color:"#fff"});
		
		
		$('.productnav li').mouseover(function(){
			if($(this).css("backgroundPosition")!="0px -66px"){
				$(this).css({backgroundPosition:"0px -33px"});
			}
		});
		
		$('.productnav li').mouseout(function(){
			if($(this).css("backgroundPosition")!="0px -66px"){$(this).css({backgroundPosition:"0px 0px"});}			   
		});
		
		$('.productnav li a').click(function(){
			selectindex=$('.productnav li a').index(this);
			$('.prodcontent').hide().eq(selectindex).show();
			$('.faq_content').hide().eq(selectindex).show();
			$('.productnav li').css("backgroundPosition","0px 0px").children('a').css("color","#666666");
			$('.productnav li').eq(selectindex).css("backgroundPosition","0px -66px").children('a').css("color","#fff");
			
			//FAQ
			$('.faq_content').eq(selectindex).children().children('.daqTabInfo')
			.css({display:'none'}).eq(0).css({display:'block'});
			
			$('.faq_content').eq(selectindex).children().children('.faqTabLst')
			.children('li').children('a').removeClass('tabSelect').eq(0).addClass('tabSelect');

			
			return false;
		});
		
		
		
		$('.product_radiobutton').click(function(){
			optionindex=$('.product_radiobutton').index(this);
			if(optionindex==2){$('.dropbox').eq(0).hide();}
			else{$('.dropbox').eq(0).show();}	
		});
	}
	
	//CONTACT PAGE
	if(pgeIndex==5)
	{
		$(".bluebox").draggable();
	
		$(".redbox").droppable({
			drop: function(event, ui) {
				
				$(this).css("backgroundPosition","bottom");
				$(".bluebox").hide();
				$('#contactreturn').text("Processing...").show();
				
				t1=$('.txtbox').eq(0).val();
				t2=$('.txtbox').eq(1).val(); //email 1
				t3=$('.txtbox').eq(2).val(); //email 2
				t4=$('.txtbox').eq(3).val();
				t5=$('.txtbox').eq(4).val();
				t6=$('#contactform .theHf').val();
				
				
				if(CheckEmail(t2) && t1!="" && t2==t3 && t6==""){
					$.post("scripts/php/contact.php",{f1:t1,f2:t2,f3:t3,f4:t4,f5:t5},function(data){
						$('#contactreturn').AnimateMessage(data);
						$('.txtbox').val("");
					});
					
				}
				else{
					$('#contactreturn').AnimateMessage("Please enter your name and a valid email address.");
					$(".bluebox").css({"top":83,"left":154}).show();
					$(this).css("backgroundPosition","top");
				}
			}
		});
	}
	
	
	//ALL PAGES
	
	tic_i.animate({opacity:0},{duration:0});
	
	$('a').click(function(){
		if($(this).attr('rel')=="nw"){window.open($(this).attr('href'));}
		else if($(this).attr('href')!="#" && $(this).attr('href')!="" ){window.location=$(this).attr('href');}
		return false;
	});
	
   
  //LOAD FLASH

	$('.googleearthvid').flash({ src: 'vids/google.swf',width: 385,height: 227,wmode: 'transparent' },{ version: 8 });
	$('.skiviewdemo').flash({ src: 'vids/satski.swf',width: 385,height: 227,wmode: 'transparent' },{ version: 8 });
	
	$('.showcase_section').eq(0).flash({ src: 'vids/Gadget_Show2.swf',width: 440,height: 260,wmode: 'transparent'},{ version: 8 });
	//$('.showcase_section').eq(1).flash({ src: 'vids/google.swf',width: 440,height: 260,wmode: 'transparent' },{ version: 8 });
	
	$('.showcase_section').eq(2).flash({ src: 'vids/Gadget_Show.swf',width: 440,height: 260,wmode: 'transparent'},{ version: 8 });
	$('.showcase_section').eq(3).flash({ src: 'vids/satski.swf',width: 440,height: 260,wmode: 'transparent' },{ version: 8 });
	
	//$('.showcase_section').eq(4).flash({ src: 'vids/sif.swf',width: 440,height: 260,wmode: 'transparent' },{ version: 8 });
	
	
	//$('.productgoogle3d').flash({ src: 'vids/google.swf',width: 430,height: 250 },{ version: 8 });
	//$('.product_skidemo').flash({ src: 'vids/satski.swf',width: 430,height: 250 },{ version: 8 });
	
	if(pgeIndex==8)
	{
		$('.monitor .innercon').eq(0).flash({ src: 'vids/google285.swf',width: 285,height: 157 },{ version: 8 });
	}
	else if(pgeIndex==0)
	{
		<!--$('.monitor .innercon').eq(1).flash({ src: 'vids/google285.swf',width: 285,height: 157 },{ version: 8 });-->
		$('.monitor .innercon').eq(0).flash({ src: 'vids/satski285.swf',width: 285,height: 157 },{ version: 8 });
		
	}
	else{
		$('.geVid').flash({ src: 'vids/google285.swf',width: 285,height: 157 },{ version: 8 });	
	}
	
	
  //LOAD DB
  
  if(pgeIndex==1)
  {	
	$.post("scripts/php/news.php",{funcrequest: "newsslider"},function(data){
		$('#hp_newsticker_inner').html(data);
		$('.hp_newsticker_sec').css("left",newsSliderAniLeft).eq(0).css("left","0px");
	});  
  }
	
	
	//FORM SUBMITS	
	$('#contactform').submit(function(){
		return false;								  
	});
	
	$('#baf').submit(function(){
		thisVal=$('#baf .txtbox').val()
		if(CheckEmail(thisVal))
		{
			$.post("scripts/php/baf.php",{emailadd:thisVal,site:'SS'},function(data){
				$('#contactreturn').AnimateMessage(data);					  
			});
		}
		else
		{
			$('#contactreturn').AnimateMessage("Please enter a valid email address.");
		}
		return false;
	});
	
	//popup
	$('.popC').css({zIndex:100}).children('.mask').animate({opacity:0},{duration:0,queue:false})
	.parent().children('.diag').corner('5px cc:#303030').centreElm();
	
	$(window).bind("resize",function(){
		$('.popC .diag').centreElm();		   
	});

        $(document).keyup(function(event){
            if (event.keyCode == 27) {
                $('.popC').children('.diag').css({display:'none'}).parent().children('.mask')
                .animate({opacity:0},300,function(){
                    $(this).parent().css({display:'none'});
                });
            }
        });

        $('.popC .diag .closePop').click(function(){
                $('.popC').children('.diag').css({display:'none'}).parent().children('.mask')
                .animate({opacity:0},300,function(){
                    $(this).parent().css({display:'none'});
                });
                return false;
        });
        
	
	//PAGE LOADED
	$(window).bind("load",function(){

            try{
				if(ProductIndex==3){
					$('.popC').css({display:'block'}).children('.mask').animate({opacity:.8},{duration:300,queue:false});
					$('.popC .diag').centreElm();
				}
			}
			catch(err){}

		$('#top_img_container').height($('#top_img_container').children('img').eq(0).height());
		$('#top_img_container').children('img').eq(0).fadeIn(200);
		
		$('.navlist li a').mouseover(function(){
			if($('.navlist li a').index(this)==5){$('#pop_products').fadeIn(300);}
			else{$('#pop_products').fadeOut(300);}
		});
		$('.navlist li a').mouseout(function(){
			if($('.navlist li a').index(this)!=5){$('#pop_products').fadeOut(300);}
		});
		
		
		
		$('.faq_prodinfo').children('.faq_content').children('h3').click(function(){
			selectindex=$('.faq_prodinfo').children('.faq_content').children('h3').index(this);
			$('.faq_prodinfo').children('.faq_content').children('.faqanswer').eq(selectindex).slideToggle(300);																	
		});
		
		
		/* FAQ TABS*/
		$('.daqTabInfo').eq(0).css({display:'block'});
		
		$('.faqTabLst li a').click(function(){
			thsIndex=$(this).parent().parent().children().children().index(this);
			
			$(this).parent().siblings('li').children('a').removeClass('tabSelect');
			$(this).addClass('tabSelect');
			
			$(this).parent().parent().parent().children('.daqTabInfo').css({display:'none'})
			.eq(thsIndex).css({display:'block'});
			
			return false;
		});
		
		$('.daqTabInfo h3').click(function(){
			selectindex=$('.daqTabInfo h3').index(this);
			$('.daqTabInfo .faqanswer').eq(selectindex).slideToggle(300);
		});
		
		
		$('.showcase_growbox').animate({width:444,height:280,top:0},600, newsSliderEasing,function(){
		$(this).children('.showcase_section').eq(0).fadeIn(300);	
		});
		
		//HEAD IMAGES CHANGE
		/*
		$('.arrow').hover(function(){
			ti=$('.arrow').index(this)+1;
				tic_i.animate({opacity:0},{queue:false,duration:300}).eq(ti).animate({opacity:1},{queue:false,duration:500});	
		},function(){
				tic_i.animate({opacity:0},{queue:false,duration:300}).eq(0).animate({opacity:1},{queue:false,duration:500});
		});
		*/
		
		//end img changer
		
		/*if(pgeIndex==2)
		{
			$('.product_imgsection .pi_thumbs img').hide().eq(0).fadeIn(300);
			$('.product_imgsection .ss_caption p').hide().eq(0).fadeIn(300);
			
			$('.product_imgsection .pi_thumbs').MyCycle();
			
			if(ProductIndex==1 || ProductIndex==3){}
		}
		*/
		if(pgeIndex==4)
		{
			$.post("scripts/php/news.php",{funcrequest:"newsfull"},function(data){
				$('.newscontainer').html(data);
				$('.newsitemcontent').hide();
				$('.newscontainer h3').click(function(){
					$(this).parent().children('.newsitemcontent').slideToggle(300);
				});
			});
		}
		
		LoadVals();
	});
	   
});


//jQuery Functions
$(function(){
		 
	$.fn.ShwcaseAnimate = function(scDirection,callback){
		scSection=$(this).children('.showcase_section')
		scCount=scSection.size()-1;
		scSection.eq(ShowcaseIndex).fadeOut(300,function(){
			ShowcaseBoxAni(function(){
				scNextShow=ReturnNextInLine(scCount,scDirection,ShowcaseIndex);
				scSection.eq(ShowcaseIndex).fadeOut(300);scSection.eq(scNextShow).fadeIn(300,function(){
					ShowcaseIndex=scNextShow;
					if(callback != undefined){callback();}
				});
			});											 
		});
	};
	
	$.fn.NewsSlider = function(sideselect, callback){
		elementcount=$(this).children('.hp_newsticker_sec').size()-1;
		if(sideselect=="left"){nStartPos="-"+newsSliderAniLeft;nAniOutPos=newsSliderAniLeft;}
		else{nStartPos=newsSliderAniLeft;nAniOutPos="-"+newsSliderAniLeft;}
		if(elementcount>0)
		{
			$(this).children('.hp_newsticker_sec').css("left",nStartPos).eq(newsSliderSelectIndex).css("left","0px");
			nextAnimate=ReturnNextInLine(elementcount, sideselect, newsSliderSelectIndex);
			NewsSliderAni(newsSliderSelectIndex, nextAnimate, $(this), nAniOutPos, '.hp_newsticker_sec', function(){
				if(callback != undefined){callback();}																								
			});
			newsSliderSelectIndex=nextAnimate;
		}
	};
	
	$.fn.MyCycle = function(){
		imgcount=$(this).children('img').size()-1;
		if(imgcount>0)
		{
			if(Mci==imgcount){mCanimate(Mci,0,$(this));Mci=0;}
			else{mCanimate(Mci,Mci+1,$(this));Mci++;}	
		}
	};
	
	$.fn.AnimateMessage = function(returnmessage){
		$(this).text(returnmessage).fadeIn(300,function(){
			$(this).animate({opacity:1},3000,function(){$(this).fadeOut(300);});
		});
	};
	
});

(function($) {
    $.fn.extend({
        centreElm:function(options){
            var defaults = {easing:'easeInOutExpo',aniSpeed:300};
            var options = $.extend(defaults, options);
            return this.each(function() {
                var aniType=options.easing;
                var timeout=options.aniSpeed;
                var thisElmName=$(this);
                var thisElmHeight=thisElmName.height();
                var thisElmWidth=thisElmName.width();
                var windHeight=$(window).height();
                var windWidth=$(window).width();
                thisElmName.animate({top:((windHeight/2)-(thisElmHeight/2)),left:((windWidth/2)-(thisElmWidth/2))},{duration:timeout,easing:aniType,queue:false});
            });
        }
    });
})(jQuery);


//Standard JS Functions

function ShowcaseInter()
{
	$('.showcase_container').animate({opacity:1},5000,function(){
		$('.showcase_growbox').ShwcaseAnimate("left");
		ShowcaseInter();
	});
}

function ShowcaseBoxAni(callback)
{	
	$('.showcase_growbox').animate({width:0,height:0,top:ShowcaseMid
	},600,newsSliderEasing,function(){
		$(this).animate({width:444,height:280,top:0		
		},600,newsSliderEasing,function(){
			if(callback != undefined){callback();}
		});
	});	
}

function NewsSliderAni(wutuOut, wuduIn, wuduElement, AniLeft, wuduSectionElement, callback)
{	
	wuduElement.children(wuduSectionElement).eq(wuduIn).animate({
	"left":"0px"},{duration: newsSliderSpeed, queue: false, "easing": newsSliderEasing});
	
	wuduElement.children(wuduSectionElement).eq(wutuOut).animate({
	"left":AniLeft},newsSliderSpeed, newsSliderEasing, function(){
		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();
	});
}

function ReturnNextInLine(elementlimit, AnimateDirection, currentIndex)
{	
	if(AnimateDirection=="left")
	{if(currentIndex==0){return elementlimit;}else{return currentIndex-1;}}
	else{if(currentIndex==elementlimit){return 0;}else{return currentIndex+1;}}
}

function NewsTicker()
{
	$('#hp_newsticker_inner').animate({
		opacity:1					  
	},mCtimout,function(){
		$(this).NewsSlider("right",function(){
			NewsTicker();								
		});	
	});
}

function LoadVals()
{
	$('.np_news_arrowleft').click(function(){
		$('#hp_newsticker_inner').stop().NewsSlider("left",function(){
			NewsTicker();														
		});
		return false;
	});
						
	$('.np_news_arrowright').click(function(){
		$('#hp_newsticker_inner').stop().NewsSlider("right", function(){
			NewsTicker();															  
		});
		return false;
	});
	
	$('.showcasearrowleft').click(function(){
		$('.showcase_container').stop();
		$('.showcase_growbox').ShwcaseAnimate("left",function(){
			//ShowcaseInter();														 
		});
		
		return false;								   
	});
	
	$('.showcasearrowright').click(function(){
		$('.showcase_container').stop();
		$('.showcase_growbox').ShwcaseAnimate("right",function(){
			//ShowcaseInter();												   
		});
		return false;								   
	});
	
	//$('#quotebox').MyCycle();
	NewsTicker();
}

function CheckEmail(inputemail) 
{
	AtPos = inputemail.indexOf("@");
	StopPos = inputemail.lastIndexOf(".");
	if (AtPos == -1 || StopPos == -1){return false;}
	else{return true;}
}
