var dataBox = new dataHandler();
var clickFromTopNav = true;//For tracking
dataBox.init();

$(document).ready(function(){
	var navigationBox = new navigationHandler($("ul#progressBar li"));
	navigationBox.init();
	
	$('a#smallFont').click(function(){
		$('div.pageHolder *').css('font-size','12px');
	});
	$('a#mediumFont').click(function(){
		$('div.pageHolder *').css('font-size','13px');
	});
	$('a#largeFont').click(function(){
		$('div.pageHolder *').css('font-size','14px');
	});
	
	
	/******************************************** SLIDERS ********************************************/
	
	
	$("div.slidersPlace").each(function(){
		
		var page = $(this).attr('id').replace(/[a-zA-Z]/g,'');
		
		var slideSize = $("div#slideplace"+page).width();
		
		var movePx = slideSize / 5;
		var sliderHandle = -3;
		var currStep = 1;		
		
		$('div#leftarrow'+page).click(function(){
			var curr = $("div#slide"+page).css('margin-left').replace(/[a-zA-Z]/g,'');
			curr = parseInt(curr) - parseInt(movePx) - parseInt(sliderHandle);
			
			if(curr >= 0) {
				$("div#slide"+page).css('margin-left',curr+'px');
				currStep--;
				saveData(currStep,page);
			}										
		});
		
		$('div#slideplace'+page).click(function(e){					
				var relativeClickX = e.pageX - $(this).position().left;
				var currStep = (Math.floor(relativeClickX/87.25));
				$('div#slide'+page).css('margin-left',(Math.round(currStep*87.25))+'px');
				saveData(currStep+1,page);								
		});
		
		
		
		$('div#slide'+page).mousedown(function(){
			$().mousemove(function(e){
				
				var mousePosition = e.pageX - $('div#slideplace'+page).position().left;
				
				var marginLeft = parseInt($("div#slide"+page).css('margin-left').replace(/[a-zA-Z]/g,''));
				var marginLeft = mousePosition-51;
				
				if(marginLeft<0) marginLeft = marginLeft = 0;
				if(marginLeft>349) marginLeft = marginLeft = 349;
				$("div#slide"+page).css('margin-left',(marginLeft)+'px');
				
				
				$('div#formStep1 *').each(function(){
					$(this).bind('selectstart',function(){return false;});
					$(this).css('-moz-user-select', 'none'); 
				});
			});	
		});		
		
		$('div#slide'+page).mouseup(function(){
			$().unbind('mousemove');
			$('div#formStep1 .slide').each(function(){
				var marginLeft = parseInt($(this).css('margin-left').replace(/[a-zA-Z]/g,''));
				var currStep = (Math.round(marginLeft/87.25));
				$(this).css('margin-left',(Math.round(currStep*87.25))+'px');
				var thisPage = $(this).attr("id").replace("slide","");
				saveData(currStep+1,thisPage);
			});
			$(this).unbind('selectstart');
				$(this).css('-moz-user-select', 'normal'); 
		});
		
		$().mouseup(function(){
			$().unbind('mousemove');
			$('div#formStep1 .slide').each(function(){
				var marginLeft = parseInt($(this).css('margin-left').replace(/[a-zA-Z]/g,''));
				var currStep = (Math.round(marginLeft/87.25));
				$(this).css('margin-left',(Math.round(currStep*87.25))+'px');
				var thisPage = $(this).attr("id").replace("slide","");
				saveData(currStep+1,thisPage);
			});
			$(this).unbind('selectstart');
			$(this).css('-moz-user-select', 'normal'); 
		});
		
		$('div#rightarrow'+page).click(function(){
			
			alert(currStep);
			var curr = $("div#slide"+page).css('margin-left').replace(/[a-zA-Z]/g,'');
			curr = parseInt(curr) + parseInt(movePx) + parseInt(sliderHandle);
			
			if(currStep < 5) {
				$("div#slide"+page).css('margin-left',curr+'px');
				currStep++;
				saveData(currStep,page); 
			}						
		});
		
		//dataBox.currentStepNumber = '1';
		var controllerValue = parseInt( dataBox.getValue($('#slider'+page+'amount').attr('name')) );
		
		if(!controllerValue) {	controllerValue = 3;	}
		
		moveToStep(page,controllerValue);
		/*for(ind=1;ind<controllerValue;ind++) {
			$('div#rightarrow'+page).click();	
		}*/
		$('#slider'+page+'amount').val(controllerValue);
		
		//console.log(controllerValue);
	});	
	
	
	function moveToStep(page,currStep){
				$('div#slide'+page).css('margin-left',(Math.round((currStep-1)*87.25))+'px');
				saveData(currStep,page);
	}
	
	
	
	/*************************************************************************************************/	
	/*************************************** Form Step 1 and 2 ***************************************/
	var inputName = '';
	var currentValue = '';
	var stepIsCompleted = '';
	
	// click on step one controllers
	
	$('div#formStep2 input:radio').each(function(){
		if(inputName != $(this).attr('name')) {
			// set input name and value if this is a new var
			dataBox.currentStepNumber = $(this).parent().attr('class').replace(/[a-zA-Z]/g,'');
			inputName = $(this).attr('name');
			currentValue = dataBox.getValue(inputName);
			
			// turn the controllers to gray
			if($(this).attr('type') == 'radio' && (currentValue == '' || !currentValue)) {			
				var nextBox = $(this).parents('.cheboxesSpace2').next('.cheboxesSpace2');
				if(nextBox.attr('class') != undefined) {
					nextBox.css('color','#CCC');
					nextBox.find('input:radio').attr('disabled',true);
				}
			}
			
		}
		
		// check if the current value is the one on the cookies
		if($(this).attr('type') == 'radio') {
				
			if(currentValue == $(this).val()) { $(this).attr('checked',true); }
			/* CHECK THIS LINE */
			if(currentValue == null){ dataBox.setValue(inputName,''); }
						
			// add on click event to the element
			$(this).click(function() {

				// Enable the next controller
				var nextBox = $(this).parents('.cheboxesSpace2').next('.cheboxesSpace2');
				if(nextBox.attr('class') != undefined) {
					nextBox.css('color','#666');
					nextBox.find('input:radio').attr('disabled',false);
				} else {
					dataBox.currentStepNumber = '2';
					dataBox.setValue('stepCompleted','true');
					stepIsCompleted = 'true';
					//console.log('complete');
				}
				
				// set the currentStepNumber yet again on the click because this value may change
				// during the initialization of the components, this way will be always applied to the current page
				dataBox.currentStepNumber = $(this).parent().attr('class').replace(/[a-zA-Z]/g,'');
				// change the current var value on the cookies
				dataBox.setValue($(this).attr('name'),$(this).attr('value'));
				stepIsCompleted = dataBox.getValue('stepCompleted');
				
				if($(this).attr("name")=="SC_Q1"){
				    s.events='event18';
	                var s_code=s.t();
	                if(s_code)document.write(s_code);
	                s.events='';
				}
			});
		} 
		
	});
	/*******************************************************************************************/
	
	/*************************************** Form Step 3 ***************************************/	
	$('li#progressPage3').click(function(){
		dataBox.currentStepNumber = '3';
		dataBox.setValue('stepCompleted','true');
	});
	/*******************************************************************************************/
	//
	/*************************************** Form Step 4 ***************************************/	
	var pageOpen;
	var groupNumber;
	$(".option-tree h3").each(function(){
		dataBox.currentStepNumber = '4';
		
		groupNumber = $(this).attr('id').replace(/[a-z\-]/g,'');
		
		if(dataBox.getValue('openedPage'+groupNumber) == 'true') { 
			group = this.id.replace("title-","");
			$("#"+group).removeClass("inactivegroup").addClass("activegroup");
			$(this).removeClass("collapsed").addClass("expanded");
		}
		
		$(this).click(function(){
			group = this.id.replace("title-","");
			groupNumber = $(this).attr('id').replace(/[a-z\-]/g,'');
			dataBox.currentStepNumber = '4';
			if($(this).hasClass("collapsed")){
				$("#"+group).removeClass("inactivegroup").addClass("activegroup");
				$(this).removeClass("collapsed").addClass("expanded");
				dataBox.setValue('openedPage'+groupNumber,'true');
				
				s.events='event19';
	            var s_code=s.t();
	            if(s_code)document.write(s_code);
	            s.events='';
			}else{
				$("#"+group).removeClass("activegroup").addClass("inactivegroup");
				$(this).removeClass("expanded").addClass("collapsed");
				dataBox.setValue('openedPage'+groupNumber,'false');
			}
			
			dataBox.currentStepNumber = '4';
			dataBox.setValue('stepCompleted','true');
			
			
		});
	});	

	var currentValue;
	var attfor;
	var DDC_Q1_ALL_UNSELECTED = true;
	var DDC_Q2_ALL_UNSELECTED = true;
	var DDC_Q3_ALL_UNSELECTED = true;
	$('div#formStep4 .option-tree label').each(function(){
		attfor = $(this).attr("for");
		dataBox.currentStepNumber = '4';
		currentValue = dataBox.getValue(attfor);
		if(currentValue == null) {
			currentValue='1'; 
			$("#"+attfor).val('1'); 
			
			$("#"+attfor).val(currentValue); 
			$(this).parent().find("div.checkboxwrapper").removeClass("unchecked").addClass("checked");
			
		}
		else { 
			$("#"+attfor).val(currentValue); 
			if(currentValue == '0') {
				$(this).parent().find("div.checkboxwrapper").removeClass("checked").addClass("unchecked");
				if(attfor.substring(0,6) == 'DDC_Q1') DDC_Q1_ALL_UNSELECTED = false;
			    if(attfor.substring(0,6) == 'DDC_Q2') DDC_Q2_ALL_UNSELECTED = false;
			    if(attfor.substring(0,6) == 'DDC_Q3') DDC_Q3_ALL_UNSELECTED = false;
			} else {
				$(this).parent().find("div.checkboxwrapper").removeClass("unchecked").addClass("checked");
				
			}
		}
		
		
		$(this).click(function() {
		    
			attfor = $(this).attr("for");
			if($("#"+attfor).val() == 1){
				dataBox.currentStepNumber = '4';
				dataBox.setValue(attfor,'0');
				$("#"+attfor).val('0');
				$(this).parent().find("div.checkboxwrapper").removeClass("checked").addClass("unchecked");
			} else {
				dataBox.currentStepNumber = '4';
				dataBox.setValue(attfor,'1');
				$("#"+attfor).val('1');
				$(this).parent().find("div.checkboxwrapper").removeClass("unchecked").addClass("checked");
			}
			var allSelected = true;
			var allUnselected = true;
			$(this).parent().parent().find("input").each(function(){
			    if($(this).val()==1)
			        allUnselected = false;
			    if($(this).val()==0)
			        allSelected = false;
			});
			if(allSelected)
			    $(this).parent().parent().find('.deselectlink').text('Deselect All');
			if(allUnselected)
			    $(this).parent().parent().find('.deselectlink').text('Select All');
		});
	});
	//alert("DDC_Q1_ALL_UNSELECTED=" + DDC_Q1_ALL_UNSELECTED + "\nDDC_Q2_ALL_UNSELECTED=" + DDC_Q2_ALL_UNSELECTED + "\nDDC_Q3_ALL_UNSELECTED=" + DDC_Q3_ALL_UNSELECTED);
	if(!DDC_Q1_ALL_UNSELECTED){
	    $('div#formStep4 #questiongroup-1 .deselectlink').text('Select All');
	}
	if(!DDC_Q2_ALL_UNSELECTED){
	    $('div#formStep4 #questiongroup-2 .deselectlink').text('Select All');
	}
	if(!DDC_Q3_ALL_UNSELECTED){
	    $('div#formStep4 #questiongroup-3 .deselectlink').text('Select All');
	}
	$("div#formStep4 .checkboxwrapper").each(function(){
		$(this).click(function(){
			$(this).parent().find("label").click();
		});
	});
	$("div#formStep4 .deselectlink").each(function(){
	
	    
	    
		$(this).click(function(){
		    var selectAll = $(this).text()!="Deselect All";
			$(this).parent().find("input").each(function(){
			    if(selectAll)
			        $(this).val('0');    
			    else
			        $(this).val('1');    
				$(this).parent().find("label").click();
			});
			if(!selectAll)
			    $(this).text("Select All");
			else
			    $(this).text("Deselect All");
		});
	});
	
	
	
	/*******************************************************************************************/
	
	/*************************************** Form Step 5 ***************************************/
	var currentValue = '';
	/*
	$('li#progressPage5').click(function(){
		dataBox.currentStepNumber = '5';
		dataBox.setValue('stepCompleted','true');
	});
	*/
	$('div#formStep5 input:checkbox').each(function(){
		dataBox.currentStepNumber = '5';
		currentValue = dataBox.getValue($('input#'+$(this).attr('name')+'Hidden').attr('name'));
		
		
		
		if(currentValue == null) {
			$(this).attr('checked',true);
			$('#'+$(this).attr('name')+'Hidden').val('1');		
			$(this).parent().find(".checkboxwrapper").removeClass("unchecked").addClass("checked");			
			dataBox.setValue($('input#'+$(this).attr('name')+'Hidden').attr('name'),'1');
		} else if(currentValue == '1') {
			$(this).attr('checked',true);
			$(this).parent().find(".checkboxwrapper").removeClass("unchecked").addClass("checked");			
			$('#'+$(this).attr('name')+'Hidden').val('1');
		} else {
			$(this).attr('checked',false);
			$(this).parent().find(".checkboxwrapper").removeClass("checked").addClass("unchecked");			
			$('#'+$(this).attr('name')+'Hidden').val('0');
		}
		
		$('#'+$(this).attr('name')+'Remove').click(function(){
			var compName = $(this).attr('id').replace(/Remove/,'');
			if($('#'+compName).attr('checked') == true) {
				$('#'+compName).attr('checked',false);
				var inputObject = $('#'+$(this).attr('id').replace(/Remove/,'')+'Hidden');
				inputObject.val('0');
								
				dataBox.currentStepNumber = '5';
				inputObject.parent().find(".checkboxwrapper").removeClass("checked").addClass("unchecked");
				dataBox.setValue(inputObject.attr('name'),'0');
			}
			return false;
		});
		
		$(this).click(function(){
			if($(this).attr('checked') == true) {
				$('#'+$(this).attr('name')+'Hidden').val('1');
				$(this).parent().find(".checkboxwrapper").removeClass("unchecked").addClass("checked");
				dataBox.currentStepNumber = '5';
				dataBox.setValue($('#'+$(this).attr('name')+'Hidden').attr('name'),'1');	
			} else {
				$('#'+$(this).attr('name')+'Hidden').val('0');
				$(this).parent().find(".checkboxwrapper").removeClass("checked").addClass("unchecked");
				dataBox.currentStepNumber = '5';
				dataBox.setValue($('#'+$(this).attr('name')+'Hidden').attr('name'),'0');
			}							
		});
		
		//IE can't seem to detect a label click if the associated checkbox is hidden
		//so I have to check if the browser is IE, then force the normal behaviour of marking the input when
		//clicking on the label
		
		//note to self: ie sux
		if($.browser.msie){
			$(this).parent().find("label").click(function(){
				objCheck = $(this).parent().find("input:checkbox");
				
				if(objCheck.attr("checked")==true){
					objCheck.attr("checked",false);
					$('#'+objCheck.attr('name')+'Hidden').val('0');
					$(this).parent().find("div.checkboxwrapper").removeClass("checked").addClass("unchecked");
					dataBox.currentStepNumber = '5';
					dataBox.setValue($('#'+objCheck.attr('name')+'Hidden').attr('name'),'0');
				}else{
					objCheck.attr("checked",true);
					$('#'+$(objCheck).attr('name')+'Hidden').val('1');
					$(this).parent().find("div.checkboxwrapper").removeClass("unchecked").addClass("checked");
					dataBox.currentStepNumber = '5';
					dataBox.setValue($('#'+$(objCheck).attr('name')+'Hidden').attr('name'),'1');	
				}
				
			});
		}	
	});
	
	//handlers for customized checkboxes
	$('div#formStep5 .checkboxwrapper').click(function(){
		if($(this).hasClass("checked")){
			objCheck = $(this).parent().find("input:checkbox");
			objCheck.attr("checked",false);
			$('#'+objCheck.attr('name')+'Hidden').val('0');
			$(this).removeClass("checked").addClass("unchecked");
			dataBox.currentStepNumber = '5';
			dataBox.setValue($('#'+objCheck.attr('name')+'Hidden').attr('name'),'0');
		}else{
			objCheck = $(this).parent().find("input:checkbox");
			objCheck.attr("checked",true);
			$('#'+$(objCheck).attr('name')+'Hidden').val('1');
			$(this).removeClass("unchecked").addClass("checked");
			dataBox.currentStepNumber = '5';
			dataBox.setValue($('#'+$(objCheck).attr('name')+'Hidden').attr('name'),'1');	
		}
		
	});
	
	/*******************************************************************************************/
	
	/*************************************** FORM SUBMIT ***************************************/
	$('div#formSubmit a').click(function(){
		var checkvalues = $('#mainForm').serialize();
		var wnd = window.open('about:blank','seeingSingsPost','with=200,heigth=200');
		$('#mainForm').submit();
	});			
	/*******************************************************************************************/			
	
	$("#example").tabs();
	
});

