$(document).ready(function() {
	//Favorite Video for Booking Event: JustFoodERP-YouTube-Video-Contest
	if (0 < document.location.href.toLowerCase().lastIndexOf('justfooderp-youtube-video-contest')) {
		//alert('welcome');
		$('.favoritevideo').parent().parent().show();
		$('.favoritevideo').show();
	} 


	$("#support-ctas").fade({		
		pause: 6000,
		speed:500,
		showControls: true,
		controlBottom:4,
		controlLeft:6,
		loop:true
	});

    /*kentico BizForm apply this class to the image submit button*/
    $(".FormButton").hover(
	  function() {
	      $(this).attr("src", $(this).attr("src").replace("_off", "_on"));
	  },
	  function() {
	      $(this).attr("src", $(this).attr("src").replace("_on", "_off"));
	  }
	);
    /*you can apply this class to image buttons so they will have rollover effections*/
    $(".ImageButton").hover(
	  function() {
	      $(this).attr("src", $(this).attr("src").replace("_off", "_on"));
	  },
	  function() {
	      $(this).attr("src", $(this).attr("src").replace("_on", "_off"));
	  }
	);
	
	//ToolTip for Social icons
	$("#brandingQuickLinks a img[title]").tooltip({
		// custom positioning
		position: 'bottom center'										  
	});

    /*any pages use Tab menu layout: News & Events, Resources etc. */
    $(function() {
        //$("#tabs").tabs();
        //$("#tabs").bind("tabsselect", function(event, ui) {ui.tab.hash=window.location.hash;})
        // setup ul.tabs to work as tabs for each div directly under div.panes
        $("ul#tabsMenu").tabs("div.panes > div");

    });


    /* User Event */
    $('.eventDate').click(function() {
        $(this).next('div').slideToggle('slow').siblings('div:visible');
        $(this).toggleClass('active');
        $(this).siblings('.eventDate').removeClass('active');
        var expando = $(this).children('img').attr("src");
        if (expando == '/images/plus.gif') $(this).children('img').attr("src", "/images/minus.gif");
        else $(this).children('img').attr("src", "/images/plus.gif");
    });


    /* News & Events */
    //alert(document.location.href.lastIndexOf('NewsEvents.aspx#tabs1'));
    if (0 < document.location.href.toLowerCase().lastIndexOf('newsevents.aspx#tabs1')) {
        $("#tabs-1").show();
        $("#tabs-2").hide();
        $("li#tabNews a").addClass("selected");

    } else if (0 < document.location.href.toLowerCase().lastIndexOf('newsevents.aspx#tabs2')) {
        $("#tabs-2").show();
        $("#tabs-1").hide();
        $("li#tabEvents a").addClass("selected");
    } else if (0 < document.location.href.toLowerCase().lastIndexOf("/newsevents/news/")) {
        $("#tabs-1").show();
        $("#tabs-2").hide();
        $("li#tabNews a").addClass("selected");
    } else if (0 < document.location.href.toLowerCase().lastIndexOf("/newsevents/events/")) {
        $("#tabs-1").hide();
        $("#tabs-2").show();
        $("li#tabEvents a").addClass("selected");
    } else if (0 < document.location.href.toLowerCase().lastIndexOf('/newsevents')) {
        $("#tabs-1").show();
        $("#tabs-2").hide();
        $("li#tabNews a").addClass("selected");
    }

    $("li#tabNews a").click(function() {
		document.location.href = '/newsevents.aspx#tabs1';
        $("#tabs-1").show();
        $("#tabs-2").hide();
        $("li#tabNews a").addClass("selected");
        $("li#tabEvents a").removeClass("selected");
    });

    $("li#tabEvents a").click(function() {
		document.location.href = '/newsevents.aspx#tabs2';
        $("#tabs-2").show();
        $("#tabs-1").hide();
        $("li#tabEvents a").addClass("selected");
        $("li#tabNews a").removeClass("selected");
    });

    /* Resources */
   /* if (0 < document.location.href.toLowerCase().lastIndexOf('resources.aspx?tabs-1')) {
        $("#tabs").tabs('select', 0)
    }
    if (0 < document.location.href.toLowerCase().lastIndexOf('resources.aspx?tabs-2')) {
        $("#tabs").tabs('select', 1)
    }
    if (0 < document.location.href.toLowerCase().lastIndexOf('resources.aspx?tabs-3')) {
        $("#tabs").tabs('select', 2)
    } */
    if (0 < document.location.href.toLowerCase().lastIndexOf('/products---services/erp-demo-viewer.aspx?video=0')) {
        $.cookie('DemoPage', '0b60c05b-3b55-4f81-b64d-41fb7cd4552e', { path: '/' }); //dev: c899c147-0de1-44cf-87a0-058c0a0ff887
        $.cookie('DemoVideo', '?video=0', { path: '/' });
        $.cookie('RegisterFor', 'JustFoodERP Business Software Demo', { path: '/' });
        if ($.cookie('ResourcesRegister')) {

        } else {
            document.location.href = '/Resources/Registration.aspx';
        }
    } else if (0 < document.location.href.toLowerCase().lastIndexOf('/products---services/crm-demo-viewer.aspx?video=1')) {
        $.cookie('DemoPage', 'e1668131-6047-4fa7-bfdb-119d03fe691f', { path: '/' }); //dev:649ef7ec-e405-4ecf-a76d-17f566fe1baa
        $.cookie('DemoVideo', '?video=1', { path: '/' });
        $.cookie('RegisterFor', 'JustFoodERP Food Safety Demo', { path: '/' });
        if ($.cookie('ResourcesRegister')) {

        } else {
            document.location.href = '/Resources/Registration.aspx';
        }
    } else if (0 < document.location.href.toLowerCase().lastIndexOf('/products---services/foodsafety-demo-viewer.aspx?video=2')) {
        $.cookie('DemoPage', '87906286-a903-44b3-a21f-880d5ebc2ce9', { path: '/' }); //dev:afcc1a35-09fd-4f9c-a848-1d2bb850d034
        $.cookie('DemoVideo', '?video=2', { path: '/' });
        $.cookie('RegisterFor', 'JustFoodERP Reporting Demo', { path: '/' });
        if ($.cookie('ResourcesRegister')) {

        } else {
            document.location.href = '/Resources/Registration.aspx';
        }
    } else if (0 < document.location.href.toLowerCase().lastIndexOf('/products---services/reporting-demo-viewer.aspx?video=3')) {
        $.cookie('DemoPage', '97a0eafd-5e59-4f3c-a141-278b1a0c09ba', { path: '/' }); //dev:efc0d393-fc06-4519-943b-798e4cc48b2d
        $.cookie('DemoVideo', '?video=3', { path: '/' });
        $.cookie('RegisterFor', 'JustFoodERP CRM Demo', { path: '/' });
        if ($.cookie('ResourcesRegister')) {

        } else {
            document.location.href = '/Resources/Registration.aspx';
        }
    }

    $("a.checkCookies").click(function() {
        //alert(this);
        /*if (0 < document.location.href.lastIndexOf('/Products---Services/ERP.aspx')) {
        $.cookie('DemoPage', '51ad335f-6937-4c2e-9813-47effd2abae8', {path: '/'});
        $.cookie('DemoVideo', '?video=0', {path: '/'});
        } else if (0 < document.location.href.lastIndexOf('/Products---Services/Food-Safety.aspx')) {
        $.cookie('DemoPage', '51ad335f-6937-4c2e-9813-47effd2abae8', {path: '/'});
        $.cookie('DemoVideo', '?video=1', {path: '/'});
        } else if (0 < document.location.href.lastIndexOf('/Products---Services/Reporting.aspx')) {
        $.cookie('DemoPage', '51ad335f-6937-4c2e-9813-47effd2abae8', {path: '/'});
        $.cookie('DemoVideo', '?video=2', {path: '/'});
        } else if (0 < document.location.href.lastIndexOf('/Products---Services/CRM.aspx')) {
        $.cookie('DemoPage', '51ad335f-6937-4c2e-9813-47effd2abae8', {path: '/'});
        $.cookie('DemoVideo', '?video=3', {path: '/'});
        } else */
        if (0 < document.location.href.toLowerCase().lastIndexOf('/resources')) {
            $.cookie('DemoPage', '244ec0ad-e920-4087-9596-37287c0b2ad2', { path: '/' });
            $.cookie('DemoVideo', '', { path: '/' });
            $.cookie('RegisterFor', this, { path: '/' });

            /*$("#tabs-1 a").click(function () { 
            $.cookie('DemoPage', '244ec0ad-e920-4087-9596-37287c0b2ad2', {path: '/'});
            $.cookie('DemoVideo', '#tabs-1', {path: '/'});		
            });
            $("#tabs-2 a").click(function () { 
            $.cookie('DemoPage', '244ec0ad-e920-4087-9596-37287c0b2ad2', {path: '/'});
            $.cookie('DemoVideo', '#tabs-2', {path: '/'});
            });
            $("#tabs-3 a").click(function () { 
            alert('hi');
            $.cookie('DemoPage', '244ec0ad-e920-4087-9596-37287c0b2ad2', {path: '/'});
            $.cookie('DemoVideo', '#tabs-3', {path: '/'});
            });*/
        }

        if ($.cookie('ResourcesRegister')) {

        } else {
            this.href = '/Resources/Registration.aspx';
            this.target = '_self';
        }

    });


    //alert($.cookie('DemoVideo'));
    //alert($.cookie('DemoPage'));
    //alert($.cookie('RegisterFor'));
    if ($.cookie('RegisterFor') && $('.HiddenTextBoxField')) {
        $(".HiddenTextBoxField").attr("value", $.cookie('RegisterFor'));
        //alert($(".HiddenTextBoxField").attr("value"));
    }
    //alert(document.location.href.lastIndexOf('/Resources.aspx') + $.cookie('DemoVideo'));

    /*//alert(document.location.href);
    if (0 < document.location.href.lastIndexOf('Resources.aspx#demos')) {
		
	} else if (0 < document.location.href.lastIndexOf('Resources.aspx#casestudies')) {
		
	}
    $("li#tabProduct a").click(function () { 
    $("#tabs-1").show();
    $("#tabs-2").hide();
    $("#tabs-3").hide();
    });
	
	$("li#tabDemos a").click(function () { 
    $("#tabs-2").show();
    $("#tabs-1").hide();
    $("#tabs-3").hide();
    });
	
	$("li#tabCase a").click(function () { 
    $("#tabs-3").show();
    $("#tabs-1").hide();
    $("#tabs-2").hide();
    });*/

    /* Distribution Webcast Series */
    //alert(document.location.href.toLowerCase().lastIndexOf('/distributionwebcastseries'));
    if (0 < document.location.href.toLowerCase().lastIndexOf('/distributionwebcastseries')) {
		//alert($.cookie('distributionwebcastseries'));
        if ($.cookie('DistributionWebcastSeriesRegister')) {
			//has registered before, do nothing
		}
		else {
			$.cookie('DistributionWebcastSeriesRegister', 'yes', { path: '/' });
            $.cookie('DemoPage', 'd28e23f4-ef80-49f6-b735-c8c75db46cad', { path: '/' }); /*live site: d28e23f4-ef80-49f6-b735-c8c75db46cad  ; dev site: d28e23f4-ef80-49f6-b735-c8c75db46cad */
            $.cookie('DemoVideo', '', { path: '/' });			
			if (0 < document.location.href.toLowerCase().lastIndexOf('?video=1')) {
            	$.cookie('RegisterFor', 'Distribution Webcast Series - First Steps to archieving effective inventory management', { path: '/' });
			}
			else if (0 < document.location.href.toLowerCase().lastIndexOf('?video=2')) {
				$.cookie('RegisterFor', 'Distribution Webcast Series - improving forecst accuracy', { path: '/' });
			}
			else if (0 < document.location.href.toLowerCase().lastIndexOf('?video=3')) {
				$.cookie('RegisterFor', 'Distribution Webcast Series - Effective Replenishment Parameters', { path: '/' });
			}
			else {
				$.cookie('RegisterFor', 'Distribution Webcast Series', { path: '/' });
			} 
			
            document.location.href = '/Resources/Registration.aspx';
        } 
    }

	



    function getCookie(c_name) {
        if (document.cookie.length > 0) {
            c_start = document.cookie.indexOf(c_name + "=");
            if (c_start != -1) {
                c_start = c_start + c_name.length + 1;
                c_end = document.cookie.indexOf(";", c_start);
                if (c_end == -1) c_end = document.cookie.length
                return unescape(document.cookie.substring(c_start, c_end));
            }
        }
        return ""
    }

    function setCookie(c_name, value, expiredays) {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + expiredays);
        document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + exdate.toGMTString());
    }

    function checkCookie() {
        username = getCookie('username');
        if (username != null && username != "") {
            //alert('Welcome again ' + username + '!');
        }
        else {
            username = prompt('Please enter your name:', "");
            if (username != null && username != "") {
                setCookie('username', username, 365);
            }
        }
    }




/*Bing Search*/
	if ($('#WLSearchBoxButton')){
		$('#WLSearchBoxButton').click(function(){
			constructBingSearchString();			   
		});
	}
	
	$(function(){
		$('#WLSearchBoxInput').keydown(function(e){
			if (e.keyCode == 13) {
				constructBingSearchString();
				return false;
			}
		});
	});
	
	function constructBingSearchString() {
		var formTarget = "http://www.bing.com/search?go=&qs=n&sk=&sc=5-11"; // http://www.bing.com/search?q=tools&go=&form=QBLH&qs=n&sk=&sc=5-11&q1=site:equip-soft.com
		if ($('#WLSearchBoxInput').val() != '') {
			formTarget = formTarget + '&q=' + $('#WLSearchBoxInput').val(); // keyword field
		}
		if ($('.WLSearchBoxFORM').val() != '') {
			formTarget = formTarget + '&FORM=' + $('.WLSearchBoxFORM').val();
		}
		if ($('.WLSearchBoxq1').val() != '') {
			formTarget = formTarget + '&q1=' + $('.WLSearchBoxq1').val(); // domain of your site
		}
		//alert(formTarget);
		window.open(formTarget,'newpage',''); // 'scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no'			
	
	}



});
