﻿$(document).ready(function () {

    //if ($.client.os == 'Mac' || $.client.os == 'Linux') {
    //                $('#container> header>nav').css({ 'font-size': '13px', 'padding': '15px 18px 0' });
    //$('#container> header>nav a').css({ 'font-size': '13px', 'padding': '15px 18px 0' });
    //$('#mainmenu>ul>li>a').css({ "font-size": "13px", "padding": "25px 18px 0" });
    //}

    if (GetPageFromURL().toLowerCase() == '') {
        setCookie("parentMenu", '', 7);
    }

    //            setCookie("parentMenu", '', 7);
    //$(document).queue("ajaxRequests", GetSEO('Home', onError));
    //$(document).queue("ajaxRequests", GetWebsiteInfo(onLoadWebsiteInfoSuccess, onError));
    $(document).queue("ajaxRequests", GetRootMenu(onLoadMenuSuccess, onError));
    //GetSEO('Home', onError);
    //GetWebsiteInfo(onLoadWebsiteInfoSuccess, onError);
    //GetRootMenu(onLoadMenuSuccess, onError);

    $(document).dequeue("ajaxRequests");

    $('#subnav li a').live('click', function (e) {
        var parentMenu = $('#mainmenu li.nav_active a').text();
        var tmp = getCookie("parentMenu");

        if (parentMenu.toLowerCase() != tmp.toLowerCase()) {
            setCookie("parentMenu", parentMenu, 7);
        }
    });

    $('#mainmenu li a').live('click', function (e) {
        setCookie("parentMenu", '', 7);
    });

    $("#srh").keypress(function (event) {
        //location.href = 'http://www.google.com'; 
        if (event.keyCode == 13) {
            if ($(this).val() == '') {
                //alert('Empty');
                event.preventDefault();
            }
            else {
                //alert($(this).val());
                //location.hash = '/Search/' + $(this).val();
                event.preventDefault();
                location.href = 'Search-' + $('#srh').val();
            }
        }
    });

    $('#srh_btn').click(function (event) {
        if ($('#srh').val() != '') {
            location.href = 'Search-' + $('#srh').val();
        }
        else {
            event.preventDefault();
        }
    });
});

function markSubNavigationActiveLink(selector) {
    //Look through all the links in the sidebar
    //alert('in side markActiveLink');
    $(selector).filter(function () {

        //Take the current URL and split it into chunks at each slash
        var currentURL = window.location.toString().split("/");

        if (currentURL[currentURL.length - 1] == '') {
            return $(this).find('a').attr("href").toLowerCase() == 'Home'.toLowerCase();
        }

        //                var parentMenu = getCookie("parentMenu");
        //                if (parentMenu != null && parentMenu != "") {
        //                    if ($(this).find('a').text() == parentMenu) {
        //                        return $(this).find('a').text() == parentMenu;
        //                    }
        //                }

        //return true if the bit after the last slash is the current page name
        //alert(currentURL[currentURL.length - 1]);
        //                alert(currentURL[currentURL.length - 1]);
        return $(this).find('a').attr("href").toLowerCase() == currentURL[currentURL.length - 1].toLowerCase();

        //when the filter function is done, you're left with the links that match.
    }).addClass("nav_active");

    //Afterwards, look back through the links. If none of them were marked,
    //mark your default one.
    //            if ($("mainmenu a").hasClass("nav_active") == false) {
    //                $("div#sidebar h2:nth-child(2) a").addClass("active");
    //            }
}

