jQuery.fn.slideFadeToggle = function(speed, easing, callback) {
	return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);
};

//common functions

$(function(){
	$(".popupwindow").popupwindow();
});

initGetStarted = function(){
	setTimeout(function() { $(".enroll_now").slideFadeToggle("slow"); }, 700);
}

initRandomFeaturedlogos = function(){
	var logos = new Array('img/featured/featured_logo_1.jpg', 'img/featured/featured_logo_2.jpg', 'img/featured/featured_logo_3.jpg', 'img/featured/logo_ups1.jpg', 'img/featured/featured_logo_5.jpg', 'img/featured/featured_logo_6.jpg', 'img/featured/logo_ups2.jpg', 'img/featured/logo_redenvelope.jpg', 'img/featured/logo_proflowers.jpg');
	var logo1 = logos[Math.ceil(logos.length * Math.random()) - 1];
	var temp = new Array();

	for (var i = 0; i < logos.length; i++)
	{
		if (logos[i] != logo1 && (logo1.indexOf('ups') == -1 || logos[i].indexOf('ups') == -1))
		{
		  temp.push(logos[i]);
		}
	}
	
	var logo2 = temp[Math.ceil(temp.length * Math.random()) - 1];

	$("#featured_left").attr({ src: logo1, alt: "" });
	$("#featured_right").attr({ src: logo2, alt: "" });
}

initFeaturedNav = function(){
	$("#featured_logo_1 img").unbind("hover").hover(function() {
		$(this).css("cursor", "pointer");
	}, function() {
		$(this).css("cursor", "auto");
	});

	$("#featured_logo_2 img").unbind("hover").hover(function() {
		$(this).css("cursor", "pointer");
	}, function() {
		$(this).css("cursor", "auto");
	});
	
	$("#featured_logo_1 img").click(function () { 
		location.href="offers.html" 
	});

	$("#featured_logo_2 img").click(function () {
		location.href="offers.html" 
	});
}


//enroll form functions

initEnrollTooltips = function(){

	if ( $('#enroll_form #EmailAddress').length > 0 ) {

		var tooltipDefaults = {cluetipClass: 'default', dropShadow: false, activation: 'focus', mouseOutClose: true, sticky: false, ajaxCache: false, arrows: true, width: '240px', showTitle: false, positionBy: 'fixed'};

		tooltipDefaults.topOffset = -20;
		$('#enroll_form #EmailAddress').cluetip(tooltipDefaults);
		$('#enroll_form #Password').cluetip(tooltipDefaults);
		$('#enroll_form #Answer1').cluetip(tooltipDefaults);
		$('#enroll_form #Answer2').cluetip(tooltipDefaults);


		var tooltipDefaults2 = {cluetipClass: 'default', dropShadow: false, activation: 'focus', mouseOutClose: true, sticky: false, ajaxCache: false, arrows: true, width: '240px', showTitle: false, positionBy: 'fixed'};

		tooltipDefaults2.topOffset = -15;
		tooltipDefaults2.leftOffset = 135;
		$('#enroll_form #card1').cluetip(tooltipDefaults2);
		$('#enroll_form #card2').cluetip(tooltipDefaults2);
		$('#enroll_form #card3').cluetip(tooltipDefaults2);
		$('#enroll_form #card4').cluetip(tooltipDefaults2);
		$('#enroll_form #card5').cluetip(tooltipDefaults2);
		$('#enroll_form #card6').cluetip(tooltipDefaults2);
		$('#enroll_form #card7').cluetip(tooltipDefaults2);
		$('#enroll_form #card8').cluetip(tooltipDefaults2);
		$('#enroll_form #card9').cluetip(tooltipDefaults2);
		$('#enroll_form #card10').cluetip(tooltipDefaults2);

		tooltipDefaults2.topOffset = -20;
		tooltipDefaults2.leftOffset = 210;
		$('#enroll_form #num1').cluetip(tooltipDefaults2);
		$('#enroll_form #num2').cluetip(tooltipDefaults2);
		$('#enroll_form #num3').cluetip(tooltipDefaults2);
		$('#enroll_form #num4').cluetip(tooltipDefaults2);
		$('#enroll_form #num5').cluetip(tooltipDefaults2);
		$('#enroll_form #num6').cluetip(tooltipDefaults2);
		$('#enroll_form #num7').cluetip(tooltipDefaults2);
		$('#enroll_form #num8').cluetip(tooltipDefaults2);
		$('#enroll_form #num9').cluetip(tooltipDefaults2);
		$('#enroll_form #num10').cluetip(tooltipDefaults2);

		var tooltipDefaults3 = {activation: 'click', dropShadow: false, mouseOutClose: true, sticky: false, ajaxCache: false, arrows: true, width: '340px', showTitle: false, positionBy: 'fixed', onShow: closeme};

		tooltipDefaults3.topOffset = -35;
		tooltipDefaults3.leftOffset = 125;
		$('#enroll_form #lock').cluetip(tooltipDefaults3);
	}
}

