/**
 * jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2007-2008 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 9/11/2008
 * @author Ariel Flesler
 * @version 1.4
 *
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
 */
;(function(h){var m=h.scrollTo=function(b,c,g){h(window).scrollTo(b,c,g)};m.defaults={axis:'y',duration:1};m.window=function(b){return h(window).scrollable()};h.fn.scrollable=function(){return this.map(function(){var b=this.parentWindow||this.defaultView,c=this.nodeName=='#document'?b.frameElement||b:this,g=c.contentDocument||(c.contentWindow||c).document,i=c.setInterval;return c.nodeName=='IFRAME'||i&&h.browser.safari?g.body:i?g.documentElement:this})};h.fn.scrollTo=function(r,j,a){if(typeof j=='object'){a=j;j=0}if(typeof a=='function')a={onAfter:a};a=h.extend({},m.defaults,a);j=j||a.speed||a.duration;a.queue=a.queue&&a.axis.length>1;if(a.queue)j/=2;a.offset=n(a.offset);a.over=n(a.over);return this.scrollable().each(function(){var k=this,o=h(k),d=r,l,e={},p=o.is('html,body');switch(typeof d){case'number':case'string':if(/^([+-]=)?\d+(px)?$/.test(d)){d=n(d);break}d=h(d,this);case'object':if(d.is||d.style)l=(d=h(d)).offset()}h.each(a.axis.split(''),function(b,c){var g=c=='x'?'Left':'Top',i=g.toLowerCase(),f='scroll'+g,s=k[f],t=c=='x'?'Width':'Height',v=t.toLowerCase();if(l){e[f]=l[i]+(p?0:s-o.offset()[i]);if(a.margin){e[f]-=parseInt(d.css('margin'+g))||0;e[f]-=parseInt(d.css('border'+g+'Width'))||0}e[f]+=a.offset[i]||0;if(a.over[i])e[f]+=d[v]()*a.over[i]}else e[f]=d[i];if(/^\d+$/.test(e[f]))e[f]=e[f]<=0?0:Math.min(e[f],u(t));if(!b&&a.queue){if(s!=e[f])q(a.onAfterFirst);delete e[f]}});q(a.onAfter);function q(b){o.animate(e,j,a.easing,b&&function(){b.call(this,r,a)})};function u(b){var c='scroll'+b,g=k.ownerDocument;return p?Math.max(g.documentElement[c],g.body[c]):k[c]}}).end()};function n(b){return typeof b=='object'?b:{top:b,left:b}}})(jQuery);


//no conflict
jQuery.noConflict();