function markActiveLink(selector, firstelement) {
    //Look through all the links in the sidebar
    //alert('in side markActiveLink');
    $(selector).filter(function () {

        //Take the current URL and split it into chunks at each slash
        var currentURL = window.location.toString().split("/");

        //                var pageName = currentURL[currentURL.length - 1];
        //                var defaultPage = 'home'.toLowerCase();
        //                var hrefText = $(this).find('a').attr("href").toLowerCase(); 
        //                alert('in Main mark menu ' + currentURL[currentURL.length - 1]);
        if (currentURL[currentURL.length - 1] == '') {
            return $(this).find('a').attr("href").toLowerCase() == $(firstelement).find('a').attr("href").toLowerCase();
        }

        //alert('parentMenu ' + getCookie("parentMenu"));
        var parentMenu = getCookie("parentMenu");
        //alert('parentmenu text : ' + parentMenu.toLowerCase());
        if (parentMenu != null && parentMenu != "") {
            //                    alert('a text : ' + $(this).find('a').text().toLowerCase());
            //                    alert('parentmenu text : ' + parentMenu.toLowerCase());
            if ($(this).find('a').text().toLowerCase() == parentMenu.toLowerCase()) {
                return $(this).find('a').text().toLowerCase() == parentMenu.toLowerCase();
            }
        }

        //return true if the bit after the last slash is the current page name
        //alert(currentURL[currentURL.length - 1]);
        //                alert(currentURL[currentURL.length - 1]);
        return $(this).find('a').attr("href").toLowerCase() == currentURL[currentURL.length - 1].toLowerCase();

        //when the filter function is done, you're left with the links that match.
    }).addClass("nav_active");

    //Afterwards, look back through the links. If none of them were marked,
    //mark your default one.
    if ($('#mainmenu li').hasClass("nav_active") == false) {
        $(this).removeClass("nav_active");
    }
}

//function GetWebsiteInfo(onSuccess, onError) {
//    $.ajax({
//        type: "POST",
//        url: "PageService.aspx/GetWebsiteInfo",
//        data: "{}",
//        contentType: "application/json; charset=UTF-8",
//        dataType: "json",
//        success: function (result) {
//            onSuccess(result);

//        }, error: function (resutl) {
//            onError(result);
//        }
//    });
//}

//function GetPageTitle(SEOName, onSuccess) {
//    $.ajax({
//        type: "POST",
//        url: "PageService.aspx/GetPageTitle",
//        data: "{'SEOName':'" + SEOName + "'}",
//        contentType: "application/json; charset=UTF-8",
//        dataType: "json",
//        success: function (result) {
//            onSuccess(result);

//        } //, error: function (resutl) {
//        //onError(result);
//        //}
//    });
//}

//function onLoadWebsiteInfoSuccess(result) {
//    document.title = result.d.Title;
//    var SEOName = GetPageFromURL();
//    //GetPageTitle(SEOName, onLoadPageTitleSuccess);
////    $(document).queue("ajaxRequests", GetPageTitle(SEOName, onLoadPageTitleSuccess));
////    $(document).dequeue("ajaxRequests");
//    //            $.get('Template/FacebookTemplate.htm', function (template) {
//    //                $.tmpl(template, result.d).appendTo('div.facebook');
//    //                //alert(result.d.Title);
//    //                
//    //                //alert(SEOName);
//    //            });

//    //            $.get('Template/TwitterTemplate.htm', function (template) {
//    //                $.tmpl(template, result.d).appendTo('div.twitter');
//    //            });
//}

function GetPageFromURL() {
    var currentURL = window.location.toString().split("/");
    //var filename = '';
    if (currentURL[currentURL.length - 1] == '') {
        return '';
    }
    else {
        return currentURL[currentURL.length - 1].split("-")[0];
    }
}

//function onLoadPageTitleSuccess(result) {
//    //alert(result.d);
//    if (result.d.length) {
//        document.title += " [" + result.d + "]";
//    }
//    $(document).dequeue("ajaxRequests");
//}

function GetRootMenu(onSuccess, onError) {
    $.ajax({
        type: "POST",
        url: "PageService.aspx/GetRootMenu",
        data: "{}",
        contentType: "application/json; charset=UTF-8",
        dataType: "json",
        success: function (result) {
            onSuccess(result);

        }, error: function (resutl) {
            onError(result);
        }
    });
}

function GetSubMenu(parentMenuName, onSuccess, onError) {
    $.ajax({
        type: "POST",
        url: "PageService.aspx/GetSubMenu",
        data: "{'parentMenuName': '" + parentMenuName + "'}",
        contentType: "application/json; charset=UTF-8",
        dataType: "json",
        success: function (result) {
            onSuccess(result);

        }, error: function (resutl) {
            onError(result);
        }
    });
}

