﻿
// **************** OnLoad of the Page **************** //

var _dataList;
var _pager;


$(document).ready(function() {

    //    var flashvars = {};
    //    flashvars.settingsXML = "xml/settings.xml";
    //    var params = {};
    //    params.scale = "noscale";
    //    params.salign = "tl";
    //    params.wmode = "transparent";
    //    var attributes = {};
    //    swfobject.embedSWF("swf/coverflow.swf", "CoverFlowDiv", "980", "300", "9.0.0", false, flashvars, params, attributes);


    $('#mainNav > li').bind('mouseover', jsddm_open);
    $('#mainNav > li').bind('mouseout', jsddm_timer);


    $('.accordion .head').click(function() {
        $(this).next().toggle();
        return false;
    }).next().hide();

    $('#contactForm').jqTransform({ imgPath: '/forms/img/' });
    $("#accordion").accordion();
    //    $("#navbar-cont").tabs("#navbar-cont div.pane", { tabs: 'p', effect: 'slide', initialIndex: null });
    $("#ContactForm").validationEngine()

    if (typeof sIFR == "function") {
        // This is the older, ordered syntax
        sIFR.replaceElement("h5#pullquote", "tradegothic.swf", "#000000", "#000000", "#FFFFFF", "#FFFFFF", 0, 0, 0, 0);
        //sIFR.replaceElement("h2", "/Rexline/nevis.swf", "#006699", null, null, null, 0, 0, 0, 0);
        sIFR.replaceElement("h4.subhead", "tradegothic.swf", "#660000", null, null, null, 0, 0, 0, 0);
        sIFR.replaceElement("h3.sidebox", "/Rexline/nevis.swf", "#000000", "#000000", "#DCDCDC", "#DCDCDC", 0, 0, 0, 0, null);
        sIFR.replaceElement("h3", "nevis.swf", "#000000", null, null, null, 0, 0, 0, 0, "textalign=center&offsetTop=0&offsetLeft=");
        sIFR.replaceElement("h1.catalogHeading", "/Rexline/nevis.swf", "#ccc", null, null, null, 0, 0, 0, 0, "textalign=center&offsetTop=0&offsetLeft=");
        // sIFR.replaceElement("h2#searchTitle", "/Rexline/nevis.swf", "#006699", null, null, "#000000", 0, 0, 0, 0);
    };

    $('.catalog-entries').corner("4px");
    $("#nav ul").tabs("#panes > div", { effect: 'fade', fadeOutSpeed: 400 });
    $("#navigation").css({ 'background-color': 'transparent' }, 100)

    // setup ul.tabs to work as tabs for each div directly under div.panes
    $("ul.tabs").tabs("div.panes > div");


    $('#triggerModal').click(function(e) {
        $("#modalFocus").modal({
            opacity: 80,
            overlayCss: { backgroundColor: "#000" }
        });
    });

    $('#modalFocus').click(function(e) {
        $.modal.close();
    });

    // initialize scrollable
    $("#slideshow").scrollable().circular().autoscroll({ api: true, interval: 2000 });

    $("#flashContainer img[title]").tooltip({
        tip: '#tooltip',
        offset: [45, -120]
    });



});


function SearchKeyPress(e) {

    var searchVal = $('#s').val();
    var concLoc = "http://www.therexline.com/search/Default.aspx?searchKey=" + searchVal;

    if (navigator.userAgent.indexOf("Firefox") != -1) {
        if (e.keyCode == 13) {
            window.location = concLoc;
        }
    }
    else {
        if (event.keyCode == 13) {
            window.location = concLoc;
            event.returnValue = false;
        }
    }
}

document.onkeypress = SearchKeyPress; 


// **************** Main JS Class In Charge of Logic for Controls **************** //

//Type.registerNamespace("RexLine");

//RexLine.Products = function() { }

//RexLine.Products.prototype = {

//    getItemsOnCategory: function(catID) {
//        var catid = "#cat-" + catID;
//        $.ajax({
//            type: "POST",
//            url: "GetRexLineData.asmx/GetProductsListFromItemNo",
//            data: "{'prodCategory':'" + catID + "'}",
//            contentType: "application/json; charset=utf-8",
//            dataType: "json",
//            success: function(msg) {
//                var items = (typeof msg.d) == 'string' ? eval('(' + msg.d + ')') : msg.d;
//                $(catid).empty();

//                for (var i = 0; i < items.length; i++) {
//                    $(catid).append('<p class=navsubitems><strong>' + items[i] + '</strong></p>');
//                }

//            },

//            failure: function(msg) {
//                alert(msg);
//            }
//        });

//    }
//}

//function RexLineProductsObject() {
//    return new RexLine.Products();
//}

//var callProductsObject = RexLineProductsObject();

//function getProductData() {
//   callProductsObject.getProductData();
//}

//function getItemFromCategories(catID) {
//    callProductsObject.getItemsOnCategory(catID);
//}

/* dropdown menu logic */

var timeout = 500;
var closetimer = 0;
var ddmenuitem = 0;

function jsddm_open() {
    jsddm_canceltimer();
    jsddm_close();
    ddmenuitem = $(this).find('ul').eq(0).css('visibility', 'visible');
}

function jsddm_close()
{ if (ddmenuitem) ddmenuitem.css('visibility', 'hidden'); }

function jsddm_timer()
{ closetimer = window.setTimeout(jsddm_close, timeout); }

function jsddm_canceltimer() {
    if (closetimer) {
        window.clearTimeout(closetimer);
        closetimer = null;
    } 
}

document.onclick = jsddm_close;


        function loadProducts()
        {
            var startIndex = (_pager.get_pageIndex() * _pager.get_pageSize());

            DataService.GetProductList(startIndex, _pager.get_pageSize(), '', '', onProductLoadSuccess);
        }

        function onProductLoadSuccess(result)
        {
            _pager.set_recordCount(result.Total);
            _dataList.set_dataSource(result.Rows);
            _dataList.dataBind();
        }

        function onItemDataBound(sender, e)
        {
            var item = e.get_item();

            if (item.get_isDataItemType())
            {
                var product = item.get_dataItem();

                var spnProductName = item.findControl('spnProductName');
                var spnCategory = item.findControl('spnCategory');
                var spnSupplier =  item.findControl('spnSupplier');
                var spnDiscontinued = item.findControl('spnDiscontinued');
                var spnUnitPrice = item.findControl('spnUnitPrice');

                setText(spnProductName, product.Name);
                setText(spnCategory, product.CategoryName);
                setText(spnSupplier, product.SupplierName);
                setText(spnDiscontinued, ((product.Discontinued == true) ? 'Yes' : 'No') );
                setText(spnUnitPrice, product.UnitPrice.localeFormat('C'));
            }
        }

        function pageChanged(sender, e)
        {
            _pager.set_pageIndex(e.get_newPageIndex());
            loadProducts();
        }

        function setText(element, text)
        {
            if (typeof element.textContent != 'undefined')
            {
                element.textContent = text;
            }
            else if (typeof element.innerText != 'undefined')
            {
                element.innerText = text;
            }
        }