initAddCards = function(){
	if ( $(".addmore").length > 0 ) {
		$(".addmore a").click(function(e){$(".card2").show('fast'); $(".addmore").hide(); e.preventDefault();});
	}
}

initCellOptions = function(){
	$("#cell_opt").click(function(){
		checkMobile($(this));
	});
}

function checkMobile(cellOpt) {
	if (cellOpt.is(":checked")) {
		$("#mobile_no_label").show();
		$("#mobile_no").show();
	}
	else if(cellOpt.not(":checked")) {
		$("#mobile_no_label").hide();
		$("#mobile_no").hide();
	}
}

function lockHover() {
	$("#lock").hover(
	  function () {
	    $(this).addClass("hover");
	  },
	  function () {
	    $(this).removeClass("hover");
	  }
	);
}

function closeme() {
	setTimeout(function() {
		$('#locktip').parent().parent().parent().hide();
	}, 5000);
}

initMaskCard = function(){
	if ( $("#card1").length > 0 ) {
		$('#card1').mask('9999-9999-9999-9999');
		$('#card2').mask('9999-9999-9999-9999');
		$('#card3').mask('9999-9999-9999-9999');
		$('#card4').mask('9999-9999-9999-9999');
		$('#card5').mask('9999-9999-9999-9999');
		$('#card6').mask('9999-9999-9999-9999');
		$('#card7').mask('9999-9999-9999-9999');
		$('#card8').mask('9999-9999-9999-9999');
		$('#card9').mask('9999-9999-9999-9999');
		$('#card10').mask('9999-9999-9999-9999');
	}
}

initOverlayFix = function(){
	if ( $("#enroll_overlay").length > 0 ) {
		$("#enroll_overlay").ifixpng();
	}

	if ( $("#faq_overlay").length > 0 ) {
		$("#faq_overlay").ifixpng();
	}
	
	if ( $("#overview_overlay").length > 0 ) {
		$("#overview_overlay").ifixpng();
	}
	
	if ( $("#offers_overlay").length > 0 ) {
		$("#offers_overlay").ifixpng();
	}
	
	if ( $("#signin_overlay").length > 0 ) {
		$("#signin_overlay").ifixpng();
	}
}

//FAQ page functions

initDisplayFAQ = function(){

	$("div#faqTrigger p span").hover(function () {
		  $(this).addClass("hilite");
		}, function () {
		  $(this).removeClass("hilite");
	});


	$('div#faqList h3').hover(function () {
		  $(this).addClass("hover");
		}, function () {
		  $(this).removeClass("hover");
	});


	$('div#faqList h3').click(function() {
	
		$(this).next().slideToggle('fast').siblings('div:visible:not(.back2top)').slideUp('fast');
				
		if ($(this).siblings().hasClass('arrow-down')) {
			$(this).siblings().removeClass('arrow-down').addClass('arrow-up');
		}

		if ($(this).hasClass('arrow-up')) {
			$(this).removeClass('arrow-up').addClass('arrow-down');
		}
		else  {
			$(this).removeClass('arrow-down').addClass('arrow-up');
		}
		
		$("#emphasis").css({ color:"#666666" });
	});

	$('span#expand_all').click(function() {
		if ($('div#faqList h3').hasClass('arrow-up')) {
			$("#emphasis").css({ color:"#666666" });
			$('div#faqList h3').removeClass('arrow-up').addClass('arrow-down').next().slideDown('fast');
		}
	});

	$('span#collapse_all').click(function() {
		if ($('div#faqList h3').hasClass('arrow-down')) {
			$("#emphasis").css({ color:"#666666" });
			$('div#faqList h3').removeClass('arrow-down').addClass('arrow-up').next().slideUp('fast');
		}
	});

}