function onLoadMenuSuccess(result) {
    $.get('Template/MenuTemplate.htm', function (template) {
        $.tmpl(template, result.d).appendTo('#mainmenu > ul');
        $.tmpl(template, result.d).appendTo('footer ul#menufooter');
        $('<li><a href="SiteMap">Sitemap</a></li>').appendTo('footer ul#menufooter');
        //$('#mainmenu li.nav_active').find('a').text()

        var activeMenu = getCookie("parentMenu");
        if (activeMenu != null && activeMenu != "") {
            $('#mainmenu li').each(function () {
                if ($(this).find('a').text().toLowerCase() == activeMenu.toLowerCase()) {
                    $(this).addClass("nav_active");
                }
                $(document).dequeue("ajaxRequests");
            });
        }

        if ($('#mainmenu li').hasClass("nav_active") == false) {
            //$(this).removeClass("nav_active");
            GetParentMenuFromURL();
            //alert(getCookie("parentMenu"));
            markActiveLink('#mainmenu li', '#mainmenu li:first');
        }
        //markActiveLink('#mainmenu li');
        //GetSubMenu($('#mainmenu li.nav_active a').text(), onLoadSubMenuSuccess, onError);
        $(document).queue("ajaxRequests", GetSubMenu($('#mainmenu li.nav_active a').text(), onLoadSubMenuSuccess, onError));
        $(document).dequeue("ajaxRequests");
    });
}

function GetParentMenuFromURL() {
    $.ajax({
        type: "POST",
        async: false,
        url: "PageService.aspx/GetParentMenuName",
        data: "{'seoPage': '" + GetPageFromURL() + "'}",
        contentType: "application/json; charset=UTF-8",
        dataType: "json",
        success: function (result) {
            //onSuccess(result);
            //alert(result.d);
            if (result.d) {
                setCookie("parentMenu", result.d, 7);
                //alert(getCookie("parentMenu"));
            }
        }, error: function (resutl) {
            onError(result);
        }
    });
}

function onLoadSubMenuSuccess(result) {
    $.get('Template/MenuTemplate.htm', function (template) {
        if (result.d.length > 0) {

            $.tmpl(template, result.d).appendTo('#subnav > ul');
            //markActiveLink('#subnav li');
            markSubNavigationActiveLink('#subnav li');
            //                    alert('#subnav li ' + $('#subnav li.nav_active a').text());
        }
        else {
            $('#subnav').hide();
        }
        $(document).dequeue("ajaxRequests");
        if ($.client.os == 'Mac' || $.client.os == 'Linux') {
        //                $('#container> header>nav').css({ 'font-size': '13px', 'padding': '15px 18px 0' });
        //$('#container> header>nav a').css({ 'font-size': '13px', 'padding': '15px 18px 0' });
        $('#mainmenu>ul>li>a').css({ "font-size": "13px", "padding": "25px 18px 0" });
        }
    });
}

function onError(result) {
    alert("error :" + resutl.status + ":" + resutl.statusText);
}


function getCookie(c_name) {
    var i, x, y, ARRcookies = document.cookie.split(";");
    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x == c_name) {
            return unescape(y);
        }
    }
}

function setCookie(c_name, value, exdays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
    document.cookie = c_name + "=" + c_value;
}

//function GetSEO(filename, onError) {
//    $.ajax({
//        type: "POST",
//        url: "PageService.aspx/GetPageSEO",
//        data: "{'fileName': '" + filename + "'}",
//        contentType: "application/json; charset=UTF-8",
//        dataType: "json",
//        success: function (result) {

//            $("<h1><a href=" + result.d + ">First prize</a></h1>").insertBefore('div#search');
//            $("<div id=\"logo\"><a href=" + result.d + "><img src=\"App_Themes/default/images/foot_logo.png\" width=\"153\" height=\"15\" border=\"0\" /></a></div>").insertBefore('ul#menufooter');
//            $(document).dequeue("ajaxRequests");

//        }, error: function (resutl) {
//            onError(result);
//        }
//    });
//}
