
var Arise = {
	
	dropdown_duration : 250,
	logged_in: null,
	search_field : null,
//	search_int: null,
	slideshow_count : 0,
	slideshow_int: null,
	strip_x : {},
	wps_tab_closed : true,
	init : function()
	{
		$('#wps_tab').animate({"top": "+=210px"}, "medium");
	    
	//	$('a.secure').bind('click', Arise.check_login);
		$('a.expand').bind('click', Arise.expand);
		$('#wps_tab').bind('click', Arise.toggle_wps_tab);
		$('#coverage').bind('change', Arise.toggle_spouse)
						.bind('change', Arise.toggle_children);
		
		$('#search').bind('focus', Arise.search.clear_input)
					.bind('blur', Arise.search.check_input)
					.bind('keyup', Arise.search.get_results);
		
		Arise.slideshow_int = setInterval(Arise.next_slide,10000);
		
		//start the first slide sooner for a more dynamic experience!
		setTimeout(Arise.next_slide,2500);
		
		Arise.logged_in = false;
		
		$("a[href^='#'].formularyscroller").live('click',Arise.scroll_anchors);
		// $('input.datepicker').datepicker();
		
		$('a.qiprev,a.qinext').each(function(index) {
			$(this).bind('click', Arise.slide_qi);
		});
		$('.smtabs a').each(function(index) {
			$(this).bind('click', Arise.swap_sm);
		});
		
		$('#visitor_provider_search').bind('click', Arise.visitor_provider_search);
		
		$('#register3 #password_field').bind('keyup', Arise.checkcomplexity);
		$('#reset #password_field').bind('keyup', Arise.checkcomplexity);
		$('#reset #adm_password_field').bind('keyup', Arise.checkcomplexity2);
		
		//if this is the clear claims connection page, auto-submit the form.
		if ($('#cccform').length){ $('#cccform').submit(); }
        
        Arise.insert_file_images_for_links();

		if ($('#providersearch_form img[title]').length){ $('#providersearch_form img[title]').tooltip().dynamic();}
		
		if ($('#providersearch_form .got-a-tip').length){ $('#providersearch_form .got-a-tip').tooltip().dynamic();}
		
		$('.self_mgmt_track').each(function(idx) {
			var category = 'Self-Management Tools';
			var action = $(this).find('a.expand').text();
			$(this).find('.faq_answer a').bind('click',{cat:category,act:action},Arise.recordOutboundLink);
		});

	},
    

	recordOutboundLink : function(event) {
		if (typeof _gaq != 'undefined')
		{
			var category = event.data.cat || '';
			var action = event.data.act || '';
			var label = this.href;
			_gaq.push(['_trackEvent', category, action, label]);
		}
		setTimeout('location.href="'+this.href+'"',200);
		return false;
	  },


    insert_file_images_for_links : function()
    {
        //use class="noimage" to exclude your link from recieving an image
        //get all links inside right side of page. 
        $('#R a:not(.noimage)').not('a:has(img)').not('.pdfdoc').each( function() {
            var url = $(this).attr('href');
            var extention = url.substr(url.length-4);
                        
            //determine if ends in .pdf , .doc or other file
            //documents needed differnt spacing to line up
            if(extention == '.pdf') {
                
                $(this).prepend('<img src="/images/interface/pdfpreview.jpg" />');
            }
            if(extention == '.doc') {
                $(this).prepend('&nbsp;<img src="/images/interface/icons/doc.png" />&nbsp;&nbsp;');
            }
            if(extention == '.xls') {
                $(this).prepend('<img src="/images/interface/icons/excel-16.gif">&nbsp;&nbsp;');
            }
        });
        
        
    },
	
	show_service_area : function(county)
	{
		var params = { 'county':county };
		$.post(
			'/ajax/get_service_area',
			params,
			function(resp)
			{
				$('#countylisting').empty();
				$('#countylisting').append(resp);
			}
		);
	}, 
	
	
	checkcomplexity : function(el)
	{
		var pwval = $(this).val();

		if (pwval && pwval.match(/[a-zA-Z]/))
			$('#pw_ltr').addClass('isGood');
		else
			$('#pw_ltr').removeClass('isGood');

		if (pwval && pwval.match(/[a-z]/))
			$('#pw_lc').addClass('isGood');
		else
			$('#pw_lc').removeClass('isGood');

		if (pwval && pwval.match(/[A-Z]/))
			$('#pw_uc').addClass('isGood');
		else
			$('#pw_uc').removeClass('isGood');
		
		if (pwval && pwval.match(/[0-9]/))
			$('#pw_num').addClass('isGood');
		else
			$('#pw_num').removeClass('isGood');
				
		if (pwval && pwval.match(/[^a-zA-Z0-9]/))
			$('#pw_sp').addClass('isGood');
		else
			$('#pw_sp').removeClass('isGood');
		
	},
	
	//Administrators have stricter rules
	checkcomplexity2 : function(el)
	{
		var pwval = $(this).val();

		if (pwval && pwval.match(/[a-z]/g) && pwval.match(/[a-z]/g).length > 1)
			$('#pw_lc').addClass('isGood');
		else
			$('#pw_lc').removeClass('isGood');

		if (pwval && pwval.match(/[A-Z]/g) && pwval.match(/[A-Z]/g).length > 1)
			$('#pw_uc').addClass('isGood');
		else
			$('#pw_uc').removeClass('isGood');
		
		if (pwval && pwval.match(/[0-9]/g) && pwval.match(/[0-9]/g).length > 1)
			$('#pw_num').addClass('isGood');
		else
			$('#pw_num').removeClass('isGood');
				
		if (pwval && pwval.match(/[^a-zA-Z0-9]/g) && pwval.match(/[^a-zA-Z0-9]/g).length > 1)
			$('#pw_sp').addClass('isGood');
		else
			$('#pw_sp').removeClass('isGood');
		
	},

	trigger_survey : function(href){
	    var surv_cookie = $.cookie('members_tools_survey');
	    
	    if (!surv_cookie || surv_cookie.indexOf(href) == '-1') {
	        $.post(
    			'/ajax/popup',
    			{ path: "survey" },
    			function(resp)
    			{
    				var options = {};
    				options.title = 'Consumer Health Tools Usability Test';
    				Util._window(resp,options);

            		$('#survey_submit').bind('click', function(){
            		    Arise.submit_survey(href);
            		});
    			}
    		);
	    }
	    	
    		
	},
	visitor_provider_search : function()
	{
		$.cookie('visitor_provider_search', 1, { expires: 7, path: '/'});
		
	},
	
	slide_qi : function()
	{
		var prev = ($(this).hasClass('qiprev')) ? true:false;
		var rel = $(this).attr('rel');
		if(!Arise.strip_x[rel]) Arise.strip_x[rel] = 0;
		if(prev)
		{
			if(Arise.strip_x[rel] < 0)
			{
				Arise.strip_x[rel] += 300;
				$('#'+rel).stop().animate({ left:Arise.strip_x[rel] }, 1500, 'easeInOutQuint' );
			}
		}
		else
		{
			if(Arise.strip_x[rel] > -(($('#'+rel+' div.box').size()-1)*300))
			{
				Arise.strip_x[rel] -= 300;
				$('#'+rel).stop().animate({ left:Arise.strip_x[rel] }, 1500, 'easeInOutQuint' );
			}
		}
		
		return false;
	},
	
	swap_sm : function()
	{
		var rel = $(this).attr('rel');
		// alert(rel);
		$('.smcategory').hide();
		$('#'+rel).show();
		
		$('a.active').removeClass('active');
		$(this).addClass('active');
		
		return false;
	},
	
	search : {
		
		clear_input : function()
		{
			if(!Arise.search_field) Arise.search_field = $(this);
			
			switch(Arise.search_field.val())
			{
				case 'Search':
					Arise.search_field.val('');
					break;
			}
			Arise.search.get_results();

		},
		
		check_input : function()
		{
			switch(Arise.search_field.val())
			{
				case '':
					Arise.search_field.val('Search');
					break;
			}
			$('#results').fadeOut(250);
		},
		
		get_results : function()
		{
			if(Arise.search_int) clearTimeout(Arise.search_int);
			
			var term = Arise.search_field.val();
			if(term.length>2)
			{
				$('.search_btn_inactive').hide();
				$('.search_btn_active').show();
			}
			else
			{
				$('.search_btn_inactive').show();
				$('.search_btn_active').hide();
			}
		},

		database : function()
		{
			var term = Arise.search_field.val();
			var params = { 'term':term };
			$.post(
				'/ajax/get_search_results',
				params,
				function(resp)
				{
					Arise.search_field.removeClass('loading');
					$('#results').html(resp).fadeIn(250);
				}
			);
		}

	},
	
	expand : function()
	{
		var id = $(this).attr('rel');
		var expandgroup = $(this).attr('expandgroup');
		if (expandgroup)  //if an expandgroup is assigned, then only one item in the expandgroup can be open at a time
		{
			$('a[expandgroup='+expandgroup+']').each(function(i){ 
				var gid = $(this).attr('rel'); 
				if (gid != id)
				{
					if ($(this).hasClass('minus'))
					{
//						setTimeout('$("#expand_'+gid+'").animate({"height": "toggle"}, Arise.dropdown_duration, "easeInOutCubic")',Arise.dropdown_duration/2);
						$('#expand_'+gid).animate({"height": "toggle"}, Arise.dropdown_duration, "easeInOutCubic");
//						$('#expand_'+gid).hide(); 
						$(this).addClass('plus').removeClass('minus');
					}
				}
			});
			
		}
		if($.browser.msie && parseInt(jQuery.browser.version)<7)
		{
			$('#expand_'+id).toggle();
		}
		else
		{
			$('#expand_'+id).animate({"height": "toggle"}, Arise.dropdown_duration, 'easeInOutCubic').delay(1800,"foo");
		}
		if($(this).hasClass('plus'))
		{
			$(this).addClass('minus').removeClass('plus');
		}
		else
		{
			$(this).addClass('plus').removeClass('minus');
		}
		return false;
	},
	
	toggle_spouse : function()
	{
		var val = $(this).val();
		if(val=='applicant_spouse' || val=='family')
		{
			$('#spouse_information').show();
			$('#gender_2').addClass('validate[required]');
			$('#age_2').addClass('validate[required,custom[date]] text-input');
			$('#smoker_2').addClass('validate[required]');
		}
		else
		{
			$('#spouse_information').hide();
			$('#gender_2').removeClass('validate[required]');
			$('#age_2').removeClass('validate[required,custom[date]] text-input');
			$('#smoker_2').removeClass('validate[required]');
		}
	},
	
	toggle_children : function()
	{
		var val = $(this).val();
		if(val=='applicant_children' || val=='family')
		{
			$('#children_information').show();
			$('#num_of_children').addClass('validate[required]');			
		}
		else
		{
			$('#children_information').hide();
			$('#num_of_children').removeClass('validate[required]');			
		}
	},
	
	check_login : function()
	{
		if($.cookie('logged_in') || Arise.logged_in)
		{
			
		}
		else
		{
			var href = $(this).attr('href');
			Arise.prompt_login(href);
			return false;
		}
	},
	
	prompt_login : function(href)
	{
		$.post(
			'/ajax/popup',
			{ path: "agent_login" },
			function(resp)
			{
				var options = {};
				options.title = 'Agent Login';
				Util._window(resp,options);
				
				$('#login_submit').bind('click', { href:href }, Arise.submit_login);
			}
		);
	},
	
	scroll_anchors : function()
	{
	//KNH 12/15 - changing to a scrolling DIV, if this is needing for scrolling elsewhere, then we should create a scroll_window function.	
	//	$.scrollTo($(this).attr('href'), 1000, {easing:'easeInOutCubic'});
		$('div.formularygroup').scrollTo($(this).attr('href'), 1000, {easing:'easeInOutCubic'});
		// alert($(this).attr('href'));
		return false;
	},
	
	submit_login : function(e)
	{
		var u = $('#email_address').val();
		var p = $('#password').val();
		var list_id = $('#list_id').val();
		
		$.post(
			'/ajax/submit_login/',
			{ username:u, password:p, list_id:list_id },
			function(resp)
			{
				var o = $.evalJSON(resp);
				if(parseInt(o.list_id))
				{
					document.location.href = e.data.href;
				}
				else
				{
					$('#errormsg').html('Your credentials were not found in our database. Please try again.').show();
				}
			}
		);

		
	},

	submit_survey : function(href)
	{
	    
		var n = $('#emp_name').val();
		var e = $('#employer').val();
		var font_size = $("input[name='font_size']:checked").val();
		var reading_level = $("input[name='reading_level']:checked").val();
		var content_organization = $("input[name='content_organization']:checked").val();
		var ease_navigation = $("input[name='ease_navigation']:checked").val();
		
		$.post(
			'/ajax/submit_survey/',
			{ href:href,name:n, employer:e, font_size:font_size,reading_level:reading_level, content_organization:content_organization, ease_navigation:ease_navigation },
			function(resp)
			{
				var o = $.evalJSON(resp);
				if(o != 0)
				{
				    var survey_cookie = $.cookie('members_tools_survey');
				    if (survey_cookie) {
				        var value = survey_cookie + ";" + href;
				        $.cookie('members_tools_survey', value, { expires: 120, path: '/'});
				    } else {
				        $.cookie('members_tools_survey', href, { expires: 120, path: '/'});
				    }
					$('.windowbox div').html('Thank you for your feedback!');
				}
				else
				{
					$('#errormsg').html('Please evaluate the tool using each of the 4 categories.').show();
				}
			}
		);

		
	},

	next_slide : function()
	{
		var slides = ['s1','s2','s3','s4'];
		//alert('transition');
		Arise.transition_slideshow(slides[++Arise.slideshow_count%slides.length]);
	},
	
	transition_slideshow : function(area)
	{
		//var area = $(this).attr('rel');
		var btn = $('#'+area+'_btn');
		var img = $('#'+area+'_img');
		
		$('#slideshow').append(img);
		img.fadeIn(600,function()
		{
			$(this).siblings('img').each(function(index) {
				$(this).hide();
			});
		});
	},
	
	toggle_wps_tab : function()
	{
		if(Arise.wps_tab_closed)
		{
		    $('#wps_tab').animate({"top": "-=210px"}, "fast");
			Arise.wps_tab_closed = false;
		}
		else
		{
		    $('#wps_tab').animate({"top": "+=210px"}, "fast");
			Arise.wps_tab_closed = true;
		}
		return false;
	},

	
	validate_form : function(form)
	{
		if(!form) form = document.forms[0];
		var submit_it = true;
		$(form).find(".REQUIRED").each(function(req)
		{
			if(this.value == '')
			{
				$(this).parents("div.formfield").addClass('error');
				submit_it = false;
			}
			else
			{
				$(this).parents("div.formfield").removeClass('error');
			}			
		});
		if( !submit_it ) $('#error_message').fadeIn(250);
		return submit_it;
	}
		
}


$(Arise.init);
//Extend the methods of jQuery

var success_color = '#0C0';
var error_color = '#C00';

$.fn.extend({
	success_fx:function()
	{
		$(this).effect("highlight", {color:success_color}, 1000);
	},
	
	error_fx:function()
	{
		$(this).effect("highlight", {color:error_color}, 1000);
	},
	
	add_fx:function()
	{
		$(this).effect("highlight", {color:success_color}, 1000);
	},
	
	remove_fx:function()
	{
		$(this).css({background:'#C00'}).fadeOut("slow",function(){ $(this).remove() });
	}
});


