$(document).ready(function(){
	shareChannelsForMainSFPage(); 
	 
	$('input[name*="email"]').change( function() {
  		unbind_input();
	});
	 
	 if ($.browser.msie){
		$('.download-link').addClass('for-ie');
		$('.overlay').css('filter','progid:DXImageTransform.Microsoft.Alpha(opacity=50)');
		
	} else 
	if ((! $.browser.msie)&& (! $.browser.mozilla)){                
		$(".add-box").html("<p style='margin-top: 30px;'>The Window Shopper browser add-on currently works only on the Firefox and Internet Explorer browsers.</p>")
		
	};
	
	 $(".subscribe").click(function(){
	 	$(".subscribe:first").css('z-index','77');
	 	$(this).toggleClass('active');
		if ($(this).hasClass('active')){
                                    $(this).children('.in-box').css('display','block');
		}else {
                                    $(this).children('.in-box').css('display','none');			
		}
	 });
	 if ($('#form-web').length > 0) {
	 $("#form-web").validate({
                rules : {
                        email : {required : true},
						radio : {required : true}
                },
                messages : {
                        email : {required : "Please enter email"},
						radio : {required : "Please select visual commerce"}
                },
				submitHandler: function() {
					$('.thank').stop(true,true).fadeTo(3000, 1, function(){
						$('.thank').fadeTo(50000, 0);
						window.location.href='webPartner.html';
					});
				}
        });
	}	
	
	 if ($('#form-mob').length > 0) {	
	 $("#form-mob").validate({
                rules : {
                        mobemail : {required : true}
                },
                messages : {
                        mobemail : {required : "Please enter email"}
                },
				submitHandler: function() {
					$('.thank').stop(true,true).fadeTo(3000, 1, function(){
						$('.thank').fadeTo(50000, 0);
                                                    window.location.href='mobilePartner.html';
					});
				}
        });	
	}
	if ($('.main-box UL').length > 0) {
		$('.main-box UL').cycle({ 
	  		fx:     'fade', 
	  	  	delay:  4000 
		});
	}
	
	$('.autohint').focus(function(){
		if ($(this).val() == $(this).attr('title')) $(this).val('');
	});
	$('.autohint').blur(function(){
		if ($(this).val() == '') $(this).val($(this).attr('title'));
	});
	
	$('.popup-link').click(function(e){
                e.preventDefault();
		$('.overlay').width($(window).width());
		$('.overlay').height($(document).height());
		$('.overlay').show();
		var left_pos = ($('body').width()-parseInt($('.form-box').css('width')))/2;
		var top_pos = ($(window).height()-parseInt($('.form-box').css('height')))/2;
		$('#'+$(this).attr('rel')).css('left',left_pos);
		$('#'+$(this).attr('rel')).css('top',top_pos);
		$('#'+$(this).attr('rel')).show();
	});
	
	$('.close-btn, .overlay').click(function(){
		$('.form-box').hide();	
		$('.overlay').hide();
	});

	$('.block-mentions .item H6').click(function(){
		if ($(this).parents('.item').hasClass('open')) {
			$(this).parents('.item').removeClass('open');
		} else {
			$('.block-mentions .item.open').removeClass('open');
			$(this).parents('.item').addClass('open');
		}
	});

});

function shareChannelsForMainSFPage(/*theurl, thetitle*/){
    document.getElementById('sfTweetSpanId').setAttribute('st_url', 'http://www.superfish.com');
    document.getElementById('sfFBSpanId').setAttribute('st_url', 'http://www.superfish.com');
    document.getElementById('sfGPSpanId').setAttribute('st_url', 'http://www.superfish.com');
    //document.getElementById('spanID1').setAttribute('st_title', thetitle);

    stButtons.locateElements();

    //finally display the popup after the buttons are made and setup.
    //displaypopup();
}



function unbind_input() {
	
	if ($('input[name*="email"]').val() != '')
		$('input[name*="url"]').unbind('focus');
	
}

