﻿var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

$(document).ready(function() {

    var com_AgentId = 0;

    if (BrowserDetect.browser.toLowerCase() == 'chrome' || BrowserDetect.browser.toLowerCase() == 'safari')
    {
        $(".notIE").show();
        $(".isIE").hide();
    }
    else
    {
        $(".notIE").hide();
        $(".isIE").show();
        $("#paper").css("top", "132px");
        //alert($("#menu").css("top"));
        //$("#menu").css("top","0px");
    }
    
    $("#menu ul:first").css("margin-top", "0px");
    
    $("#registernow").bind("click", registerNow);
    $("#competition-terms-and-conditions").bind("click", termsAndConditions);
    $(".initiate").bind("click", initiateMission);
    $("#SendToFriend").bind("click", sendToFriend);
    $(".eventDate").bind("change", updateEvent);

    $("#search-bar input").eq(0).keypress(function(e) {
        if (e.which == 13) {
            searchProducts();
            return false;
        }
    });

    $("#btnSearch").bind("click", searchProducts);
    $("#btnSubscribe").bind("click", subscribeMe);
    $("#categories a").bind("mouseenter", brightenCategory);
    $("#categories .polaroid").bind("mouseenter", brightenCategory);
    
    function brightenCategory()
    {
        if (BrowserDetect.browser.toLowerCase() == 'chrome' || BrowserDetect.browser.toLowerCase() == 'safari')
        {
            $(".polaroid", $(this).parent()).removeClass("bright").removeClass("dim").addClass("dim");
            $(this).removeClass("dim").addClass("bright");
        }
        else
        {
            $(".polaroid-ie", $(this).parent().parent()).removeClass("bright-ie").removeClass("dim-ie").addClass("dim-ie");
            $(this).find("span").removeClass("dim-ie").addClass("bright-ie");
        }
        /*
        $(".polaroid", $(this).parent().parent()).removeClass("bright").removeClass("dim").addClass("dim");
        $(this).find("span").removeClass("dim").addClass("bright");
*/
    }

    function searchProducts() {
        var c = $("#search-bar select, #search-bar input");
        var text = escape(c.eq(2).val());

        var href = "/search/" + c.eq(0).val() + "/" + c.eq(1).val();

        if (text.length > 0)
            href += "/" + text;

        window.location.href = href;
    }



    function subscribeMe() {

        
        var i = $("#newsletter input");
        var em = i.eq(0).val();

        i.eq(1).val("Wait...");
        i.eq(1).attr("disabled", true);

        ajaxJsonPost("/mf.svc/Subscribe", '{"email":"' + em + '"}',
                                function(msg) {

                                    if (msg.d != "false") {
                                        $("#newsletter div").html("<em style='color:green;'>Thank you! You've been subscribed.</em>");
                                    }
                                    else {
                                        i.eq(1).attr("disabled", false);
                                        i.eq(1).val("Subscribe");
                                        alert("You could not be subscribed. Please try again.");
                                    }

                                });
    }

});

function welcomeBack(agentId)
{
    com_AgentId = agentId;
    setTimeout(showWelcome, 1000);
}

function showWelcome(agentId)
{
    tb_show("Welcome Back","/welcome/" + com_AgentId + "?TB_iframe=true&modal=false&width=400&height=150");
    //$("#welcome").html("<span>Welcome Back Agent #" + com_AgentId + "</span>").addClass("welcome").show("slow");
    //setTimeout(clearWelcome, 3000);
}

function clearWelcome()
{
    //$("#welcome").fadeOut("slow");
    parent.tb_remove();
}

function registerNow()
{
    tb_show("Become an Agent","/signup?TB_iframe=true&modal=false&width=600&height=500");
}
function termsAndConditions()
{
    tb_show("Terms and Conditions","/competition-terms-and-conditions-pop?TB_iframe=true&modal=true&width=500&height=400");
}

function initiateMission()
{
    var rel = $(this).attr("rel");
    if (rel == undefined)
        rel = '/mission-dossier/initiate-mission';
        
    tb_show("Initiate Mission",rel + "?TB_iframe=true&modal=true&width=600&height=620");
}
function sendToFriend()
{
    var rel = '/refer-a-friend';
        
    tb_show("Refer a Friend",rel + "?TB_iframe=true&modal=true&width=600&height=475");
}
function updateEvent()
{
    var productId = $(this).attr("productId");
    var requestedDate = $(this).val();
    
   
    ajaxJsonPost("/mf.svc/UpdateProductWishList", '{"productId":"' + productId + '","requestedDate":"' + requestedDate + '"}',
                            function(msg) {

                                if (msg.d == "false") {
                                    alert('Update of date failed, please try again.');
                                }

                            });
}

    function maximumLength(e, l)
    {
        supportsKeys = true
        calcCharLeft(e, l)
    }

    //This function is used to give a maximum value for the textarea fields
    function calcCharLeft(e, l)
    {
        clipped = false
        maxLength = l
            if (e.value.length > maxLength)
        {
	        e.value = e.value.substring(0,maxLength)
	        charleft = 0
	        clipped = true
            }
        else
        {
	        charleft = maxLength - e.value.length
        }
            if (charleft <= 0)
    	        alert('You have exceeded the available length of this field.')

   	        return clipped
    }