jQuery(document).ready(function($){
	
	// walkthrough load effect
	$("#walkthrough a").click(function(){
		if (!$(this).hasClass('current')) {
			var show = $(this).attr('href');
			$(this).parent().siblings().children().attr('class', '');
			$(this).attr('class', 'current');
   			$("#panel-holder").fadeTo(300, 0, function() {
				$('#panel-holder').children().attr('class', 'hide');
				$(show).attr('class', 'panel');
			}).fadeTo(300, 0).fadeTo(300, 1);
		}
		return false;
	});
	
	// walkthrough load from button within main area
	$("#main a.white-button").click(function(){
		var show = $(this).attr('href');
		// added this so that the last button just goes to the login page
		if (show.match("expert-login"))
		{
			return true;
		}
		$('#walkthrough a').each(function(){
			var href = $(this).attr('href');
			if(show==href){
				$(this).attr('class', 'current');
			} else {
				$(this).attr('class', '');
			}
		});
   		$("#panel-holder").fadeTo(300, 0, function() {
		     $('#panel-holder').children().attr('class', 'hide');
		     $(show).attr('class', 'panel');
		}).fadeTo(300, 0).fadeTo(300, 1); 
		return false;
	});
	
	// terms and privacy scroll effect
	$("#terms-page a").click(function() {
		var scroll = $(this).attr('href');
		$.scrollTo( scroll, 800 )
		return false;
	});
	
	// video radio group toggle visual effect for expert broadcast area
	$("#video-toggles input:radio").click(function(){
		if ($(this+':checked')) {
			var radiovalue = $(this).val();
			if (!radiovalue.match("private"))
			{
				$(this).parent().parent().attr('class', radiovalue).next().children().attr('class', radiovalue);
			}
		}
	});
	
	// expert broadcast load new pages
	$('#chatbox-tabs a').click(function(){	
   		var toLoad = $(this).attr('href');
   		var isCurrent = $(this).attr('class');
    		if (toLoad != '' && !isCurrent) {
   			$("#chatbox-area-wrap").children().slideUp(300,function() {
   				$("#chatbox-area-wrap").prev().hide().load(toLoad,{}, function() { 
   					innerAjaxLinks();
					// NEW
					if (toLoad.match("expert-widget-info"))
					{
						$(".help").html('Expert Widget Setup').attr('href','help-expert-setup.php#expert-widget-info');
					}
					else if (toLoad.match("expert-report.php"))
					{
						$(".help").html('My Account Balance').attr('href','help-report.php#expert-report');
					}
					else if (toLoad.match("expert-payinfo"))
					{
						$(".help").html('Account Information').attr('href','help-account-info.php#expert-payinfo');
					}
					else
					{
						$(".help").html('Getting Started').attr('href','help-getting-started.php#getting-started');
					}
					helpLink();
   					// END NEW
   					$(this).slideDown(300, 0);
   				});
   			});
   			$('#chatbox-tabs a').attr('class', '');
   			$(this).attr('class', 'current');
		} else if (toLoad == '' && !isCurrent) {
   			$("#ajax-load").slideUp(300, function() {
   				$(this).next().children().slideDown(300, 0);
  				// NEW
				$(".help").html('Getting Started').attr('href','help-getting-started.php#getting-started');
				// END NEW
   			});
   			$('#chatbox-tabs a').attr('class', '');
   			$(this).attr('class', 'current');
   		}
   		return false;	 
   	});
   	function innerAjaxLinks() { 
   		$('#ajax-sub-menu a').click(function(){
				var thisHref = $(this).attr("href");
   				$("#ajax-load").hide(1,function(){
   					$("#ajax-load").load(thisHref,{},function(){
    						innerAjaxLinks();
//				   		sendMailAjax();
						// NEW
						// these help sections line up with the inner expert pages
						if (thisHref.match("expert-widget-info"))
						{
							$(".help").html('Expert Widget Setup').attr('href','help-expert-setup.php#expert-widget-info');
						}
						else if (thisHref.match("expert-widget-pic"))
						{
							$(".help").html('Expert Widget Setup').attr('href','help-expert-setup.php#expert-widget-info');
						}
						else if (thisHref.match("expert-view-widget"))
						{
							$(".help").html('Expert Widget Setup').attr('href','help-expert-setup.php#expert-widget-info');
						}
						else if (thisHref.match("expert-hosted-page-setup"))
						{
							$(".help").html('Expert Hosted Page').attr('href','help-hosted-page.php#expert-hosted-page-setup');
						}

						else if (thisHref.match("expert-report.php"))
						{
							$(".help").html('My Account Balance').attr('href','help-report.php#expert-report');
						}
						else if (thisHref.match("expert-trans-details"))
						{
							$(".help").html('Transaction Details').attr('href','help-trans-details.php#expert-trans-details');
						}
						else if (thisHref.match("expert-report-activity"))
						{
							$(".help").html('Activity Report').attr('href','help-report-activity.php#expert-report-activity');
						}
						else if (thisHref.match("expert-report-visitors"))
						{
							$(".help").html('Visitors Report').attr('href','help-report-visitors.php#expert-report-visitors');
						}
						else if (thisHref.match("expert-send-email"))
						{
							$(".help").html('Send Email').attr('href','help-send-email.php#expert-send-email');
						}
						else if (thisHref.match("expert-call-client"))
						{
							$(".help").html('Call Client').attr('href','help-call-client.php#expert-call-client');
						}

						else if (thisHref.match("expert-payinfo"))
						{
							$(".help").html('Account Information').attr('href','help-account-info.php#expert-payinfo');
						}
						else if (thisHref.match("expert-password"))
						{
							$(".help").html('Account Information').attr('href','help-account-info.php#expert-password');
						}

						else
						{
							$(".help").html('Getting Started').attr('href','help-getting-started.php#getting-started');
						}
						helpLink();
						// END NEW
				   		$("#ajax-load").slideDown(300);
				   	});
   				});
   				return false; 
    		});
   	};

	// look up name for send mail
	// no longer used - tm
   	function sendMailAjax() {
   		$("#part2").hide();
  	  	$("#look-up-name").click(function(){
  	  		$("#part1").hide().next().show();
  	  		return false;
  	  	});
 	};

	// NEW
	// expert help link
	$("#overlay-wrap").hide().removeClass('hide');
	$("#help-content").hide();
	function helpLink() {
		$("a.help").click(function(){
			// var helpLink = $(this).attr('href');
			// var toLoad = 'help.php';
			// ie help-expert-setup.php#set-pic
			var myString = $(this).attr('href');
			var mySplitResult = myString.split("#");
			var toLoad = mySplitResult[0];
			var helpLink = '#' + mySplitResult[1];

			$("#help-content").stop().load(toLoad,'',function(){
				$(helpLink).addClass('current-help');
				$(this).prepend('<p><a class="close-help">&laquo; back to <strong>SpeakToMe</strong></a></p>').parent().fadeIn(300,function(){ 
					$("select").hide(); // for stupid ie6
					$("#help-content").fadeIn(300); 
				});
				closeHelpLink();
			});
			return false;
		});
	} helpLink();
	function closeHelpLink() {
		$(".close-help").click(function(){
			$("#help-content").stop().fadeOut(300,function(){ 
				$("select").show(); // for stupid ie6
				$("#overlay-wrap").fadeOut(300); 
			});
			return false;
		});
   	}
	// END NEW

	$("#expert-payinfo-form-submit").click(function()
	{
		var hasError = false;

		var FullNameval = $("#FullName").val();
		var CompanyNameval = $("#CompanyName").val();
		var Address1val = $("#Address1").val();
		var Address2val = $("#Address2").val();
		var Cityval = $("#City").val();
		var Stateval = $("#State").val();
		var Postalcodeval = $("#Postalcode").val();
		var Countryval = $("#Country").val();
		var Receiverval = $("#Receiver").val();

		if (hasError == false)
		{
			$.post("expert-payinfo_chk.php", { FullName: FullNameval, CompanyName: CompanyNameval, Address1: Address1val, Address2: Address2val, City: Cityval, State: Stateval, Postalcode: Postalcodeval, Country: Countryval }, function(data) {
				$("#status").html(data);
			});
		}
		return false;
	});

	$("#expert-password-form-submit").click(function()
	{
		var hasError = false;
		var pass1val = $("#pass1").val();
		var pass2val = $("#pass2").val();
		
	    if (pass1val != pass2val)
		{
			$("#status").html("Error: Passwords do not match, please re-enter.");
			hasError = true;
	    }
		else if (pass1val.length > 16 || pass1val.length < 5)
		{
			$("#status").html("Error: Password must be 5-16 chars.");
			hasError = true;
	    }

		if (hasError == false)
		{
			$.post("expert-password_chk.php", { pass1: pass1val }, function(data) {
				$("#status").html(data);
			});
		}
		return false;
	});

	$("#expert-send-email-part1-submit").click(function()
	{
		var clientnameval = $("#clientname").val();

		// just reload this sub page with the clientname as an arg
		var thisHref = "expert-send-email.php?clientname=" + escape(clientnameval);
		$("#ajax-load").hide(1,function(){
			$("#ajax-load").load(thisHref,{},function(){
				innerAjaxLinks();
				$("#ajax-load").slideDown(300);
			});
		});
		return false;
	});
	
	$("#expert-call-client-part1-submit").click(function()
	{
		var clientnameval = $("#clientname").val();

		// just reload this sub page with the clientname as an arg
		var thisHref = "expert-call-client.php?clientname=" + escape(clientnameval);
		$("#ajax-load").hide(1,function(){
			$("#ajax-load").load(thisHref,{},function(){
				innerAjaxLinks();
				$("#ajax-load").slideDown(300);
			});
		});
		return false;
	});
	
	$("#expert-send-email-part2-submit").click(function()
	{
		var hasError = false;
		var clientnameval = $("#clientname").val();
		var clientemailval = $("#clientemail").val();
		var subjectval = $("#subject").val();
		var messageval = $("#mail_message").val();
		
	    if (subjectval.length > 100)
		{
			$("#status").html("Error: Subject is limited to 100 characters.");
			hasError = true;
	    }
		else if (messageval.length > 2000)
		{
			$("#status").html("Error: Message is limited to 2000 characters.");
			hasError = true;
	    }

		if (hasError == false)
		{
			$.post("expert-send-email_chk.php", { clientname: clientnameval, clientemail: clientemailval, subject: subjectval, mail_message: messageval }, function(data) {
				$("#status").html(data);
			});
		}
		return false;
	});

	$("#expert-widget-info-form-submit").click(function()
	{
		var nicenameval = $("#nicename").val();
		var emailval = $("#email").val();
		var phoneval = $("#phone").val();
		var rateval = $("#rate").val();

		var hasError = false;

		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		if (!emailReg.test(emailval))
		{
			$("#status").html("Error: Please enter a valid email address.");
			hasError = true;
		}
		
		var strphone = "";
	    for (var i = 0; i < phoneval.length; i++)
		{
	        oneChar = phoneval.charAt(i).charCodeAt(0);
	        if (oneChar > 47 && oneChar < 58)
			{
				strphone = strphone + phoneval.charAt(i);
			}
		}
		if (strphone.length < 11)
		{
			$("#status").html("Error: Phone number must be in international format, ie. 1-212-555-1212.");
			hasError = true;
	    }

		var rate = rateval + 0;
		if (rate < 0.25)
		{
			$("#status").html("Error: Rate per minute must be at least $0.25.");
			hasError = true;
	    }

		if (hasError == false)
		{
			$.post("expert-widget-info_chk.php", { nicename: nicenameval, email: emailval, phone: phoneval, rate: rateval }, function(data) {
				$("#status").html(data);
			});
		}
		return false;
	});

	$("#expert-hosted-page-setup-submit").click(function()
	{
		var hasError = false;

		var Title1val = $("#Title1").val();
		var Title1Linkval = $("#Title1Link").val();
		var Title2val = $("#Title2").val();
		var IdleTextval = $("#IdleText").val();
		var ActiveTextval = $("#ActiveText").val();

		if (hasError == false)
		{
			$.post("expert-hosted-page-setup_chk.php", { Title1: Title1val, Title1Link: Title1Linkval, Title2: Title2val, IdleText: IdleTextval, ActiveText: ActiveTextval }, function(data) {
				$("#status").html(data);
			});
		}
		return false;
	});

	$("#expert-report-form-submit").click(function()
	{
		var timeframeval = $("#timeframe").val();
		var thisHref = "expert-report.php?timeframe=" + timeframeval;
		$("#ajax-load").hide(1,function(){
			$("#ajax-load").load(thisHref,{},function(){
				innerAjaxLinks();
				$("#ajax-load").slideDown(300);
			});
		});
		return false;
	});

	$("#expert-report-activity-form-submit").click(function()
	{
		var timeframeval = $("#timeframe").val();
		var thisHref = "expert-report-activity.php?timeframe=" + timeframeval;
		$("#ajax-load").hide(1,function(){
			$("#ajax-load").load(thisHref,{},function(){
				innerAjaxLinks();
				$("#ajax-load").slideDown(300);
			});
		});
		return false;
	});

	$("#ajax-load-url a").click(function()
	{
		var thisHref = $(this).attr('href');
		$("#ajax-load").hide(1,function(){
			$("#ajax-load").load(thisHref,{},function(){
				innerAjaxLinks();
				if (thisHref.match("expert-trans-details"))
				{
					$(".help").html('Transaction Details').attr('href','help-trans-details.php#expert-trans-details');
				}
				else if (thisHref.match("expert-send-email"))
				{
					$(".help").html('Send Email').attr('href','help-send-email.php#expert-send-email');
				}
				else if (thisHref.match("expert-call-client"))
				{
					$(".help").html('Call Client').attr('href','help-call-client.php#expert-call-client');
				}
				$("#ajax-load").slideDown(300);
			});
		});
		return false;
	});
	
	$("#expert-trans-details-submit").click(function()
	{
		var tran_idval = $("#tran_id").val();
		var thisHref = "expert-trans-details.php?tran_id=" + tran_idval;
		$("#ajax-load").hide(1,function(){
			$("#ajax-load").load(thisHref,{},function(){
				innerAjaxLinks();
				$("#ajax-load").slideDown(300);
			});
		});
		return false;
	});

	$("#expert-trans-details-reversal").click(function()
	{
		var tran_idval = $("#tran_id").val();
		$.post("expert-trans-details-reimburse.php", { tran_id: tran_idval }, function(data) {
			// send user back to expert-report page
			var thisHref = "expert-report.php?error_msg=" + escape(data);
			$("#ajax-load").hide(1,function(){
				$("#ajax-load").load(thisHref,{},function(){
					innerAjaxLinks();
					$("#ajax-load").slideDown(300);
				});
			});
		});
		return false;
	});

	$("#expert-trans-details-reversal-cancel").click(function()
	{
		var tran_idval = $("#tran_id").val();
		var thisHref = "expert-trans-details.php?tran_id=" + tran_idval;
		$("#ajax-load").hide(1,function(){
			$("#ajax-load").load(thisHref,{},function(){
				innerAjaxLinks();
				$("#ajax-load").slideDown(300);
			});
		});
		return false;
	});

	$("#expert-report-visitors-form-submit").click(function()
	{
		var timeframeval = $("#timeframe").val();
		var chattypeval = $("#chattype").val();
		var thisHref = "expert-report-visitors.php?timeframe=" + timeframeval + "&chattype=" + chattypeval;
		$("#ajax-load").hide(1,function(){
			$("#ajax-load").load(thisHref,{},function(){
				innerAjaxLinks();
				$("#ajax-load").slideDown(300);
			});
		});
		return false;
	});

});

