document.write('<script type="text/javascript" src="/sites/all/themes/wordstream/jquery.cookie.js" ></script>');
//var WS_SERVER = 'https://my.wordstream.com'
var WS_SERVER = 'https://dev-demo-auth.wordstream.com'

$(document).ready(function() {
	
	$('#sign_out').click(function() {
		$.getJSON(WS_SERVER+'/sso/logout?cb=?', function(v) {
			window.location = '/logout';
		});
	})

	$('#verify_link').click(function() {
		$.getJSON(WS_SERVER+'/sso/resend_user_activation_email_jsonp?cb=?', function(r) {
			if (r['code'] == 'OK')
			{
				window.location = '/registration-email-confirm';
			}
			else
			{
				window.location = '/my-account';
			}
		});
	})

	$('.underline_link').mouseover(function() {
		$(this).css({'text-decoration': 'underline', 'cursor': 'pointer'});
	}) .mouseout(function() {
		$(this).css("text-decoration", "none");
	})

	$.getJSON(WS_SERVER+'/sso/status_jsonp?cb=?', function(r){
		var uri = location.pathname;
		$('#alert').hide();
		$('#newTopicImg').hide();
		$('#ReplyImg').hide();
		if (r['code'] == 'OK')  //Case Logged In
		{
			$('#forum-login').hide();
			$('#newTopicImg').show();
			$('#ReplyImg').show();
			
			if(stristr(uri, 'comment')){
				$('#ReplyImg').hide();
				$('#post').hide();
			}
			
			$.ajax({
				type: "POST",
				url: "/display_name_check.php",
				data: "name="+r['user'],
				success: function(msg){
					str = jQuery.trim(msg);
					if(str == 'true'){
						$('#alert').hide();
					}else{
						$('#alert').show();
						$('#newTopicImg').hide();
						$('#ReplyImg').hide();
						if(uri == '/forum'){
							$('#comment-form').hide();
						}
					}
				}
			
			});
			
			
				//Update Cookie Value
				var curr_status = $.cookie('sso_s');
				if (curr_status == 'unverified' && r['status'] == 'verified')
				{
					$.cookie('sso_s', 'verified', {domain: '.wordstream.com', expires: 30});
				}

				//SSO Display
				$('#text_links').html('<a href="http://my.wordstream.com">Client Login</a>');
				$('#text_links').show();
				$('#logged_in_info_area_user').empty();
				$('#logged_in_info_area_user').append(r['user']);
				$('#register_nav').hide();
				$('#login_nav a').html('My Account') .attr({href: '/my-account', title: 'WordStream Keyword Management Customer Account'});
				$('#logged_in_info_area').show();
				
				
				if ($.cookie('sso_s') == 'unverified')
				{
					$('#account_status_area').show();
				}
				switch(uri)
				{
					case '/affiliate-ppc-free-trial':  
					case '/affiliate-seo-free-trial':
						$.getJSON(WS_SERVER+'/sso/logout?cb=?' , function(v){
							$('#register_nav').show();
							$('#login_nav a').html('Login') .attr({href: '/login', title: 'WordStream Keyword Management Customer Login'});
						});
						break;
					case '/register':
					case '/register-free-keyword-tool':
					case '/register-niche-finder':
					case '/register-keyword-grouper':
					case '/keyword-tool-api':
						$('#register_section').fadeTo('slow', 0.7);
						$('input').attr('disabled', 'disabled');	
						$('#register_logged_in_message').empty() .append('<ul><li>You are currently logged in as ' + r['user'] + '. <a href="/my-account">Click here to manage your account</a>.</li><li>You may also <a href="/sign-out">logout</a> to register a new account.</li></ul>') .show();
						break;
					case '/login':
						window.location = '/my-account';
						break;
					case '/widget-reporting-login':
						window.location = 'http://widget.wordstream.com/reporting';
						break;
					case '/widget-register':
						window.location = '/widget-generator';
						break;
					case '/sign-out':  
						$.getJSON(WS_SERVER+'/sso/logout?cb=?' , function(v){
							$('#logged_in_info_area_user').empty();
							$('#logged_in_info_area').hide();
							$('#account_status_area').hide();
							$('#register_nav').show();
							$('#login_nav a').html('Login') .attr({href: '/login', title: 'WordStream Keyword Management Customer Login'});
						});
						break;
					case '/ppc-free-trial':
						$.getJSON(WS_SERVER+'/signup_rpc/get_profile_type_jsonp?cb=?', 
								function(v) {
									$('#agreement2').attr('disabled', '');	
									$('#existing_signup_btn').attr('disabled', '');	
									if (v['code']=='OK') {
										var profile_types = v['types'];
										for (var i=0; i < profile_types.length; i++)
										{
											if (profile_types[i] == 'ppc')
											{
												$('#ppc_signup_message').hide();
												$('#ppc_signup_message_existing').show();
												$('#ppc_login_link').attr('href', WS_SERVER + '/explorer');	
												$('#agreement2').attr('disabled', 'disabled');	
												$('#existing_signup_btn').attr('disabled', 'disabled');	
												$('#signup_ppc_agreement').fadeTo('slow', 0.7);
												break;
											} 
										}
									} 
								});
						$('#ppc_signup_user').append(r['user']);
						$('#new_signup').hide();
						$('#existing_signup').show();
						break;
					case '/seo-free-trial':
						$('#seo_signup_welcome').show();
						$('#seo_signup_user').append(r['user']);
						$('#step1').hide();
				  	$('.new_signup_buttons').hide();
						$('#step2').show();
						$('#step2_bar').css({background: "url('/sites/all/themes/wordstream/src_12_01_09/img/columnHeader_step01_existing_user.png') no-repeat"});
						$('#step3_bar').css({background: "url('/sites/all/themes/wordstream/src_12_01_09/img/columnHeader_step02_existing_user.png') no-repeat"});
						$('.existing_signup_buttons').show();
						break;
					case '/my-account':
						$('#text_links').html('');
						$.getJSON(WS_SERVER+'/signup_rpc/get_profile_type_jsonp?cb=?', 
								function(v) {
									$('.ppc_link').attr('href','/ppc-free-trial');
									$('.seo_link').attr('href','/seo-free-trial');
									if (v['code']=='OK') {
										var profile_types = v['types'];
										for (var i=0; i < profile_types.length; i++)
										{
											if (profile_types[i] == 'ppc')
											{
												$('.ppc_link').attr('href', WS_SERVER + '/explorer');					
											} 
											else if (profile_types[i] == 'seo')
											{
												$('.seo_link').attr('href', WS_SERVER + '/explorer');	
											}
										}
									} 
								});
						break;
					case '/change-password':
						$('#page').show();
						break;
					case '/try':
					case '/try2':
					case '/try-2':
						$('#step1').hide();
						$('#step2').show();
						$.getJSON(WS_SERVER+'/signup_rpc/get_profile_type_jsonp?cb=?', 
							function(v) {
								if (v['code']=='OK') 
								{
									var profile_types = v['types'];
									for (var i=0; i < profile_types.length; i++)
									{
										if (profile_types[i] == 'ppc')
										{
											$('#ppc_prod').attr('disabled','disabled');
											$('#ppc_prod').parent('div').fadeTo('fast', 0.3);
											$('#error_area2').empty();
											$('#error_area2').append("<ul><li>You already have a WordStream for PPC account, please click <a href='" + WS_SERVER + "/explorer' class='underline_link'>here</a> to login.</li><li>You can also try our WordStream for SEO.</li></ul>");
											$('#error_area2').show();
											break;
										} 
								}
							} 
						});
						break;
					case '/forgot-password':
						$('#forgot_password_logged_in_message').empty() .append('You are currently logged in as ' + r['user'] + '<ul><li><a href="/my-account">Click here to manage your account</a>.</li><li><a href="/change-password">Click here to change your password</a>.</li><li>You may also <a href="/sign-out">logout</a> to access this page.</li></ul>') .show();
						$('#uname').attr('disabled', 'disabled');	
						$('#btn').attr('disabled', 'disabled');	
						$('#forgot_password').fadeTo('slow', 0.7);
						break;
				}
				
		}
		else  //Case Not Logged In
		{
				if(uri == '/forum'){
					$('#comment-form').hide();
				}
				$("div[id^='links-forum-inner']").hide()

				//SSO Not Display
				$('#logged_in_info_area').hide();
				$('#register_nav').show();
				$('#text_links').show();
				switch(uri)
				{
					case '/login':
						$('#page').show();
						break;
					case '/widget-reporting-login':
						$('#page').show();
						break;
					case '/ppc-free-trial':
						$('#ppc_signup_user').empty();
						$('#existing_signup').hide();
						$('#new_signup').show();
						break;
					case '/seo-free-trial':
						$('#step1').show();
						$('#step2').hide();
						break;
					case '/my-account':
						window.location = '/login';
						break;
					case '/change-password':
						window.location = '/login';
						break;
					case '/try':
					case '/try-2':
					case '/try2':
						$('#step2').hide();
						$('#step1').show();
						break;
				}
		}
	});
})
	
	
function stristr (haystack, needle, bool) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfxied by: Onno Marsman
    // *     example 1: stristr('Kevin van Zonneveld', 'Van');
    // *     returns 1: 'van Zonneveld'
    // *     example 2: stristr('Kevin van Zonneveld', 'VAN', true);
    // *     returns 2: 'Kevin '

    var pos = 0;

    haystack += '';
    pos = haystack.toLowerCase().indexOf( (needle+'').toLowerCase() );
    if (pos == -1){
        return false;
    } else{
        if (bool) {
            return haystack.substr( 0, pos );
        } else{
            return haystack.slice( pos );
        }
    }
}

