jQuery(document).ready(function() {
    
    $(".trhover").hover(
        function(){ $(this).addClass("ui-state-hover")},
        function(){ $(this).removeClass("ui-state-hover")
    });
    
    $("#productMenu li").hover(
        function(){$(this).addClass("hover");}, 
        function(){$(this).removeClass("hover");}
    );
    
    if (!$('#wait').length) {
        $('body').append('<div id="wait" class="wait ui-overlay"><div class="ui-widget-overlay"></div></div>');
    }
    
    $("a.zoom").lightBox({
        imageLoading: fullUrl + "/images/lightbox-ico-loading.gif",
        imageBtnPrev: fullUrl + "/images/lightbox-btn-prev.gif",
        imageBtnNext: fullUrl + "/images/lightbox-btn-next.gif",
        imageBtnClose: fullUrl + "/images/lightbox-btn-close.gif",
        imageBlank:    fullUrl + "/images/lightbox-blank.gif"
    });

    if ($('.login-but').length) {
        initModal();
        $(".login-but").click(function(){
            var url = $(this).attr('href');
            $('#modalIframe').attr('src', url + '/m/i');
            $('#modal').dialog('open');
            return false;
        });
    }
});

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

/**
 * Init modal windows
 */
function initSmallModal() {
    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
        }
    });
}
function closeEditItems () {
    $("#modal").dialog('close');
}
function initConfirm() {
    $("#confirm").dialog({
        bgiframe: true,
        autoOpen: false,
        resizable: false,
        modal: true,
        overlay: {
            opacity: 0.5
        },
        buttons: {
            'Ok': function() {
                confirmCallBack();
            },
            'Cancel': function() {
               $(this).dialog('close');
            }
        }
    });
}
function initFileSelect(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();
	});
}
function selectImage() {
	var url = $('input[name=user_image_dest]').val();
    $("#fileSelect").html('<iframe frameborder="0" src="' + url + '" name="modalWindow" class="modalIframe"></iframe>')
    .dialog('open');
    
}
function selImage(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');
}
function hideBanner(img) {
    var el = document.getElementById('img');
    el.innerHTML = '<input type="hidden" name="image" id="image" value="" /><br><input type="button" value="Выбрать фото" onClick="selectImage();" >';
}


function initDelProductConfirm() {
    $("#confirmDelProd").dialog({
        bgiframe: true,
        autoOpen: false,
        resizable: false,
        modal: true,
        overlay: {
            opacity: 0.5
        },
        buttons: {
            'Ok': function() {
                delProduct();
            },
            'Cancel': function() {
               $(this).dialog('close');
            }
        }
    });
}

/**
 * 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() + ";path=/";
}

/**
 * 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;
}

/**
 * Make stack to save last shown products.
 */
utils.pushIdToCookie = function(id) {
    var cookieName = 'lastProd';
    
    /**
     * Max num of saved el
     */
    var maxSize    = 5;
    var separator  = "/";
    var prArray    = new Array(); 
    var pr = utils.getCookie(cookieName);
    if (pr != null) {
        prArray = pr.split(separator);
        var saveTo = true;
        for (i in prArray) {
            
            /**
             * Check is id already set.
             */
            if (prArray[i] == id) {
                saveTo = false;
            }
        }
        if (saveTo) {
            prArray.unshift(id);
        }
    }
    pr = prArray.slice(0, maxSize);
    utils.setCookie(cookieName, pr.join(separator));
}
