$(document).ready(function() {

    /*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"));
	  }
	);

    /*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;})

    });


    /* 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() {
        $("#tabs-1").show();
        $("#tabs-2").hide();
        $("li#tabNews a").addClass("selected");
        $("li#tabEvents a").removeClass("selected");
    });

    $("li#tabEvents a").click(function() {
        $("#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.lastIndexOf('/Products---Services/Demo-Viewer.aspx?video=0')) {
        $.cookie('DemoPage', '51ad335f-6937-4c2e-9813-47effd2abae8', { path: '/' });
        $.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/demo-viewer.aspx?video=1')) {
        $.cookie('DemoPage', '51ad335f-6937-4c2e-9813-47effd2abae8', { path: '/' });
        $.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/demo-viewer.aspx?video=2')) {
        $.cookie('DemoPage', '51ad335f-6937-4c2e-9813-47effd2abae8', { path: '/' });
        $.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/demo-viewer.aspx?video=3')) {
        $.cookie('DemoPage', '51ad335f-6937-4c2e-9813-47effd2abae8', { path: '/' });
        $.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', '?tabs-2', { 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();
    });*/
    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);
            }
        }
    }

});