﻿function tileSelected(tile) {
    var cname = $("h2", tile).text();
    var i = $(".ui-accordion-header").index($("h2:contains('" + cname + "')"));
    if (i < 0) i = 0;
    $("#CurrentDepartmentSelector").val(i);
}
function rotateTile(container) {
    var tileList = $("#rotationList", container);
    var i = Math.floor(Math.random() * tileList.size());
    var tile = tileList.eq(i);
    //var imgNames = $("#rotationList", tile).val().split(',');
    var imgNames = tile.val().split(',');
    i = Math.floor(Math.random() * imgNames.length);
    var imgNameParts = imgNames[i].split(':');
    var imgName = imgNameParts[0];
    var width = imgNameParts[1];
    var height = imgNameParts[2];
    //var imgElement = tile.find("img");
    var imgElement = tile.next("img");
    var src = imgElement.attr("src");
    i = src.lastIndexOf('/');
    var j = src.lastIndexOf('.');
    src = src.substr(0, i + 1) + imgName + src.substr(j);
    imgElement.attr("style", "width:" + width + "px;height:" + height + "px");
    //imgElement.css("width", width);
    //imgElement.css("height", height);
    imgElement.attr("src", src);
}
$(document).ready(function() {
    /*adjustHeights();*/
    $(".tileContent").each(function(i, p) {
        $(".tileFooter > .checkoutlabelnw", p).attr("title", "Click to see available discounts");
    });
    /*
    $(".tileContent").mouseenter(function() {
        $(".hptile > img", this).attr("src", function() {
            return this.src.replace("DarkBlackTile1", "BlackTile1f");
        });
    }).mouseleave(function() {
        $(".hptile > img", this).attr("src", function() {
            return this.src.replace("BlackTile1f", "DarkBlackTile1");
        });
    });
    $("#PageTitle").each(function(i, p) {
        $(".catdesc").each(function(i2, c) {
            var height = Math.max(c.scrollHeight, 50);
            c.style.paddingTop = (((height - $("span", this).height()) / 2) - 1) + "px";
            c.parentNode.style.height = height + "px";
        })
    });
    */
    $("#ddlOrderBy").val($("#OrderByProperty").val());

    var tileListElement = $("#SubCategoryList");
    if (tileListElement.length) {
        setInterval(function() { rotateTile(tileListElement) }, 1000);
    }
});
function postOnReturn(e, eventTarget) {
    var key;
    if (window.event)
        key = window.event.keyCode; //IE
    else
        key = e.keyCode; //firefox
    if (key == 13) {
        __doPostBack(eventTarget, '');
        return false;
    } else {
        return true;
    }
}
function IsReturn(e) {
    var key;
    if (window.event)
        key = window.event.keyCode; //IE
    else
        key = e.keyCode; //firefox
    return (key == 13);
}
function gotoPage(e, pt) {
    if (IsReturn(e)) {
        var page = parseInt(pt.value);
        var currentPage = parseInt($("#CurrentPageNumber").val());
        if (!isNaN(page) && page != currentPage) {
            var baseUrl = $("#BaseOrderByUrl").val();
            var hasArg = baseUrl.indexOf('?') >= 0;
            location.href = baseUrl + (hasArg ? "&" : "?") + "page=" + page;
            return false;
        }
        return true;
    }
}
function gotoSort(sd) {
    var orderBy = sd.value;
    var url = $("#BasePageUrl").val();
    var i = url.indexOf('?');
    //if (i > 0)
    //    url = url.substring(0, i);
    if (orderBy != $("#OrderByProperty").val()) {
        if (orderBy != $("#DefaultOrderBy").val()) {
            if (i == -1)
                url = url + "?o=" + orderBy;
            else
                url = url + "&o=" + orderBy;
        }
        location.href = url;
    }
}
