﻿(function ($) {
    var imgList = [];
    $.extend({
        preload: function (imgArr, option) {
            var setting = $.extend({
                init: function (loaded, total) { },
                loaded: function (img, loaded, total) { },
                loaded_all: function (loaded, total) { }
            }, option);
            var total = imgArr.length;
            var loaded = 0;

            setting.init(0, total);
            for (var i in imgArr) {
                imgList.push($("<img />")
					.attr("src", [imgArr[i], '?', Math.random() * Math.random()].join(''))
					.load(function () {
					    loaded++;
					    setting.loaded(this, loaded, total);
					    if (loaded == total) {
					        setting.loaded_all(loaded, total);
					    }
					})
				);
            }

        }
    });
})(jQuery);

var PRODUCT_ROLLOVERS_IMGS = new Array();

var loadProductsImages = function () {
    var tempArray = new Array();
    for (var i = 0; i < PRODUCT_ROLLOVERS_IMGS.length; i++) {
        tempArray[i] = ['images/PRODUCT/icon/', PRODUCT_ROLLOVERS_IMGS[i], '.jpg'].join('');
    }
    $.preload(
        tempArray, {
            init: function (loaded, total) { },
            loaded: function (img, loaded, total) { },
            loaded_all: function (loaded, total) {
                loadProductsRollOvers();
            }
        });
};

var loadProductsRollOvers = function () {
    var tempArray = new Array();
    for (var i = 0; i < PRODUCT_ROLLOVERS_IMGS.length; i++) {
        tempArray[i] = ['images/PRODUCT/swatch/', PRODUCT_ROLLOVERS_IMGS[i], '.jpg'].join('');
    }
    $.preload(
        tempArray, {
            init: function (loaded, total) { },
            loaded: function (img, loaded, total) {
                var helpArray = img.src.split('/');
                var helpStr = helpArray[helpArray.length - 1];
                helpArray = helpStr.split('.');
                helpStr = helpArray[0];

                var iconImage = $(['#', helpStr].join('')).attr('src');

                $(['#', helpStr].join('')).mouseenter(function () {
                    $(this).attr('src', img.src);
                });
                $(['#', helpStr].join('')).mouseleave(function () {
                    $(this).attr('src', iconImage);
                });
            },
            loaded_all: function (loaded, total) { }
        });
};

var IRebuttonOverOut = function () {
    $('#IRebutton').mouseover(function () {
        this.src = 'images/go_btn_in.png';
    });
    $('#IRebutton').mouseout(function () {
        this.src = 'images/go_btn_out.png';
    });
};

var setCookie = function (name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    window.document.cookie = name + "=" + value + expires + "; path=/";
};

var getCookie = function (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);
        }
    }

    return null;
};

var likeUsClose = function () {
    $('#faceb').fadeOut('slow');
    setCookie('likeUs', '0', 360);
};

var likeUsLogic = function () {
    var likeUs = getCookie('likeUs');

    if (likeUs == '1' || likeUs == null) {
        setCookie('likeUs', '1', 360);
        $('#faceb').hide();
        setTimeout('$("#faceb").offset({left: ($("#likeUs").offset().left - 190), top: ($("#likeUs").offset().top + 20)});', 4000);
        $('#faceb').hide().delay(4000).fadeIn('slow');
    }
    else {
        $('#faceb').hide();
    }
};

var setWrapperHeight = function () {
    $('#wrapper').height($(window).height() - ($('#wrapper').offset().top));
};

var isQuirksMode = function () {
    var m = (document.compatMode == 'CSS1Compat') ? 'Standards' : 'Quirks';

    if (m == 'Quirks' && (BrowserDetect.browser == "Firefox" || BrowserDetect.browser == "Safari" || BrowserDetect.browser == "Chrome" || BrowserDetect.browser == "Opera")) {
        var link = $("<link>");
        link.attr({
                type: 'text/css',
                rel: 'stylesheet',
                href: 'skins/Skin_5/styleQuirks.css'
        });
        $("head").append( link ); 
    }
};

var defaultsFavoriteSuit = function () {
    $('#name').focus(function () {
        if ($(this).val() == '*Name:') {
            $(this).val('');
        }
    });
    $('#name').blur(function () {
        if ($(this).val() == '') {
            $(this).val('*Name:');
        }
    });
    $('#email').focus(function () {
        if ($(this).val() == '*E-Mail:') {
            $(this).val('');
        }
    });
    $('#email').blur(function () {
        if ($(this).val() == '') {
            $(this).val('*E-Mail:');
        }
    });
    $('#suitSize').focus(function () {
        if ($(this).val() == '*Size:') {
            $(this).val('');
        }
    });
    $('#suitSize').blur(function () {
        if ($(this).val() == '') {
            $(this).val('*Size:');
        }
    });
    $('#txt').blur(function () {
        if ($(this).val() == '') {
            $(this).val('*Your Comment:');
        }
    });
    $('#txt').focus(function () {
        if ($(this).val() == '*Your Comment:') {
            $(this).val('');
        }
    });

    $('#favorite-suit').submit(function () {
        if ($('#txt').val() == '' || $('#txt').val() == '*Your Comment:') {
            $('#txt').focus();
            alert('Comment is Required');
            return false;
        } else if ($('#name').val() == '' || $('#name').val() == '*Name:') {
            $('#name').focus();
            alert('Name is Required');
            return false;
        } else if (!$('#email').val().match(/\b(^(\S+@).+((\.com)|(\.biz)|(\.info)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi)) {
            $('#email').focus();
            alert('Wrong E-Mail Address');
            return false;
        } else if ($('#suitSize').val() == '' || $('#suitSize').val() == '*Size:') {
            $('#suitSize').focus();
            alert('Size is Required');
            return false;
        } 

        $('#send-favorite').fadeOut('slow');

        return true;        
    });
};

$(document).ready(function () {
    isQuirksMode();
    IRebuttonOverOut();
    setCouponPos();
    referalLogic();
    likeUsLogic();
    setWrapperHeight();
    IRebuttonOverOut();

    $(window).resize(function () {
        setWrapperHeight();
        $("#faceb").offset({ left: ($('#likeUs').offset().left - 190), top: ($('#likeUs').offset().top + 20) })
    });
});
