Model = {
	_currentModelsPosition: 0,
	
    loadItem: function(href, catN)
    {
        $.jsHttpRequest({
             url: href,
             onReady: function(result, text) {
                 ContentWindow.show('<div class="catalog-content">' + text + '</div>', {'width' : 553, 'height': 400, 'target' : $('div#main-window .container')});
                 $('#main-window, #main-window-opacity').css('margin-top', '-210px');
                 ContentWindow.reinit();
                 $('.jScrollPaneContainer').css('width', 'auto');
                 $('#catalog-list').hide();
                 $('#catalog-list-small').show();
                 $('#catalog-list-small .model-small-item').removeClass('opacity60');
                 $('#catalog-list-small .models-container a[catn=' + catN + ']').parent().addClass('opacity60');
                 $('.close-button').unbind().click(function(){ 
			    	$('div#main-window, div#main-window-opacity').fadeOut(600);
			    	$('#catalog-list-small').hide();
			    	$('#catalog-list').show();
                  });
             }
         });     
    },   
    
    loadItemLookbook: function(href, catN, key)
    {
        console.log(href);
        $.jsHttpRequest({
             url: href,
             onReady: function(result, text) {
                 ContentWindow.show('<div class="catalog-content">' + text + '</div>', {'width' : href.indexOf('/newcol/') > 1 ? 603 : 660, 'height': 400, 'target' : $('div#main-window .container'), 'dontCloseLookbook': true});
                 $('#main-window, #main-window-opacity').css('margin-top', '-210px');
                 ContentWindow.reinit();
                 $('.jScrollPaneContainer').css('width', 'auto');
				 $('.close-button').unbind().click(function(){
					$('div#main-window, div#main-window-opacity').fadeOut(600);
					$('#lookbook-list-small').show();
					$('#lookbook-list a[key=' + key + ']:first').click();
				 });    

             }
         });     
    },   
    
    
    fixLoadBug: function()
    {  
        $('#main-window').width(553).height(400).css('margin-left', '-290px');
        ContentWindow.reinit();
        $('.jScrollPaneContainer').css('width', 'auto');
    },
    
    changeImage: function(elem)
    {
		var oldsrc = $('#mainProductImage').css('background-image');
		var newsrc = $(elem).css('background-image');
		console.log(oldsrc);
        //if( $(elem).css('background-image').indexOf('-f') ) var newsrc = $(elem).css('background-image').replace(/-f/, '-b');
        //if( $(elem).css('background-image').indexOf('-b') ) var newsrc = $(elem).css('background-image').replace(/-b/, '-f');
        
        
        $(elem).css('background-image', oldsrc.replace(/\/big\//, '/small/'));
        
        $('#mainProductImage').css('background-image', newsrc.replace(/\/small\//, '/big/')).parent().attr('href', newsrc.replace(/\/small\//, '/bigger/').replace(/^url\(["']?/, '').replace(/["']?\)$/, '') );
        
    },
    
    loadCatalog: function(href)
    {
        $.jsHttpRequest({
             url: href,
             onReady: function(result, text) {
                 ContentWindow.hide();
                 ContentWindow.hideBooklet();
                 Lookbook.hide();
                 $('body').prepend(text);
                 $('#catalog-list').show();
			     $('.window-href-model').click(
			         function(){
			             Model.loadItem($(this).attr('href'), $(this).attr('catn'));
			             return false;
			         }
			     ); 
			     Menu.closeAll();
			     //ContentWindow.hide(true); 
			     if($('#catalog-list .model-small-item').length > 10 || $('#catalog-list-small .model-small-item').length > 7 ) {
			        $('.larr, .rarr').show();
			     }
             }
         });     
    },
    
    moveCatalog: function( direction )
    {
        var frp = $('.couple:last').position().left - 3 * $('.couple:last').width();
        var wd = $('.couple:last').width();
        if(
           Math.abs(
            ($('#catalog-list .models-container .models').position().left - $('.couple:last').width()) > frp && direction == 'right')
               || 
           ($('#catalog-list .models-container .models').position().left >= 0 && direction !== 'right')
           ) return;
    	$('#catalog-list .models-container .models').animate(
    	    { right: (direction == 'right' ? '+' : '-') + '=' + ($('#catalog-list .models-container').width() - 2) }, 
    	    { duration: 2000, step: function(now, fx){ if(now > frp-wd-60 || now <= 0)  $('#catalog-list .models-container .models').stop();  } }
    	 );
    },
    
    moveSmallCatalog: function( direction )
    {
        var frp = $('.model-small-item:last').position().left - 5 * $('.model-small-item:last').width();
        var wd = $('.model-small-item:last').width();
        // console.log( $('#catalog-list .models-container .models').position().left, Math.abs($('#catalog-list .models-container .models').position().left - $('.couple:last').width()) , frp);
        /*
        if(
           Math.abs(
            ($('#catalog-list-small  .models').position().left - $('.model-small-item:last').width()) > frp && direction == 'right')
               || 
           ($('#catalog-list-small  .models').position().left >= 0 && direction !== 'right')
           ) return;
    	*/
    	$('#catalog-list-small .models-container .models').animate(
    	    { right: (direction == 'right' ? '+' : '-') + '=' + ($('#catalog-list-small .models-container').width() + 3) }, 
    	    { duration: 2000, step: function(now, fx){ if(now > frp-wd + 0 || now <= 0)  $('#catalog-list-small .models-container .models').stop();  } }
    	 );
    },
    
    removeCatalog: function()
    {
       $('#catalog-list, #catalog-list-small').remove();
    },
    
    search: function(catn)
    {
        Model.loadCatalog('/catalog/index/articul/'+catn);
        Model.closeSearchWindow();
    }
}
