﻿// namepsace
var EUROFAB = EUROFAB || {};
EUROFAB.namespace = function() {
    for (var i = 0, j = arguments.length; i < j; i++) {
        var names = arguments[i].split('.');
        var curName = names.splice(0, 1)[0];
        if (!EUROFAB[curName]) {
            EUROFAB[curName] = {};
        }
        var current = EUROFAB[curName];
        for (var name in names) {
            if (!current[names[name]]) {
                current[names[name]] = {}
            }
            current = current[names[name]];
        }
    }
}

$(function() {
    var common = EUROFAB.common;
    
    // auto expanding text areas
    $('textarea').growfield();
    
    // highlight on focus
    common.bindHighlight();

    // Rebind any jQuery plugins after update
    // Use endRequest instead of load to ensure all objects are already loaded
    var pageRequestManager = Sys.WebForms.PageRequestManager.getInstance();
    pageRequestManager.add_endRequest(function() {
        EUROFAB.common.rebindAll();
        $.unblockUI({ fadeOut: 200 });
    });
    pageRequestManager.add_endRequest(common.scrollTo);
});



EUROFAB.namespace('common');
EUROFAB.common = (function() {
    var categories = {
        Fabrics : 1,
        Draperies : 2,
        DraperyHardware : 3,
        BlindsAndShutters : 4,
        Wallpaper : 5,
        Bedding : 6,
        Upholstery : 7,
        Trim : 8,
        Accessories : 9
    };

    function bindGrowfield() {
        $('textarea').growfield();
    };

    function bindHighlight() {
        $("input:text, input:password, select, textarea").focus(function() {
            $(this).addClass("focusHighlight");
        }).blur(function() {
            $(this).removeClass("focusHighlight");
        });
    };

    function rebindAll() {
        bindGrowfield();
        bindHighlight();
    };

    function doPostBackAsync(eventName, eventArgs) {
        var prm = Sys.WebForms.PageRequestManager.getInstance();

        if (!Array.contains(prm._asyncPostBackControlIDs, eventName)) {
            prm._asyncPostBackControlIDs.push(eventName);
        }

        if (!Array.contains(prm._asyncPostBackControlClientIDs, eventName)) {
            prm._asyncPostBackControlClientIDs.push(eventName);
        }

        __doPostBack(eventName, eventArgs);
    };

    // remove filter attribute to fix IE7 cleartype fadein/out issues
    function removeFilter(controlName) {
        var controlID = $('[id$=' + controlName + ']').attr('id');
        var element = $get(controlID);
        if (element.style.filter && element.style.removeAttribute) {
            element.style.removeAttribute('filter');
        }
    };

    function scrollTo() {
        $.scrollTo('0px', { duration: 1200 });
    };

    function getCurrentDate() {
        var d = new Date();
        var date = d.getDate();
        var month = d.getMonth();
        month++;
        var year = d.getFullYear();

        if (month.toString().length == 1)
            month = "0" + month;

        return month + "/" + date + "/" + year;
    };

    return {
        bindHighlight: bindHighlight,
        categories : categories,
        doPostBackAsync: doPostBackAsync,
        rebindAll: rebindAll,
        removeFilter: removeFilter,
        scrollTo: scrollTo,
        getCurrentDate: getCurrentDate
    };
})();



/** Utility/Extension Functions **/

// removes item from array if it exists
if (!Array.prototype.remove) {
    Array.prototype.remove = function(item) {
        var index = this.indexOf(item);
        
        if (index !== -1) {
            this.splice(index, 1);
        }
    };
}

// add indexOf support to browsers (IE)
if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function(item /*, from*/) {
        var len = this.length;

        var from = Number(arguments[1]) || 0;
        from = (from < 0) ? Math.ceil(from) : Math.floor(from);
        if (from < 0) {
            from += len;
        }

        for (; from < len; from++) {
            if (from in this && this[from] === item) {
                return from;
            }
        }

        return -1;
    };
}

// checks if string ends with arg
String.prototype.endsWith = function(str) {
     return (this.match(str + "$") == str)
 }

 // returns boolean true if true string, boolean false otherwise
 String.prototype.toBoolean = function(str) {
     return (this.toLowerCase() === 'true' ? true : false);
 }

 function getEventTarget(e) {
     e = e || window.event;
     return e.target || e.srcElement;
 } 