var DialogHandler = {

	Benefits: function(){
		
		$("#benefitsdialog").dialog({
			bgiframe: true,
			height: 340,
			width: 600, 
			modal: false,
			resizable:false
		});
		
		$("#benefitsdialog").dialog().parents(".ui-dialog").find(".ui-dialog-titlebar").remove(); 
	
		$("#benefitsdialog a.close").click(function(){
			$("#benefitsdialog").dialog('destroy');
			hideBackgroundOverlay();
		});
		showBackgroundOverlay();
		
	},
	
	Symptoms: function(){
	
		
		$("#symptomsdialog").dialog({
			bgiframe: true,
			height: 500,
			width: 600,
			modal: false,
			resizable:false
		});
		
		$("#symptomsdialog").dialog().parents(".ui-dialog").find(".ui-dialog-titlebar").remove(); 
	
		$("#symptomsdialog a.close").click(function(){
			$("#symptomsdialog").dialog('destroy');
			hideBackgroundOverlay();
		});
		showBackgroundOverlay();
		
	},
	
	SetSymptomsTab: function(n){
		//$("#symptomsdialog .tabset").removeClass('tabset-1').removeClass('tabset-2').removeClass('tabset-3').addClass('tabset-'+n);
		if(n==1){
		    
		    $("#symptomsdialog .tabset2").css("display","none");
		    $("#symptomsdialog .tabset3").css("display","none");
		    $("#symptomsdialog .tabset1").css("display","block");
		    $("#symptomsdialog .tabset1").css("zIndex","999999");
		}
		else if(n==2){
		    $("#symptomsdialog .tabset1").css("display","none");
		    
		    $("#symptomsdialog .tabset3").css("display","none");
		    $("#symptomsdialog .tabset2").css("display","block");
		    $("#symptomsdialog .tabset2").css("zIndex","999999");
		}
		else if(n==3){
		    $("#symptomsdialog .tabset1").css("display","none");
		    $("#symptomsdialog .tabset2").css("display","none");
		    $("#symptomsdialog .tabset3").css("display","block");
		    $("#symptomsdialog .tabset3").css("zIndex","999999");
		}
	},
	
	Mail: function (){
	
		$("#maildialog").dialog({
			bgiframe: true,
			height: 500,
			width: 600,
			modal: false,
			resizable:false
		});
		
		$("#maildialog").dialog().parents(".ui-dialog").find(".ui-dialog-titlebar").remove(); 
	
		$("#maildialog a.close").click(function(){
			$("#maildialog").dialog('destroy');
			hideBackgroundOverlay();
		});
		showBackgroundOverlay();
		$("#maildialog").parent().css("top", "200px").css("left","220px");
	}

}
function showBackgroundOverlay(){

	var p = $("#footer").position();	
	var h = p.top-35;
	
	$('#dialogOverlay').css({
		 'height': h,
		 'width': $(document).width(),
		 'display': 'block'
	});	
}
function hideBackgroundOverlay(){
	$('#dialogOverlay').css({
		 	  'display': 'none'
	});	
}

function saveData(data,place) {
	$('#slider'+place+'amount').val(data);
	dataBox.currentStepNumber = '1';
	dataBox.setValue($('#slider'+place+'amount').attr('name'),data.toString());
	dataBox.setValue('stepCompleted','true');
}
