ajaxLoaded = false;
$(document).ready(function() {
	//load Premium Data first
	getPremiumData($('.on_off :checkbox'));
	
	//Animate Headline Text
	//fadeHeadliners();
	
	//initialize switcher
	var onchange_checkbox = $('#sliderCheckbox input:checkbox').iphoneStyle({ 
		checkedLabel	: 'NBUV', 
		uncheckedLabel	: 'BUV',
		resizeContainer	: false
	});
	
	//set Style to slider	
	function sliderStyles(){
		var status = onchange_checkbox.is(':checked');	
		if(typeof(HomeContent) === 'undefined') {
			HomeContent = {};
		}
		if(status){
			$('.textLeft').css({'color':'#989898'});
			$('.textRight').css({'color':'#0078BB'});
			$('#sliderCheckbox .iPhoneCheckHandleCenter').css({'background-position':'0px -31px'});
			$('.labelText p').html(HomeContent.textNBUV);
		} else {
			$('.textLeft').css({'color':'#0078BB'});					
			$('.textRight').css({'color':'#989898'});
			$('#sliderCheckbox .iPhoneCheckHandleCenter').css({'background-position':'0px 0px'});
			$('.labelText p').html(HomeContent.textBUV);
		}
	}	
	
	//get status on change and change color and arrow position
	sliderStyles();
	var dataValue= '-';
	onchange_checkbox.change(function() {
		sliderStyles();
		if($('#slbBranche option:selected').val()!=='blank'){
			if($('#on_off').is(':checked')){
				dataValue= $('#slbBranche option:selected').data('NBUV_Value')==undefined ? '0' : $('#slbBranche option:selected').data('NBUV_Value');
			}
			else{
				dataValue= $('#slbBranche option:selected').data('BUV_Value')==undefined ? '0' : $('#slbBranche option:selected').data('BUV_Value');
			}	
			slotAnimation(dataValue);
			$('.resultText').hide();
			$('#contentContainer ul li.checklist').css({"visibility":"hidden"});
			$('#contentContainer p.homeStartText1').css({"visibility":"hidden"});
			$('#contentContainer p.homeStartText2').css({"visibility":"hidden"});
		}
	});
	
	
	
	//make animation on select box
	$('#slbBranche').change(function(){
		if($('#slbBranche option:selected').val()!=='blank'){
			var optionText		= $("#slbBranche option:selected").text();
			var dataLinkText= '';
			var dataValue= 0;
			if($('#on_off').is(':checked')){
				dataLinkText= $('#slbBranche option:selected').data('NBUV_Link')==undefined ? '' : $('#slbBranche option:selected').data('NBUV_Link');
				dataValue= $('#slbBranche option:selected').data('NBUV_Value')==undefined ? '0' : $('#slbBranche option:selected').data('NBUV_Value');
			}else {
				dataLinkText= $('#slbBranche option:selected').data('BUV_Link')==undefined ? '' : $('#slbBranche option:selected').data('BUV_Link');
				dataValue= $('#slbBranche option:selected').data('BUV_Value')==undefined ? '0' : $('#slbBranche option:selected').data('BUV_Value');
			}
			
		/*	var regExpr = /^-\d*\.{0,1}\d+$/;
			var checkMinus = regExpr.test(dataValue);
			if(!checkMinus){
				slotAnimation("+"+dataValue);
			}else{
				
			}*/
			slotAnimation(dataValue);
			
			//set business correctly in textfield and set link in text			
			$('li.checklist a').attr('href',dataLinkText);	
			$('span.selectBoxValue').html(optionText);
		}else {			
			$('div.resultText').hide();
			$('#contentContainer ul li.checklist').css({"visibility":"hidden"});
			$('#contentContainer p.homeStartText1').css({"visibility":"hidden"});
			$('#contentContainer p.homeStartText2').css({"visibility":"hidden"});
		}
	});	
	//load send to friend overlay
	$(".contentIn a[rel]").overlay({
		effect		: 'apple',
		oneInstance	: false, 
		onBeforeLoad: function() {						
			// grab wrapper element inside content
			var wrap = this.getOverlay().find(".contentWrap");						
			// load the page specified in the trigger
			wrap.load(this.getTrigger().attr("href"));
		},
		onLoad: function(){	
			validateForm();			
		}
	});
});

//load sliderData
function getPremiumData(element){	
	if (element.is(":checked")) {
		isChecked 	= 1;
	}else{
		isChecked 	= 0;
	}
	if(ajaxLoaded){	
		prepareData(tmpData);
	}else {
		// Disable caching of AJAX responses */
		$.ajaxSetup ({
			cache: false
		});
		$.getJSON(getData,prepareData);
		ajaxLoaded = true;
	}
}

function prepareData(data){
	$('#slbBranche option:gt(0)').remove();
	var currBranchenID = 0;
	var counter=0;
	
	//dump(data.Data,true);
	
	$.each(data.Data, function(i, item){
		BranchenID= item.classNr;
		valBUV= 0;
		valNBUV= 0;
		strLink= '';
		if(BranchenID != currBranchenID){
			row= '<option>' + item.Description + '</option>';
			$(row).appendTo('#slbBranche');
			counter++;
		}
		if(item.Categorie == 0){
			$('#slbBranche option:eq('+counter+')').data('BUV_Value',item.Value).data('BUV_Link',item.Link);
		}
		else{
			$('#slbBranche option:eq('+counter+')').data('NBUV_Value',item.Value).data('NBUV_Link',item.Link);
		}
		currBranchenID = BranchenID;	
	});
	/*$('#slbBranche').html($("#slbBranche option").sort(function (a, b) {
		return a.text == b.text ? 0 : a.text < b.text ? -1 : 1;
	}));*/
	$('#slbBranche option:eq(0)').attr('selected', 'selected');
}

