/**
 * Main js routiens of maestrosudio.com.ua website
 * 
 * @author Andriy Parashchenko
 */
var Shop = {
    me: this,
    
    /**
     * Init
     */
    init: function() {
        $(document).ready(function() {
            $('a.price').click(function() { Shop.openOrderPopup(this); return false; });
        });
    },

    /**
     * Open order popup
     */
    openOrderPopup: function(_this) {
        var href = $(_this).attr('href') + '/m/i';
        utils.modals.initModal();
        utils.modals.toSmallModalSize();
        $('#modaIframe').attr('src', href);
        $('#modal').dialog('open');
        
        return false;
    }

};

var fullInfoInit = function () {
    
    /**
     * On load event
     */
    $(document).ready(function() {
        $(utils.bubble.container).change(toggleFullInfo);
        utils.bubble.init();
    });
};


var toggleFullInfo = function () {
    var fullInfoSelector = 'div.otherInformation';
    if ($('#fullInfoCheсkBox:checked').length) {
        $(fullInfoSelector).removeClass('hide');
        utils.setCookie('fullInfo', 1);
        utils.bubble.hide();
        return;
    }
    $(fullInfoSelector).addClass('hide');
    utils.setCookie('fullInfo', 0);
};


/**
 * Utils packet
 */
var utils = {};

/**
 * Set cookie function 
 * 
 * @param name - cookie name
 * @param val  - value to set to cookie
 * @param expiration - num of ours by default - 1year
 * @returns null
 */
utils.setCookie = function(name, val, expiration) {
    if (typeof expiration == 'undefined') {
        expiration = 24 * 365;
    }
    var cookieDate = new Date ();
    cookieDate.setTime(cookieDate.getTime() + (360 * expiration));
    document.cookie = escape(name) + "=" + escape(val) + ";expires=" + cookieDate.toGMTString();
}

/**
 * Get cookie by cookie name
 * 
 * @param cookieName
 * @returns
 */
utils.getCookie = function (cookieName) {
    var results = document.cookie.match ( '(^|;) ?' + cookieName + '=([^;]*)(;|$)' );
    if (results) {
        return (unescape(results[2]));
    }
    return null;
}

/**
 * Info bubble element
 */
utils.bubble = {
    
    /**
     * Conteiner where bubble is placed
     */
    container: '#fullInfoContainer',
    
    /**
     * Bubble identifier
     */
    elementId: 'div.infoBubble',
    
    /**
     * Timeout to hide bubble
     */
    hideTimeOut: 5000,
    
    init: function() {
        var el = $(this.container + ' ' + this.elementId);
        if (el.length > 0) {

            /**
             * Hide info bubble after 10 sec
             */
            setTimeout(utils.bubble.hide, this.hideTimeOut);
            
            /**
             * Set cookie hide bubble
             * Don't show bubble more
             */
            el.find('button').click(function(){
                utils.setCookie('hideBubble', 1);
                utils.bubble.hide();
            });
        }
    },
    
    hide: function () {
        var _this = utils.bubble;
        var el = _this.container + ' ' + _this.elementId;
        $(el).fadeOut('slow');
    }
    
};