initSetupScroll = function(){

	//scroll except safari
	if ($.browser.safari) {
	   //alert("this is safari!");
	}
	else {
	$.easing.elasout = function(x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	};

	$('ul.links').click(function(e){
		e.preventDefault();
	});

	$('a.back').click(function(e){
		$.scrollTo( 0, 200 );
		e.preventDefault();
	});
	
	if ( $("div#faq_container").length > 0 ) {
		$.scrollTo( 0 );
	}

	$('#prog-info').click(function(){
		if (this.blur()) this.blur();
		$.scrollTo( '#anchor1', 200 );
	});

	$('#enrollment').click(function(){
	if (this.blur()) this.blur();
		$.scrollTo( '#anchor2', 200 );
	});

	$('#purchase').click(function(){
	if (this.blur()) this.blur();
		$.scrollTo( '#anchor3', 200 );
	});

	$('#statement').click(function(){
	if (this.blur()) this.blur();
		$.scrollTo( '#anchor4', 200 );
	});

	$('#cancelling').click(function(){
	if (this.blur()) this.blur();
		$.scrollTo( '#anchor5', 200 );
	});

	$('#service').click(function(){
	if (this.blur()) this.blur();
		$.scrollTo( '#anchor6', 200 );
	});

	$('#electronic').click(function(){
	if (this.blur()) this.blur();
		$.scrollTo( '#anchor7', 200 );
	});

	$('#security1').click(function(){
	if (this.blur()) this.blur();
		$.scrollTo( '#anchor8', 200 );
	});

	$('#anchor_qualifying').click(function(e){
		e.preventDefault();
	});

	$('#anchor_qualifying').click(function(){
		if (this.blur()) this.blur();
			$('div#faqList h3').next().slideUp('fast');
			$("#qualify").removeClass('arrow-up').addClass('arrow-down').next().slideDown('fast');;
			$("#emphasis").css({ color:"#000000" });
			$.scrollTo( '#qualifying', 200 );
		});
	}
}


initGoToPurchases = function(){
	if (location.search.indexOf("tab=qual") != -1) {
		if ($.browser.safari) {
			//alert("this is safari!");
		}
		else {
			$('div#faqList h3').next().slideUp('fast');
			$("#qualify").removeClass('arrow-up').addClass('arrow-down');
			$("#emphasis").css({ color:"#000000" });
			$("#qualify").next().slideDown('fast');
			$.scrollTo( '#qualifying', 200 );
		}	
	}
}


//offers page functions


initOffersLoaded = function(){

	$(".landing_offer").hover(function() {
		$(this).css("cursor", "pointer");
		$(this).addClass("hover");
		$(".landing_offer_type").addClass("over");
		$(".landing_offer_desc").addClass("over");
    	}, function() {
		$(this).css("cursor", "auto");
		$(this).removeClass("hover");
		$(".landing_offer_type").removeClass("over");
		$(".landing_offer_desc").removeClass("over");
	});


	$(".landing_offer").click(function (e) {
	var offer = $(this);
	offerID = offer.attr("ID");
	$("#feeds").load("main_offers/" +offerID+ ".html", function(){
		displayTooltip(offer);
	});
	if (this.blur()) this.blur();
	e.preventDefault();

	});


	//close button
	$(".panel_closewindow img").unbind("hover").hover(function() {
		$(this).css("cursor", "pointer");
	}, function() {
		$(this).css("cursor", "auto");
	});

	$(".panel_closewindow img").unbind("click").click(function() {
		$(".overlay").hide();
	});

}

$(document).ready(function() {
	initGetStarted();
	initRandomFeaturedlogos();
	initEnrollTooltips();
	initAddCards();
	initCellOptions();
	lockHover();
	initMaskCard();
	initOverlayFix();
	initDisplayFAQ();
	initSetupScroll();
	initGoToPurchases();
	initOffersLoaded();
	initFeaturedNav();
});