﻿$(document).ready(function() {
    //Make selected class for selected menu
    var currentUrl = window.location.href.toUpperCase();
    if (currentUrl.indexOf('#') > 0) {
        currentUrl = currentUrl.substring(0, currentUrl.indexOf('#'));
    }

    $(".mainnav ul li,.subnav ul li").each(function(i) {
        var href = $(this).children("a").attr("href").toUpperCase();
        var isSelected = false;
        if (currentUrl.indexOf(href) != -1) {
            isSelected = true;
            var onclickstr = $(this).children("a").attr("onclick");
            if (onclickstr) {
                isSelected = false;
            }
        }
        var str = "Contact";
        if (!isSelected) {
            if (currentUrl.indexOf(str) != -1 && href.indexOf(str) != -1) {
                var str = "Store";
                if (currentUrl.indexOf(str) != -1 && href.indexOf(str) != -1) {
                    isSelected = true;
                }
            }
        }
        if (isSelected) {
            $(this).addClass("selected");
        }
    });

    var logoStr = "<li class='logo'><a href='" + homeLocation + "/Landing.aspx'><img src='" + homeLocation + "/App_Themes/SonyCentreExtra/images/hdg_main_nav.gif' width='177' height='43' alt='Sony Centre' /></a></li>";
    $(".mainnav ul li:first").before(logoStr);

    //Add last class for sub navigation
    if (!$(".subnav ul li:last").hasClass("last") && !$(".subnav ul li:last").hasClass("selected")) {
        $(".subnav ul li:last").addClass("last");
    }

    //Add active class for thumbnail small image
    if (!$(".name img:first").hasClass("active")) {
        $(".name img:first").addClass("active");
    }

    //Print function
    $(".print_view").click(function() {
        isPrintProcess = true;
        var currentUrl = window.location.href;
        printwin = window.open(currentUrl, 'printpopup', 'width=920,height=720,scrollbars=yes');
        printwin.focus();
        //$("link.changer").attr("href",$(this).attr('rel'));
        return false;
    });
    var opernerObj = window.opener;
    if (opernerObj != null) {
        try {
            if (opernerObj.IsPrintProcess) {
                var isPrint = opernerObj.IsPrintProcess();
                if (isPrint) {
                    changeStyleToPrint(opernerObj)
                }
            }
        }
        catch (err) {
        }

    }


    //Search box
    try {
        var x = $.jqURL.get("searchtext");
        if (x) {
            $(".searchInputBox:first").val(x);
        }
        else {
            $(".searchInputBox:first").val("Search");
        }
    }
    catch (err) {
    }

    //$(".searchInputBox:first").val("Search");
    $(".searchInputBox:first").bind("focus", function() {
        if ($(this).val() == "Search") {
            $(this).val("");
        }
    });
    $(".searchInputBox:first").bind("blur", function() {
        if ($(this).val() == '') {
            $(this).val('Search');
        }
    });

    //Store event
    $(".key_date, .key_time").each(function() {
        var htmlStr = $(this).html();
        if (htmlStr.length < 4) {
            $(this).hide();
        }
    });
    //SendOmniture();
});

var isPrintProcess = false;
function IsPrintProcess() {
    return isPrintProcess;
}
function SetPrintProcess(val) {
    isPrintProcess = val;
}
function changeStyleToPrint(opernerObj) {
    var printStyle = $(".print_view:first").attr("rel");
    $("link.main").attr("href", printStyle);
    if (opernerObj) {
        opernerObj.SetPrintProcess(false);
    }
}

function selectParentChange(parentObj) {
    var parentId = $(parentObj).val();
    var childId = $(parentObj).next('select').val();
    if (parentId != '0') {
        $(parentObj).next('select').children("option[value!='0']").remove();
        addOptions(parentId, parentObj);
        if ($(parentObj).next('select').children("option[value=" + childId + "]").length > 0) {
            $(parentObj).next('select').children("option[value=" + childId + "]").attr('selected', 'selected');
        }

    }
    else {
        $(parentObj).next('select').children("option:first").attr('selected', 'selected');
        $(parentObj).next('select').attr('disabled', true);
    }
}

function addOptions(parentId, parentObj) {
    $(parentObj).next('select').removeAttr('disabled');
    $.each(cascadeData, function(i, value) {
        if (value[0] == parentId) {
            $(parentObj).next('select').append($("<option></option>").attr("value", value[1]).text(value[2]));
        }
    });
}

function goToContactUs(type, etc) {
    gotoContact(type, etc);
}

function GoToStoreHandler(storeid) {
    var storeName = getStoreName(storeid);
    var url = homeLocation + '/Home/Find-a-store.aspx?storeid=' + storeid;
    if (storeName != '') {
        var storePath = 'Sony-Centre,-' + storeName;
        url = homeLocation + '/Home/' + storePath + '/Home.aspx';
    }
    location.href = url;
}

function moveToUrl(newUrl, isNewWindow) {
    if (isNewWindow == 'True') {
        window.open(newUrl, 'newWindow');
    }
    else {
        location.href = homeLocation + newUrl;
    }

    return false;
}

function clickOfferMore(url) {
    if (url) {
        window.open(url);
        return false;
    }
    return true;
}
//************ Phase2 ****************
function getStoreId(storeName) {
    for (i = 0; i < storeDic.length; i++) {
        if (storeDic[i][1] == storeName) {
            return storeDic[i][0];
        }
    }
    return 0;
}

function getStoreNameFromUrl() {
    var name = '';
    var url = window.location.href;
    var i = url.indexOf('Sony-Centre,-');
    if (i > 0) {
        var j = url.indexOf('/', i + 1);
        if (j > 0 && j > (i+13)) {
            name = url.substring(i + 13, j);
        }
    }
    return name;
}

function getStoreName(storeId) {
    for (i = 0; i < storeDic.length; i++) {
        if (storeDic[i][0] == storeId) {
            return storeDic[i][1];
        }
    }
    return '';
}

function gotoContact(type, etc) {
    var storeName = getStoreNameFromUrl();
    var url = homeLocation + '/Home/Contact-us.aspx';
    if (storeName != '') {
        var storeId = getStoreId(storeName);
        var storePath = 'Sony-Centre,-' + storeName;
        url = homeLocation + '/Home/' + storePath + '/Contact-Store.aspx?type=' + type + '&etc=' + etc + '&storeId=' + storeId + '&redirect=' + storePath;
    }
    location.href = url;
}

function gotoViewMap() {
    var storeName = getStoreNameFromUrl();
    var url = homeLocation + '/Home/Find-a-store.aspx';
    if (storeName != '') {
        var storeId = getStoreId(storeName);
        var storePath = 'Sony-Centre,-' + storeName;
        url = homeLocation + '/Home/' + storePath + '/How-to-find-us.aspx?storeid='+storeId;
    }
    location.href = url;

}

function gotoStoreHome(storeId) {
    var storeName = getStoreName(storeId);
    if (storeName != '') {
        var storePath = 'Sony-Centre,-' + storeName;
        location.href = homeLocation + '/Home/' + storePath + '/Home.aspx';
    }
}

function gotoStaff(storeId, staffId) {
    var storeName = getStoreName(storeId);
    if (storeName != '') {
        var storePath = 'Sony-Centre,-' + storeName;
        location.href = homeLocation + '/Home/' + storePath + '/Meet-the-team.aspx#STAFF'+staffId;
    }
}

//*********** End of Phase2 ***********