utils.modals = {
        
    /**
     * Init modal windows
     */
    initAlert: function () {
        $("#alert").dialog({
            bgiframe: true,
            autoOpen: false,
            resizable: false,
            modal: true,
            overlay: {
                opacity: 0.5
            },
            buttons: {
                'Ok': function() {
                    $("#alert").dialog('close');
                }
            }
        });
    },
    
    /**
     * Init modal windows
     */
    initModal: function () {
        if ($("#modal").length <= 0) {
            $('body').append('<div id="modal"><iframe id="modaIframe" frameborder="0" src="" class="modalIframe"></iframe></div>');
        }
        $("#modal").dialog({
            bgiframe: true,
            autoOpen: false,
            resizable: false,
            width: 600,
            modal: true,
            overlay: {
                opacity: 0.5
            }
        });
    },

    /**
     * Init modal windows
     */
    initSmallModal: function () {
        if ($("#modalSmall").length <= 0) {
            $('body').append('<div id="modalSmall"><iframe id="modalSmallIframe" frameborder="0" src="" class="modalSmallIframe"></iframe></div>');
        }
        $("#modalSmall").dialog({
            bgiframe: true,
            autoOpen: false,
            resizable: false,
            width: 350,
            modal: true,
            overlay: {
                opacity: 0.5
            }
        });
    },
    
    initDelProductConfirm: function () {
        if ($("#confirmDelProd").length <= 0) {
            $('body').append('<div id="confirmDelProd"><p>Удалить?</p></div>');
        }
        $("#confirmDelProd").dialog({
            bgiframe: true,
            autoOpen: false,
            resizable: false,
            modal: true,
            overlay: {
                opacity: 0.5
            },
            buttons: {
                'Ok': function() {
                    utils.modals.delProduct();
                },
                'Cancel': function() {
                   $(this).dialog('close');
                }
            }
        });
    },
    
    delLink: '',
    
    initDelConfirm: function(){
        this.initDelProductConfirm();
        
        $('a.delete').click(function(){
            utils.modals.delLink = $(this).attr('href');
            $("#confirmDelProd").dialog('open');
            return false;
        });
    },
    
    delProduct: function(){
        if(this.delLink != '') {
            document.location = this.delLink;
            this.delLink = '';
        }
    },

    closeEditItems: function () {
        $("#modal").dialog('close');
    },
    
    initConfirm: function () {
        $("#confirm").dialog({
            bgiframe: true,
            autoOpen: false,
            resizable: false,
            modal: true,
            overlay: {
                opacity: 0.5
            },
            buttons: {
                'Ok': function() {
                    confirmCallBack();
                },
                'Cancel': function() {
                   $(this).dialog('close');
                }
            }
        });
    },
    
    initFileSelect: function (url) {
        
        var img = $("#tender_img").val();
        if (img.length > 0) {
            selImage(img, url)
        }
        
        if ($("#fileSelect").length <= 0) {
            $('body').append('<div id="fileSelect"></div>');
        }
        $("#fileSelect").dialog({
            bgiframe: true,
            autoOpen: false,
            resizable: false,
            width: 600,
            modal: true,
            overlay: {
                opacity: 0.5
            },
            buttons: {
                'Cancel': function() {
                   $(this).dialog('close');
                }
            }
        });
        $('#selImageButton').click(function(){
            selectImage();
        });
    },
    
    selectImage: function () {
        var url = $('input[name=user_image_dest]').val();
        $("#fileSelect").html('<iframe frameborder="0" src="' + url + '" name="modalWindow" class="modalIframe"></iframe>')
        .dialog('open');
        
    },
    
    selImage: function (img, url) {
        $("#tender_img").val(img);
        if ($("#tenderImg").length <= 0) {
            $("#tender_img-element").append('<img src="" id="tenderImg"  onclick="selectImage()" />');
        }
        $("#tenderImg").attr('src', url + '/small/' + img);
        $("#tender_img").val(img);
        $("#fileSelect").dialog('close');
    },
    
    hideBanner: function (img) {
        var el = document.getElementById('img');
        el.innerHTML = '<input type="hidden" name="image" id="image" value="" /><br><input type="button" value="Выбрать фото" onClick="selectImage();" >';
    },
    
    /**
     * resize window to small
     */
    toSmallModalSize: function(){
        parent.$('#modaIframe').addClass('smallModal').removeClass('middleModal').removeClass('guestConfirm');
        parent.$("#modal").dialog('option', {'width':400, 'position':'center'});
    },

    /**
     * resize window to big
     */
    toBigModalSize: function(){
        parent.$('#modaIframe').removeClass('smallModal').removeClass('middleModal').removeClass('guestConfirm');
        parent.$("#modal").dialog('option', {'width':600, 'position':'center'});
    },

    /**
     * resize window to middle
     */
    toMiddleModalSize: function(){
        parent.$('#modaIframe').addClass('middleModal').removeClass('smallModal').removeClass('bigModal').removeClass('guestConfirm');
        parent.$("#modal").dialog('option', {'width':480, 'position':'center'});
    }
}

/**
 * Hover 
 */
utils.hover = {
    
    hoverClass: '.trhover',
    
    init: function(hClass){
        var _this = this;
        var hClass = hClass || _this.hoverClass;
        $(document).ready(function(){
            $(hClass).hover(_this.addClass, _this.removeClass);
        });
    },
    
    addClass: function(){ 
        $(this).addClass("hover"); 
    },
    
    removeClass: function(){ 
        $(this).removeClass("hover");
    }

}