function fadeHeadliners(){		
	$('#headliners').cycle({ 
			timeout: 5000, 
			cleartype: 1, 
			speed: 400
	});
}

//prepare array for exclamation
var exclamation 	= [];	
var counter 		= 0;
function setExclamations(){
	//count +1
	if (counter <=3){
		counter++;
	}else{
		counter = 1;			
	}
	// set array and object
	exclamation[1] 				= {};
	exclamation[1].cssStyle	= '.exlamationText_'+ HomeContent.language;
		
	exclamation[2] 				= {};
	exclamation[2].cssStyle	= '.exlamationText2_'+ HomeContent.language;
	
	exclamation[3] 				= {};
	exclamation[3].cssStyle	= '.exlamationText3_'+ HomeContent.language;
	
	exclamation[4] 				= {};
	exclamation[4].cssStyle	= '.exlamationText4_'+ HomeContent.language;
	
	
	//set style and animate	but not for IE! - Problems with PNG24 and animations		
	 if($.browser.msie){
		 $('#exclamationContainer ' + exclamation[counter].cssStyle).animate({
		    height: 'toggle'
		  }, {
		    duration: 500, 
		    complete: function() {
			  setTimeout(function(){
				 $('#exclamationContainer ' + exclamation[counter].cssStyle).css({"display": "none"});
				 //enable slider and select box
				// $('.selectBox select').attr('disabled','');
				// $('#sliderCheckbox :checkbox').attr('disabled','');
			  },1000);
		    }
		  });
	 }else{
		  $('#exclamationContainer ' + exclamation[counter].cssStyle).fadeIn(1000,function(){			
			setTimeout(function(){
				$('#exclamationContainer ' + exclamation[counter].cssStyle).fadeOut(1000); 
				//enable slider and select box
				//$('.selectBox select').attr('disabled','');
				//$('#sliderCheckbox :checkbox').attr('disabled','');
			},1000);
		 });
	 }
}

function slotAnimation(optionValue){
	$('div.resultText').hide();
	//only rotate when default not 0
	if(optionValue !== ' ' ){
		//disable slider and select box on animation
		//$('.selectBox select').attr('disabled','disabled');			
	//	$('#sliderCheckbox :checkbox').attr('disabled','disabled');
		$('.animation').show();		
		if($.browser.msie){
			$('.resultText').css({
				'top':'-55px'
			});					
		}else {
			$('.resultText').css({
				'padding-top' : '-55px'
			});
		}
		
		//set text from the select box
		var regExpr = /^-\d*\.{0,1}\d+$/;
		var checkMinus = regExpr.test(optionValue);
		
		if(!checkMinus){
			if (optionValue != 0){
				$('.resultText').html("+"+optionValue + "%");
			}else {
				$('.resultText').html(optionValue + "%");
			}				
		}else{
				$('.resultText').html(optionValue + "%");
		}
		
		//wait 0.7 seconds
		setTimeout(
		  function() 
		  {		
			  //show result text
			  $('.animation').hide();
			  $('.resultText').show();			  
			  $(".resultText").animate({ 
			    paddingTop: "25px"
			  },{ 
				  "duration": 600, 
				  "easing"	: "easeOutElastic"
			  });
			  
			//only show exclamation text if value not minus.  
			var regex =  /[-a-z-0-9\.,]+/i;
			var match = regex.exec(optionValue);
			if(match < "0"){
				if($('#on_off').is(':checked')){
					$('#contentContainer p.homeStartText1').css({"visibility":"visible"}).html(HomeContent.text4);
				}else{
					$('#contentContainer p.homeStartText1').css({"visibility":"visible"}).html(HomeContent.text1);
				}
				$('#contentContainer p.homeStartText2').css({"visibility":"visible"});
				//show exclamation Text
				//setExclamations();				
			}else if(match == "0"){
				if($('#on_off').is(':checked')){
					$('#contentContainer p.homeStartText1').css({"visibility":"visible"}).html(HomeContent.text6);
				}else{
					$('#contentContainer p.homeStartText1').css({"visibility":"visible"}).html(HomeContent.text3);
				}
				$('#contentContainer p.homeStartText2').css({"visibility":"visible"});
				$('.selectBox select').attr('disabled','');
				$('#sliderCheckbox :checkbox').attr('disabled','');					
			}else {
				if($('#on_off').is(':checked')){
					$('#contentContainer p.homeStartText1').css({"visibility":"visible"}).html(HomeContent.text5);
				}else{
					$('#contentContainer p.homeStartText1').css({"visibility":"visible"}).html(HomeContent.text2);
				}
				$('#contentContainer p.homeStartText2').css({"visibility":"visible"});
				$('.selectBox select').attr('disabled','');
				$('#sliderCheckbox :checkbox').attr('disabled','');					
			}
			
			$('#contentContainer ul li.checklist').css({"visibility":"visible"});
			  			  
		  }, 700);
	}else {
		$('#contentContainer p.homeStartText1').css({"visibility":"hidden"});
		$('#contentContainer p.homeStartText2').css({"visibility":"hidden"});
		$('.selectBox select').attr('disabled','');
		$('#sliderCheckbox :checkbox').attr('disabled','');	
		$('#contentContainer ul li.checklist').css({"visibility":"hidden"});
		
	}
}

