Model = {
	currentColorBoxElement: null,

    loadPrev: function()
    {
    	if($('#modelId-' + $('#modelinfo').attr('modelId')).prev().length){
    	    $('#modelId-' + $('#modelinfo').attr('modelId')).prev().children('a').click();
    	}  
    },
    
    loadNext: function()
    {
    	if($('#modelId-' + $('#modelinfo').attr('modelId')).next().length){
    	    $('#modelId-' + $('#modelinfo').attr('modelId')).next().children('a').click();
    	}  
    },
    
    loadItem: function(href, lookbookMode)
    {
        $.jsHttpRequest({
             url: href,
             onReady: function(result, text) {
                 //ContentWindow.show('<div class="catalog-content">' + text + '</div>', {'width' : href.indexOf('/newcol/') > 1 ? 543 : 660, 'height': 430, 'target' : $('div#main-window .container'), 'dontHideLookbook' : !!lookbookMode});
                 ContentWindow.show('<div class="catalog-content">' + text + '</div>', {'width' : 528 , 'height': 430, 'target' : $('div#main-window .container'), 'dontHideLookbook' : !!lookbookMode});
                 ContentWindow.reinit();
                 $('.jScrollPaneContainer').css('width', 'auto');
                 $('div#main-window div.control').show();
             }
         });     
    },   
    
    loadItemColorBox: function(href)
    {
    	Model.currentColorBoxElement = $.colorbox.element();
    	$.colorbox.close();
    	Model.loadItem(href, true);
    	$('.close-button').click(function(){
    	   Model.currentColorBoxElement.click();
    	});
    },
    
    fixLoadBug: function()
    {  
        $('#main-window').width(528).height(430);
        ContentWindow.reinit();
        $('.jScrollPaneContainer').css('width', 'auto');
    },
    
    changeImage: function(elem)
    {
		var oldsrc = $('#mainProductImage').attr('src');	
        if($(elem).attr('src').indexOf('_f')) var newsrc = $(elem).attr('src').replace(/_f/, '_b');
        if($(elem).attr('src').indexOf('_b')) var newsrc = $(elem).attr('src').replace(/_f/, '_f');
        
        $(elem).attr('src', oldsrc.replace(/\/big\//, '/small/'));
        $('#mainProductImage').attr('src', newsrc.replace(/\/small\//, '/big/')).parent().attr('href', newsrc.replace(/\/small\//, '/bigger/'));
        
    },
    
    loadCatalog: function(href)
    {
		Gallery.disableMove();
		Lookbook.hide();
        $.jsHttpRequest({
             url: href,
             onReady: function(result, text) {
                 $('.gallery .catalog').remove();
                 $('.gallery .init').hide();
                 $('.gallery').prepend(text);
			     $('.window-href-model').click(
			         function(){
			             Model.loadItem($(this).attr('href'));
			             return false;
			         }
			     ); 
			     Menu.closeAll();
			     ContentWindow.hide(); 
			     
			     var elementWidth = $('.gallery .catalog li:first').width();
			     console.log(elementWidth);
			     if($('.gallery .catalog li').length * elementWidth < $('body').width()){
			         $('.gallery .catalog').css('left', Math.round( (($('body').width() - $('.gallery .catalog li').length * elementWidth) / 2) ) + 'px');
			         $('.leftarrow, .rightarrow').hide();
			     }else{
			     	 $('.gallery .catalog').css('left', '0px');
			         $('.leftarrow, .rightarrow').show();
			     }
			     Gallery.disableMove();
             }
         });     
    },
    
    removeCatalog: function()
    {
        $('.leftarrow, .rightarrow').show();
        $('.gallery .init').show();
        $('.gallery .catalog').remove();
        Gallery._currentGalleryPosition = 0;
		Gallery.enableMove();
        
    },
    
    search: function(catn)
    {
        Model.loadCatalog('/catalog/index/articul/'+catn);
        Model.closeSearchWindow();
    },
    
    showSearchWindow: function()
    {
    	$('#search-window').fadeIn(600);
    	Menu.closeAll();
    },
    
    closeSearchWindow: function()
    {
    	$('#search-window').fadeOut(600);
    } 
}