var CurrentClient = {
    n : navigator,
    ua : navigator.userAgent,
    p : navigator.platform,
    v : navigator.vendor,
    supported: false,
    init: function () {

        this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
        this.version = this.searchVersion(this.ua)
        || this.searchVersion(this.n.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));
    },
    isIE7: function() {
       return  navigator.userAgent.indexOf("MSIE 7") != -1;
    },
    isIE: function() {
       return  navigator.userAgent.indexOf("MSIE") != -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/3.5",
        identity: "FF 3.5"
    },
    {
        string: navigator.userAgent,
        subString: "Firefox/3.6",
        identity: "FF 3.6"
    },

    {
        string: navigator.userAgent,
        subString: "Firefox/4.0",
        identity: "FF 4.0"
    },
    {
        string: navigator.vendor,
        subString: "Camino",
        identity: "Camino"
    },
    {
        string: navigator.userAgent,
        subString: "Netscape",
        identity: "Netscape"
    },
    {
        string: navigator.userAgent,
        subString: "MSIE 8",
        identity: "IE 8",
        versionSearch: "MSIE 8"
    },
    {
        string: navigator.userAgent,
        subString: "MSIE 9",
        identity: "IE 9",
        versionSearch: "MSIE 9"
    },
    {
        string: navigator.userAgent,
        subString: "MSIE 7",
        identity: "IE 7",
        versionSearch: "MSIE 7"
    },
    {
        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"
    }
    ]

};
CurrentClient.init();




var brSuppoerted = false;
if(
    CurrentClient.browser.indexOf("FF 3.") > -1 ||
    CurrentClient.browser.indexOf("FF 4.") > -1 ||
    CurrentClient.browser.indexOf("FF 5.") > -1 ||
    CurrentClient.browser == "Mozilla" ||
    CurrentClient.browser == "IE 7" ||
    CurrentClient.browser == "IE 8" ||
    CurrentClient.browser == "IE 9"){
    brSuppoerted = true;
}


function activateLink(source){
    if(!brSuppoerted ) {
        alert( "We are sorry but at this time WindowShopper only works on Firefox 3.5+/4.x/5.x and Internet Explorer 7+.");
    }

    var currentTime = new Date();
    var month = (currentTime.getMonth()+1);
    var dayOfMonth = currentTime.getDate();
    var hours = currentTime.getHours();
    var minutes = currentTime.getMinutes();
    var seconds = currentTime.getSeconds();    

    var downloadTS = currentTime.getFullYear().toString() +((month < 10)?"0":"") + month.toString() + ((dayOfMonth < 10)?"0":"") + dayOfMonth.toString() +((hours < 10)?"0":"") + hours.toString() + ((minutes < 10)?"0":"") + minutes.toString() + ((seconds < 10)?"0":"") + seconds.toString();

    if(CurrentClient.isIE()) {
        document.location = "https://www.superfish.com/WindowShopper_" + downloadTS +  "/exe";
    }
    else {
        // document.location = "https://addons.mozilla.org/en-US/firefox/downloads/latest/13780/addon-13780-latest.xpi?src=superfish";
        document.location = "https://www.superfish.com/WindowShopper_" + downloadTS +  "/xpi";



        $("#install").fadeIn(800,
            function(){
                $("#install").fadeOut(700,
                    function(){
                        $("#install").fadeIn(600,
                            function(){
                                $("#install").fadeOut(500,
                                    function(){
                                        $("#install").fadeIn(400)
                                    });
                            });
                    });
            });
    }

    trackEvent("-", "download from " + source, "-");
}

function setBrowserDisplay()
{    
    var divIdName;
    if(CurrentClient.isIE()){
        divIdName = "addPluginIE";
    } else if (!brSuppoerted){        
        if (document.getElementById("download") != undefined)
            document.getElementById("download").style.display = "none";
        divIdName = "unsupported";
    } else {
        divIdName = "addPluginFF";
    }
    
    document.getElementById(divIdName).style.display = "block";
}
