(function($) {  

	var gsdivname = '.getting_started';
	var savedinfo = [];
	var curr_step = 0;
	var step_name =new Array();
	
	
	$(document).ready(function(){
		setLinks();
		reset();
		changePage('home');
		
	});
	
	

	
	//Resets states of objects
	function reset(){	
	
	
		$(gsdivname + ' .gs_logo').removeClass('selected');
		$(gsdivname + ' .gs_next_button').css('display','none').css('opacity','0');
		$(gsdivname + ' .gs_submit_button').css('display','none').css('opacity','0');
		$(gsdivname + ' .gs_breadcrumb').removeClass('bg').html('');
		$(gsdivname + ' .gs_whobutton').removeClass('selected');
		$(gsdivname + ' .gs_insbutton').removeClass('selected');	
		$(gsdivname + ' .gs_hcpbutton').removeClass('selected');
		$(gsdivname + ' .gs_title').show();
		$(gsdivname + ' .gs_title.results').hide();
		
		
		
		
		closeMoreInfo();
		changePage('home');
	}
	
	//Set the click events
	function setLinks(){	
		$(gsdivname + ' .gs_next_button.page0').bind('click', function() { 
			curr_step++;
		
			changePage('who');

			tracker(savedinfo['med'],savedinfo['med'],'Step1',1,$("title").text());
		
		});
		$(gsdivname + ' .gs_back_button.page1').bind('click', function() { curr_step++;reset_med_buttons();changePage('home');});
		$(gsdivname + ' .gs_next_button.page1').bind('click', function() { 
			curr_step++;
			
			tracker(savedinfo['med'] + "|" + savedinfo['who'],savedinfo['med'],savedinfo['who'],2,$("title").text());
			
			if(savedinfo['who'] == 'Need Help Paying'){
				changePage('insurance');
			}else{
				changePage('looking');
			}
		
		
		
		});
		$(gsdivname + ' .gs_back_button.page2').bind('click', function() { curr_step++;reset_who_buttons();changePage('who');});
		$(gsdivname + ' .gs_submit_button').bind('click', function() {
		curr_step++;
		
		var step_3 = "";
		
		if (savedinfo['who']=="Need Help Paying")
		{
			step_3 = savedinfo['insured?'];
		}
		else
		{
		 	step_3 = savedinfo['hcpaction'];
		}
		
		tracker_final_step(savedinfo['med'] + "|" + savedinfo['who'] + "|" + savedinfo['insured?'],savedinfo['med'],savedinfo['who'],step_3,3, $("title").text());
		 submit();
		$(gsdivname + ' .gs_title').hide();
		$(gsdivname + ' .gs_title.results').show();});
		$(gsdivname + ' .gs_back_button.page3').bind('click', function() { 
			curr_step++;
			reset_ins_buttons();
			
			$(gsdivname + ' .gs_title').show();
			$(gsdivname + ' .gs_title.results').hide();
			if(savedinfo['who'] == 'Need Help Paying'){
				changePage('insurance');
			}else{
				changePage('looking');
			}
		});	
		$(gsdivname + ' .gs_whobutton').bind('click', function() {
  			setWho($(this).attr('name'));
		})
		
		$(gsdivname + ' .gs_insbutton').bind('click', function() {
  			setInsurance($(this).attr('name'));
		})
		
		$(gsdivname + ' .gs_hcpbutton').bind('click', function() {
  			setHcp($(this).attr('name'));
		})
		
		$(gsdivname + ' .gs_moreinfo .gs_moreinfo_icon').bind('click', function() {
  			if($(gsdivname + ' .gs_moreinfo').hasClass('open')){
  				closeMoreInfo();
  			}else{
  				openMoreInfo();
  			}
		})
		
		$(gsdivname + ' .gs_moreinfo .gs_moreinfo_close').bind('click', function() {
  			closeMoreInfo();
		})
	
		$(gsdivname + ' .gs_logo').each(function(x){
		
			$(this).find('.gs_logo_image').bind('click', function() {
  				selectProduct($(this).parent('.gs_logo').attr('name'))
			})
			
			/*.siblings('.gs_pi_link').bind('click', function() {
  				alert('This will link to the PI for ' + $(this).parent('.gs_logo').attr('name') );
			});
			*/
		
		})
		
		$(gsdivname + ' .start_over').bind('click', function() {
  			reset();
		})
	
	}
	
	
	function reset_med_buttons()
	{
	$(gsdivname + ' .gs_logo').each(function(x){
			
				$(this).removeClass('selected');
				
			
			});
	$(gsdivname + ' .gs_breadcrumb.product').html('').removeClass('bg');	
	$(gsdivname + ' .gs_next_button.page0').css('display','block').animate({opacity: 0}, 300);	
	}
	
	function reset_who_buttons()
	{
	 $(gsdivname + ' .gs_whobutton').each(function(x){
			
				$(this).removeClass('selected');
				
			
			});
	$(gsdivname + ' .gs_breadcrumb.who').html('').removeClass('bg');	
	$(gsdivname + ' .gs_next_button.page1').css('display','block').animate({opacity: 0}, 300);	
	}
	
	
	function reset_ins_buttons()
	{
	
	$(gsdivname + ' .gs_insbutton').each(function(x){
			
				
				$(this).removeClass('selected');
				
				
			
			});
			
			$(gsdivname + ' .gs_hcpbutton').each(function(x){
			
				
				$(this).removeClass('selected');
				
				
			
			});
	//$(gsdivname + ' .gs_breadcrumb.ins').html('').removeClass('bg');	
	//$(gsdivname + ' .gs_next_button.page2').css('display','block').animate({opacity: 0}, 300);	
	$(gsdivname + ' .gs_breadcrumb.inshcp').html('').removeClass('bg');
	$(gsdivname + ' .gs_submit_button').css('display','none').css('opacity','0');	
	
	
	}
	
	
	// Change the page
	function changePage(pageID){
		closeMoreInfo();
		
		
		$(gsdivname + ' .gs_page_container').animate({opacity: 0}, 300,function(){
			$(this).find('.gs_page').each(function(x){
				if($(this).attr('pageID') == pageID){
					$(this).css('left','0px');
					
				}else{
					$(this).css('left','-9999px');
				}
			
			});
			
			$(gsdivname + ' .gs_page_container').animate({opacity: 1}, 300);
			
			if(pageID == 'insurance' || pageID == 'looking'){
				//$(gsdivname + ' .gs_breadcrumb.inshcp').html('').removeClass('bg');
				//$(gsdivname + ' .gs_submit_button').css('display','none').css('opacity','0');	
			}
		});
		

		var indNum;
		
		switch(pageID){
			case 'home':
				indNum = 0;
				break;
			case 'who':
				indNum = 1;
				window.location='#1';
				step_name[curr_step]=1;
				
				break;
			case 'insurance':
				indNum = 2;
				window.location='#2';
				step_name[curr_step]=2;
				break;
			case 'looking':
				indNum = 2;
				window.location='#2';
				step_name[curr_step]=2;
				break;
			case 'info':
				indNum = 3;
				window.location='#3';
				step_name[curr_step]=3;
				break;
		
		}
		
		$(gsdivname + ' .gs_breadcrumb').each(function(x){
			if(x <= indNum){
				$(this).animate({opacity: 1}, 300);
			}else{
				$(this).animate({opacity: 0}, 300);
			}	
		});
		
		//alert("here" +  gsdivname);
		$(gsdivname + ' .gs_dot_indicator').css('background-position','0px '+indNum*-23+'px');
		
		//alert(gsdivname + ' ' + '0px '+indNum*-23+'px');
		
		//$(gsdivname + ' .gs_dot_indicator1').css('background-position','0px 0px');
		//$(gsdivname + ' .gs_dot_indicator1').css('background-position','0px -23px');
		//$(gsdivname + ' .gs_dot_indicator2').css('background-position','0px -46px');
	
		

	}

	
	// Choose the product
	function selectProduct(name){
	
		savedinfo['name'] = name;
		
		$(gsdivname + ' .gs_logo').each(function(x){
			if(name == $(this).attr('name')){
				$(this).addClass('selected');
				setMed($(this).attr('id'));
				//savedinfo['med']= $(this).attr('id');
			}else{
				$(this).removeClass('selected');
			}
		})
		
		$(gsdivname + ' .gs_breadcrumb.product').html(name).addClass('bg');
		
		$(gsdivname + ' .gs_next_button.page0').css('display','block').animate({opacity: 1}, 300);
	}
	
	function setMed(name)
	{
	  savedinfo['med'] = name;
	}
	
	// Set who
	function setWho(name){
		
		savedinfo['who'] = name;
		
		$(gsdivname + ' .gs_whobutton').each(function(x){
			if($(this).attr('name') == name){
				$(this).addClass('selected');
			}else{
				$(this).removeClass('selected');
			
			}
		})
		
		$(gsdivname + ' .gs_breadcrumb.who').html(name).addClass('bg');
		$(gsdivname + ' .gs_next_button.page1').css('display','block').animate({opacity: 1}, 300);
	}
	
	// Set the insurance
	function setInsurance(name){
		
		var insType=new Array(); 
		insType[0]="none";       
		insType[1]="ins";
		insType[2]="medicade";
		insType[3]=	"medicare";
		
		
		
		$(gsdivname + ' .gs_insbutton').each(function(x){
			var current_ins = savedinfo['insured?'];
			var cs = $(this).attr('class');	
			
			
			
			if($(this).attr('name') == name){
			
					if (cs.indexOf('selected')>2)
					{
						$(this).removeClass('selected');
					}
					else
					{
					$(this).addClass('selected');

					//bc = $(this).attr('info');	
					}

			}
			
			if($(this).attr('name') == "I have none"){
			var cs = $(this).attr('class');	
			if (cs.indexOf('selected')>2)
					{
				//alert($(this).attr('name'));
				$('.gs_insbutton.ins').removeClass('selected');
				$('.gs_insbutton.medicare').removeClass('selected');
				$('.gs_insbutton.medicade').removeClass('selected');
				}
				
			}
			if($(this).attr('name') != "I have none"){
			var cs = $(this).attr('class');	
			if (cs.indexOf('selected')>2)
					{
				
				$('.gs_insbutton.none').removeClass('selected');
				}
				
			}
			
			
			/*var ns = $(this).attr('class');	
			
			for (k=0;k<insType.length;k++)
				{
					alert(ns +  " " + ns.indexOf(insType[k]) + " " + ns.indexOf('selected'));
					if (ns.indexOf(insType[k])>2 && ns.indexOf('selected')>2)
					{
				
						cs.indexOf(insType[k])=1;
				
					}
			}
			*/
			
			//alert(ins);
			
		})
		
		var none =$('.gs_insbutton.none.selected').length>0;
		var ins = $('.gs_insbutton.ins.selected').length>0;
		var medicare=$('.gs_insbutton.medicare.selected').length>0;
		var medicade=$('.gs_insbutton.medicade.selected').length>0;
	
		
		var _pass_value="";
		if (ins==true && (medicare==false && medicade==false)) 
		{
			_pass_value="Insured";
		}
		else if ((medicare==true || medicade==true))
		{
			_pass_value="Medicare";
		}
		else
		{
			 _pass_value="Uninsured";
		}
		
		//alert(_pass_value);
		
		if (none==true && (ins==false && medicare==false && medicade==false))
		{
			bc="Uninsured";
		}
		else if (ins==true ||((medicare==true && medicade==true) ||  (medicare==true && medicade==false) || (medicare==false && medicade==true)))
		{
			bc="Insured";
		}
		else if ((ins==false && (medicare==true || medicade==true)))
		{
			bc="Insured";
			//bc="Dual Eligible";
		}
		
		
		
		savedinfo['insured?'] = _pass_value;
		
		$(gsdivname + ' .gs_breadcrumb.inshcp').html(bc).addClass('bg');
		$(gsdivname + ' .gs_submit_button').css('display','block').animate({opacity: 1}, 300);
	}
	
	// Set the hcp action
	function setHcp(name){
		
		var bc;
		
		$(gsdivname + ' .gs_hcpbutton').each(function(x){
			if($(this).attr('name') == name){
				$(this).addClass('selected');
				bc = $(this).attr('text');	
			}else{
				$(this).removeClass('selected');
			}
		})
		
		savedinfo['hcpaction'] = bc;
		
		$(gsdivname + ' .gs_breadcrumb.inshcp').html(bc).addClass('bg');
		$(gsdivname + ' .gs_submit_button').css('display','block').animate({opacity: 1}, 300);
	}
	

	// Open more info panel
	function openMoreInfo(){
		$(gsdivname + ' .gs_moreinfo').addClass('open').animate({height: 240,width:540}, 300);
		$(gsdivname + ' .gs_moreinfo .gs_moreinfo_text').animate({left: 0}, 300);
		$(gsdivname + ' .gs_moreinfo .gs_moreinfo_close').animate({bottom: 0}, 300);
	}
	
	// Close more info panel
	function closeMoreInfo(){
		$(gsdivname + ' .gs_moreinfo').removeClass('open').animate({height: 20,width:20}, 300);
		$(gsdivname + ' .gs_moreinfo .gs_moreinfo_text').animate({left: -400}, 300);
		$(gsdivname + ' .gs_moreinfo .gs_moreinfo_close').animate({bottom: 50}, 300);
	}
	
	
	
	function submit(){
		
		var segment;
		
		
		
		if(   (savedinfo['who'] == "Need Help Paying" && savedinfo['insured?'] == "Insured") ||  (savedinfo['who'] == "Health Care Professional" && savedinfo['hcpaction'] == "Co-pay Support")   ){
			
				
			segment = "1";
		}
		
		//Added need to chage insured ||  (savedinfo['who'] == "Health Care Professional" && savedinfo['hcpaction'] == "Co-pay Support")
		if(   (savedinfo['who'] == "Need Help Paying") && ((savedinfo['insured?'] == "Medicare") || (savedinfo['insured?'] == "Medicaid" ))){
			segment = "4";
		}
		
		
		
		if(   (savedinfo['who'] == "Need Help Paying" && savedinfo['insured?'] == "Uninsured") ||  (savedinfo['who'] == "Health Care Professional" && savedinfo['hcpaction'] == "Medicines at No Cost")   ){
			segment = "2";
		}
		
		if(   savedinfo['who'] == "Health Care Professional" && savedinfo['hcpaction'] == "Reimbursement Forms"   ){
			segment = "3";
		}
		
		//alert(savedinfo['who'] + "    " + savedinfo['insured?'] + "    " + savedinfo['hcpaction'] + " " + segment);
		
		$('.gs_page .endscreen').each(function(x){
			//alert(savedinfo['name'] + " -----  "  + $(this).attr('prod'));
			if(   (savedinfo['name'] == $(this).attr('prod')) && (segment == $(this).attr('segment'))    ){
				$(this).removeClass('hidden');
			}else{
				$(this).addClass('hidden');
			}
		
		})
		changePage('info');
	
	}
	
	
		  
		  // Bind an event to window.onhashchange that, when the hash changes, gets the
		  // hash and adds the class "selected" to any matching nav link.
		  $(window).hashchange( function(){
		    var hash = location.hash;
		    
		    // Set the page title based on the hash.
		   // document.title = 'The hash is ' + ( hash.replace( /^#/, '' ) || 'blank' ) + '.';
		   
		   //check if hash tag exists in the URL
			if(window.location.hash) {
			
			 //set the value as a variable, and remove the #
			 var hash_value = window.location.hash.replace('#', '');
			// alert(hash_value); 
			 //show the value with an alert pop-up
			  //alert(hash_value + ":::" + step_name[curr_step]);
			 //alert(step_name[curr_step-1]);
			// alert(step_name[curr_step]);
			  if (hash_value<step_name[curr_step] && step_name[curr_step]==2)
			  {
			  	
			  	reset_who_buttons();
			  	changePage('who');
			  }
			  else if (hash_value<step_name[curr_step] && step_name[curr_step]==3)
			  {
			  	reset_ins_buttons();
			  	if(savedinfo['who'] == 'Need Help Paying'){
				changePage('insurance');
				}else{
				changePage('looking');
				}
				

			  }
			else if (hash_value=="")
			{
				
				reset_med_buttons();
			   changePage('home');
			}			 
			}
			else
			{
			   reset_med_buttons();
			   changePage('home');
			}
		    
		    // Iterate over all nav links, setting the "selected" class as-appropriate.
		    $('#nav a').each(function(){
		      var that = $(this);
		      
		     // that[ that.attr( 'href' ) === hash ? 'addClass' : 'removeClass' ]( 'selected' );
		    });
		  })
		  
		  // Since the event is only triggered when the hash changes, we need to trigger
		  // the event now, to handle the hash the page may have loaded with.
		  $(window).hashchange();
		  
		
	

})(jQuery);




