$.extend({
     getParam: function() {
    	var vars = [], hash;
	    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	    for(var i = 0; i < hashes.length; i++)
	    {
	        hash = hashes[i].split('=');
	        vars.push(hash[0]);
	        vars[hash[0]] = hash[1];
	    }
	    return vars;
     }
 });

$(document).ready( function() {
	if($.client.os == "Mac") {
		$("#Index").css("background", "url(../images/bg-body-main.jpg) repeat-x scroll 0 109px transparent");	
	}
	
	$("#liveintent_follow_us_btn").click(function(){return false;});
	
	$("#fdbk_tab").click(function(){
		_gaq.push(['_trackEvent', 'feedbackGetSatisfaction-btn', 'clicked']);	
	});
	
	$("#liveintent_follow_us").mouseover(function() {
		_gaq.push(['_trackEvent', 'hdr-tryButtonWindow-btn', 'hover']);
	});
	
	$("#LiveIntentTag .trigger").click(function() {
		var showTag = function () {
			$("#LiveIntentTag .trigger").fadeOut("slow", function(){
				$("#LiveIntentTag .tagTxt").fadeIn("slow");
			});
		};
		$("#LiveIntentTag").animate({
			        width: "915px"
			      }, 600, "linear", showTag() );
	});
	
	$("#LiveIntentTag .btn-close-expand").click(function(){
		var hideTag = function () {
			$("#LiveIntentTag .tagTxt").fadeOut(100, function(){
				$("#LiveIntentTag .trigger").fadeIn("slow");
			});
		};
		$("#LiveIntentTag").animate({
	        width: "150px"
	      }, 1000, "linear", hideTag());
	});
	
	$(".imgBtn").each(function() {
		$(this).qtip({			
			show: {
				solo: true
			},
			content: {
	            text: "<img src='" + $(this).attr('rel') + "' />",
	            title: {
	               text: $(this).attr("title")
	            }
	        },
         	position: {
				corner: { 
					target: 'topMiddle', 
					tooltip: 'bottomMiddle' 
				}
			},
			style: {
				border: {
					width: 5,
					radius: 10
			  	},
			  	padding: 10, 
			  	tip: true, 
			  	name: 'cream',
			  	height: 200,
			  	width: 290
			},
			api: {
				onShow: function () {
					$(".qtip-active").css("z-index", 901);
				}
			}
		});
	});
	
	$("#LoginTrigger").qtip({
		show: {
			when: {
				event: "click"
			}
		},
		hide: 'unfocus', 
		position: {
			corner: { 
				target: 'bottomMiddle', 
				tooltip: 'topRight' 
			}
		},
		style: {
			border: {
				width: 5,
				radius: 10
		  	},
		  	padding: 10, 
		  	tip: true, 
		  	name: 'light'
		  	
		},
		content: $("#LoginContainer").html(),
		api: {
			onShow: function () {
				$(".qtip-active").css("z-index", 901);
			}
		}
	});
	
	
	if ($.browser.msie) {
		$("#Featured").css("width", "931px").css("height", "60px");
	}
	
	// If Carousel Exists
	
	var carouselTiming = {
			speed: 2000,
			timeout: 5000
	};
	if (document.getElementById("HowitworksSection")){ 
			carouselTiming = {
				speed: 1000,
				timeout: 5400
			};
	}
	if (document.getElementById("Testimonial")) {
		$("#Testimonial").cycle({
			speed: carouselTiming.speed,
			timeout:  carouselTiming.timeout,
			random:  1,
			after: function() {
				$("#Testimonial").css("visibility","visible");
			}
		});
	}
	
	if (document.getElementById("PublisherList")) {
		$("#PublisherList").cycle({
			speed: carouselTiming.speed,
			timeout:  carouselTiming.timeout,
			pause:  1,
			random:  1,
			after: function() {
				$("#PublisherList").css("visibility","visible");
			}
		});
	}
	if (document.getElementById("LiveIntentCycle")) {
		$(".tout1").fadeIn("slow");
		setTimeout(function(){$(".tout2").fadeIn("slow");}, 500);
		$("#LiveIntentCycle").cycle({
			pager: '#CycleControl', 
			pagerAnchorBuilder: function(idx, slide) { 
				// return selector string for existing anchor 
				return '#CycleControl li:eq(' + idx + ') a'; 
			},
			speed: carouselTiming.speed,
			timeout:  carouselTiming.timeout,
			pause:  1 
		});
		
		$('#PauseCtrl').click(function() { 
		    $('#LiveIntentCycle').cycle('pause'); 
		    $(this).hide();
		    $("#PlayCtrl").show();
		});
		
		$('#PlayCtrl').click(function() { 
		    $('#LiveIntentCycle').cycle('resume');
		    $(this).hide();
		    $("#PauseCtrl").show();
		});
		
		$("#LiveIntentCycle li").click(function(){
			_gaq.push(['_trackEvent', 'homeWebslideSignup', 'clicked']);
			window.location = "signup.php";
		});
		
		
	}
	
	// If Form Exists 
	
	if (document.getElementById("LiveIntentBetaForm")){
		$("#OptionalFieldTrigger").toggle(function(){
			$(this).find("span").text("Hide Optional Fields");
			$("#OptionalFields").slideDown("fast");
		}, function() {
			$(this).find("span").text("Show Optional Fields");
			$("#OptionalFields").slideUp("fast");
		});
		$("#LiveIntentBetaForm").validate({
			messages: {
				first_name: "*Please enter your first name.",
				last_name: "*Please enter your last name.",
				email: {
			       required: "*Please include your email.",
			       email: "Please enter in the form of name@domain.com"
			     },
			    company: {
			    	 required: "*Please enter your company name."
			     },
			    country: "*Please enter a country.",
			    terms: "You must agree to the terms and conditions.",
			    "00NA0000001rAi5": "Please enter a valid twitter handle." 
		   }
		});
		$("#BetaFormSubmit").click(function(){
			$("#LiveIntentBetaForm").submit();
		});
		
		$("#00NA0000001rAi5").blur(function() {
			$("#LiveIntentBetaForm #StatusHolder span").attr("class", "statusLoading");
			$.ajax({
				  url: "http://api.twitter.com/1/users/show/" + $(this).val() + ".json?callback=?",
				  dataType: 'json',
				  success: function(data){	
						if(data.id != null || data.id != "0") {
							$("#LiveIntentBetaForm #StatusHolder span").attr("class", "statusConfirm");
							$("#00NA0000001rAi5").removeClass("error");
							$("#TwitterError").fadeOut("fast");
							return true;
						} else{
							$("#LiveIntentBetaForm #StatusHolder span").attr("class", "statusError");
							$("#00NA0000001rAi5").addClass("error");
							$("#TwitterError").css("display", "block");
						}
						return false;
					},
				timeout: 5000
			});
			setTimeout(function(){
				if ($("#LiveIntentBetaForm #StatusHolder span").attr("class") == "statusLoading"){
				$("#LiveIntentBetaForm #StatusHolder span").attr("class", "statusError");
				$("#00NA0000001rAi5").addClass("error");
				$("#TwitterError").css("display", "block");
				}
			}, 3000);
		});
	}
});