﻿$(document).ready(function(){	
	/*
	----------------------------------------------------------------------
	GET URL PARAMATER
	----------------------------------------------------------------------
	*/
	function getURLParm(name) {
		return unescape(
			(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
		);
	}
	
	/*
	----------------------------------------------------------------------
	PRODUCT SELECTOR CODE
	----------------------------------------------------------------------
	*/
	var resetFlag = true;
	var firstClickFlag = true;
	var wsFormValue = 'null';
	var esFormValue = 'null';
	var dlpFormValue = 'null';
	
	//Reset checkboxes && radio boxes
	if( resetFlag ){
		$('#selector-left input:radio, #selector-left input:checkbox').attr('checked', false);
	}

	$('input:radio[name=web-security]').click(function(){
		wsFormValue = '#' + $(this).val();		
		checkFirstClick( $('#selector-web') );
		
		if( wsFormValue != '#none'){
			$('#selector-web .logic').removeClass('logic wslogic')
			$(wsFormValue).addClass('logic wslogic');	
		}	
		else
			$('#selector-web .logic').removeClass('logic')
			
		standardCheck('#selector-web');	
	})
	
	$('input:radio[name=email-security]').click(function(){	
		esFormValue = $(this).val();
		if(esFormValue == 'es'){
			$('#es-select').addClass('logic eslogic');
		}	
		else{	
			$('#es-select').removeClass('logic eslogic');
		}
		checkFirstClick( $('#selector-email') );
		standardCheck('#selector-email');
	})
	
	$('input:radio[name=data-security]').click(function(){
		dlpFormValue = $(this).val();
		if(dlpFormValue == 'dss'){
			$('#dss').addClass('logic dslogic');
		}	
		else{	
			$('#dss').removeClass('logic dslogic');
		}
		checkFirstClick( $('#selector-data') );
		standardCheck('#selector-dlp');
	})

	
	function selectCheck(){
		if( wsFormValue != 'null' && dlpFormValue != 'null' && esFormValue != 'null')
			return true

		return false	
	}
	
	function checkFirstClick(obj){
		if( firstClickFlag ){
			selectorFadeOut( $('.ps-anchor'));
		}		
	}
	
	function standardCheck(psParent){
		if(selectCheck()){
			firstClickFlag = false;
			if( $('#dss').hasClass('dslogic') && $('#es-select').hasClass('eslogic') && wsFormValue != '#none'){
				$('.ps-anchor[href=#ps-te]').addClass('telogic');
				
				if(wsFormValue == '#recommended')
					$('.ps-anchor[href=#ps-te]').addClass('terlogic');
				else	
					$('.ps-anchor').removeClass('terlogic');
			}
			else{
				$('.ps-anchor[href=#ps-te]').removeClass('telogic terlogic');		
			}	
			
			if( $('#es-select').hasClass('eslogic') && wsFormValue == '#recommended' && $('#dss').hasClass('dslogic') == false )
				$('.ps-anchor[href=#ps-tsga]').addClass('tsgalogic');
			else	
				$('.ps-anchor[href=#ps-tsga]').removeClass('tsgalogic');
		}
		
		if( $('.ps-anchor').hasClass('telogic') )
			selectorFadeIn( $('.telogic') );
		else	
			selectorFadeOut( $('.ps-anchor[href=#ps-te]') );

		if( $('.ps-anchor').hasClass('terlogic')== false && $('.ps-anchor').hasClass('tsgalogic') == false){
			selectorFadeOut( $(psParent + ' .ps-anchor, .terlogic') );
			selectorFadeIn( $('.logic .ps-anchor') );
		}
		else{
			selectorFadeOut( $('.ps-anchor').not('.terlogic') );
			selectorFadeIn( $('.terlogic') );
		}
		
		if( $('.ps-anchor').hasClass('tsgalogic') )
			selectorFadeIn( $('.tsgalogic') );
		else	
			selectorFadeOut( $('.ps-anchor[href=#ps-tsga]') );
		
	}
	
	//Fade out icons
	function selectorFadeOut(icon){
		$(icon).animate({
			opacity: .15
		}, 250);
		
	}
	
	//Fade in icons
	function selectorFadeIn(icon){
		$(icon).animate({
			opacity: 1
		}, 250);
	}
	
	//Modal window code
	var openedAnchor;
	
	function showSelectorModal(window){
		$(window).fadeIn(500, function(){
			if (!$.support.opacity) 
				this.style.removeAttribute('filter');
		})
	}
	
	function hideSelectorModal(window){
		$(window).fadeOut(125);
		$(openedAnchor).removeClass('opened');
	}
	
	$('.ps-modal-close').click(function(e){
		e.preventDefault();
		hideSelectorModal( $(this).parents('.ps-modal') );
	});
	
	$('.ps-anchor').click(function(e){
		e.preventDefault();
		if( $(this).hasClass('inactive') == false && $(this).hasClass('opened') == false){
			$(this).addClass('opened');
			hideSelectorModal( $(openedAnchor).attr('href') );
			showSelectorModal( $(this).attr('href') );
			openedAnchor = $(this);
		}	
	});
	
	/*Help Me Choose Modal Window Logic*/
	$('#hmc-submit, .hmc-submit').click(function(e){
		e.preventDefault();
		$('#hmc-form').submit();
	});
	
	var hmcFlag = getURLParm('hmc-flag');

	if( hmcFlag == 'true'){
		var wsValue = getURLParm('hmc-ws');
		var esValue = getURLParm('hmc-es');
		var dlpValue = getURLParm('hmc-dlp');
		
		resetFlag = false;
		
		$('input[value='+wsValue+']').attr('checked', true);
		$('#esChoice input[value='+esValue+']').attr('checked', true);
		$('#dlpChoice input[value='+dlpValue+']').attr('checked', true);
		
		selectorFadeOut($('.outer-grey .ps-anchor'));
		
		if( wsValue != 'none')
			$('#'+wsValue).addClass('logic wslogic');
	
		
		if(esValue == 'es')
			$('#es-select').addClass('logic eslogic');	

		if(dlpValue == 'dss')
			$('#dss').addClass('logic dslogic');	
			
		wsFormValue = '#' + wsValue;
		esFormValue = esValue;	
		dlpFormValue = dlpValue;			
		
		standardCheck();
			
	}
	
	$('.show-modal-window[href=#help-me-choose]').click(function(e){
		$('.hmc-slide').first().fadeIn('fast', function(){
			if (!$.support.opacity) 
				this.style.removeAttribute('filter');
		});	
		$('input:radio.hmc-radio').removeAttr('checked');
		$('input:radio.hmc-radio.checked').attr('checked', true);
	});
	
	//Next slide functions
	$('#help-me-choose .hmc-continue').click(function(e){
		e.preventDefault();
		object = $(this).parents('.hmc-slide');
		$(object).fadeOut('fast', function(){
			$(object).next().fadeIn('fast', function(){
			if (!$.support.opacity) 
				this.style.removeAttribute('filter');
			});		
		})

	});	
	
	//Specific Link functions
	$('#help-me-choose .hmc-picker').click(function(e){
		e.preventDefault();
		openSlide = $(this).attr('href');
		object = $(this).parents('.hmc-slide');
		$(object).fadeOut('fast', function(){
			$(openSlide).fadeIn('fast', function(){
			if (!$.support.opacity) 
				this.style.removeAttribute('filter');
			});		
		})
	});	
	
		
	$('#help-me-choose .modal-close').click(function(e){
		$('.hmc-slide').hide();
	})
});	
