function removeCustomStyles(id) {
	$("body").removeClass(id);
}

function addCustomStyles(id) {
	$("body").addClass(id);
}

/**
 * Create a cookie with the given name and value and other optional parameters.
 *
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Set the value of a cookie.
 * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
 * @desc Create a cookie with all available options.
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Create a session cookie.
 * @example $.cookie('the_cookie', null);
 * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
 *       used when the cookie was set.
 *
 * @param String name The name of the cookie.
 * @param String value The value of the cookie.
 * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
 * If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
 * If set to null or omitted, the cookie will be a session cookie and will not be retained
 * when the the browser exits.
 * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
 * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
 * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
 * require a secure protocol (like HTTPS).
 * @type undefined
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 */
/**
 * Get the value of a cookie with the given name.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie !== '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

	
var inputSize = 5;
var inputs = new Array(inputSize);
inputs[0] = { input: "", bubbleDesc: "", dropdownDesc: "" };
inputs[1] = { input: "", bubbleDesc: "", dropdownDesc: "" };
inputs[2] = { input: "", bubbleDesc: "", dropdownDesc: "" };
inputs[3] = { input: "", bubbleDesc: "", dropdownDesc: "" };
inputs[4] = { input: "", bubbleDesc: "", dropdownDesc: "" };
var animationStartTime = 200;
var fadeInTime = 1000;
var fadeOutTime = 600;
var waitTime = 2000;
var fadeInTimeDrop = 600;
var closeMenu = true;
var animation = true;
var focusLossOnPage = false;
var inputSeries = Math.floor(Math.random() * 10 + 1);
var seriesPage = 1; 
var visitThreshold = 4;
var closeDropdownLen = 3;
	
function homeInit () {
	$("#input-label").css("display", "none");
	$("#more-links").css("right", "80px");

	/* $("#exbtop").after("<li id=\"settings-link\"><a href=\"/homesettings.html\">Settings</a><span class=\"pipe\">|</span></li>");*/ 

	var tips = $.cookie('WolframAlphaHomepageTips');
	var visits = $.cookie('WolframHomepageVisits');
	var returningFromSettings = ((window.location+ "").indexOf('animation=none') != -1);
	
	//Fade in hack for firefox on vista and windows 7
	if($.browser.mozilla && 
			(navigator.userAgent.indexOf("Windows NT 6.0") > -1 || navigator.userAgent.indexOf("Windows NT 6.1") > -1))  {
		$("#fadeinput").css({opacity : ".45", color : "#000"});
	}
	
	if(visits == null) 
		 visits = 0;
	
	if(!returningFromSettings){ 
		$.cookie('WolframHomepageVisits',++visits,{expires: 365, domain: '.wolframalpha.com'});}
	else {
		animation = false;
	}
	
	if(tips == "never" || ($.browser.msie && $.browser.version == "6.0" )) {
		animation = false;
	}

	var displayTips = (((tips != "never") && 
			(visits <= visitThreshold)) || 
			(tips == 'always')) && 
			(!$.browser.msie || $.browser.version != "6.0");

	if(!displayTips) {animation = false; }
	
	$("#i").val("").focus();

        $("form").submit(function(e) {
                       
                        if(animation) {
                                $("#i").val($("#fadeinput").html());
                                $("#fadeall").remove();
                                return true;
                        }else if($("#i").val()==""){e.preventDefault(); return false;}
             		});
	
	$("#makeHomepage").click(function(e) {
		if ($.browser.msie) {
			e.preventDefault();
			this.style.behavior = "url(#default#homepage)";
			this.setHomePage("http://" + window.location.hostname);
		}
	});
	
	setTimeout( function () {
		 	$("#input-label").fadeIn(fadeInTime , function () {
				if(displayTips && animation)
		 			animateBubbles(0);
		 	});
		}, animationStartTime); 

	if(displayTips) {
		$("#more-links").css("right", "35px");
		$("#input-links").prepend("<li><a id=\"show-hints\" href=\"#\">Show Hints</a><span class=\"pipe\">|</span></li>");

		$("#show-hints").click(function (e) {
			e.preventDefault();
			$("#fadeall").remove();
			$("#dropdown").show();
			animation = false;
			closeMenu = false;
		});

		// parse html into javascript json array
		var i = 0;
		$("#dropdowninputs li").each(function() {
			if (i < 5) {
				inputs[i].input = $(this).find(".input").text();
				inputs[i].bubbleDesc = $(this).find(".textdesc").html();
				inputs[i].dropdownDesc = $(this).find(".desc").html();
				i++;
			} else {
				return false;
			}
		});

		$("body").click(function (e) {
			var id = e.target.id;
			if(id != "fadeinput" && id != "i") {
				focusLossOnPage = true;
				if(closeMenu) {
					$("#dropdown").hide();
				}
			}
		});

		$("#dropdown")
			.mouseover(function() { closeMenu = false; })
			.mouseout(function() { closeMenu = true; });
	
		$("#dropdown ul li").click(function (e) {
			$("#i").val($("span.input", this).text()).focus();
			$("#dropdown").hide();
			$("#howTo").show();
                        window.scrollTo(1200,0);
		});
		
		$("#i")
		.click (function () {
			if(animation) {
				$("#fadeall").remove();
				$("#dropdown").fadeIn(fadeInTime);
				animation = false;
				$("#i").val("")
			}
		})
		.focus(function () {
			if(focusLossOnPage) {
				if(animation) {
					$("#fadeall").remove();
					$("#dropdown").fadeIn(fadeInTime);
					animation = false;
					$("#i").val("");
				}
			}
		})
		.keyup(function (e) {
			if(animation && e.keyCode != 13 && e.keyCode != 17 && e.keyCode != 116) { 
				$("#fadeall").remove();
				$("#dropdown").fadeIn(fadeInTimeDrop);
				animation = false;
			}else {
				if(e.keyCode != 17 && e.keyCode != 116 && e.keyCode != 13) {
					var inputLen = $("#i").val().length;
					if(inputLen >= closeDropdownLen) {
						$("#dropdown").hide();
					}
					if(inputLen < closeDropdownLen) {
						$("#dropdown").show();
					}
				}
			}
		});
		
		$("#fadeinput").click( function () {
			$("#fadeall").remove();
			$("#dropdown").fadeIn(fadeInTimeDrop);
			animation = false;
			$("#i").focus();
		});
		
		$("#close-dropdown").click (function () {
			$("#dropdown").hide();
		});
	}


   // ipad specific events if needed
   if (navigator.userAgent.indexOf("iPad") != -1) {
	$("#i").css("width","420px");
    // ipad clear input button
    $("#i").keyup(function(){
        if( $("#i").val().length > 0 ) {
            $("#iClear").show();
        }
    }).click(function(){
        if( $("#i").val().length > 0 ) {
            $("#iClear").show();
        }
    }).blur(function(){
        setTimeout(function(){
            $("#iClear").hide();
        },100);
    });

    $("#iClear").click(function(){
        $("#i").val('').focus();
    });
   }
   
   if (navigator.userAgent.indexOf("iPhone") == -1 && navigator.userAgent.indexOf("iPod") == -1) {
   $("#home-more").hover(function () {
		var winHeight = $(window).height();
		var height = $(this).find("#more-links").height();
		var ypos = $(this).offset().top;
		if (height + ypos - 16 > winHeight && !($.browser.msie && $.browser.version == "6.0")) {
			$(this).find("#more-move").css({ "top": (10 + winHeight - (ypos + height)) + "px" });
			$(this).find("#more-links").css({ "top": (-5 + winHeight - (ypos + height)) + "px" });
		} else {
			$(this).find("#more-links").css({ "top":"-15px" });
		}
	}, function () {
		$(this).find("#more-move").css("top", "");
		$(this).find("#more-links").css("top", "-10000px");
	});
   } else {
       $("#home-more").click(function (e) {
                e.preventDefault();
                var winHeight = $(window).height();
                var height = $(this).find("#more-links").height();
                var ypos = $(this).offset().top;
                if (height + ypos - 16 > winHeight) {
                        $(this).find("#more-move").css({ "top": (10 + winHeight - (ypos + height)) + "px" });
                        $(this).find("#more-links").css({ "top": (-5 + winHeight - (ypos + height)) + "px" });
                } else {
                        $(this).find("#more-links").css({ "top":"-15px" });
                }
        });
   }

}

function animateBubbles(index) {
	if(index < inputSize) {
		$("#bubble-desc").html(inputs[index].bubbleDesc + "");
		$("#fadeinput").html(inputs[index].input + "");
		$("#fadeall").fadeIn(fadeInTime, 
			function () {
				setTimeout( function () {
					$("#fadeall").fadeOut(fadeOutTime, function () {
						animateBubbles(++index);
					})
				}, waitTime);
			});
	} else {
		$("#fadeall").remove();
		$("#dropdown").fadeIn(fadeInTime);
		animation = false;
		$("#i").val("");
	}
}

$(document).ready(homeInit);

