
var Catalog = {
  
  //BIND PAGE EVENTS
  'bindEvents' : function() {
        $('.navigator').livequery('click', this.navigate);
        $('.lightbox-page').livequery( 'click', this.showLightboxPage );
        $('.shopping-cart').livequery('click', this.shoppingCart);
        $('.more-info').livequery('click', function () { window.location = $(this).attr('href') });
        $('.toggle').livequery('mouseover', this.changeImage);
        $('.toggle').livequery('mouseout',  this.returnImage);
        $('.toggle').livequery('click',  this.freezeImage);
        $('#change_location_link').livequery('click',  this.changeLocation);
        $('#input_zip_button').livequery('click',  this.setZipCode);
		
    	if($('#product-quantity').size())
        	$('#product-quantity').validation({ type: 'int' });
  },

    'setZipCode': function() {
        zip = $('#zipcode').val();
        ids_line = $('#ids').val();
      	
        $.getJSON(
          '/index.php/catalog/zipcode?zipcode='+zip+ids_line,
          function (data_zip) {
            if( ! data_zip.success){
                window.location.href = "/catalog/contact-dealer";
            }else{
                
                $('#intro').html('<span id="change_location_box">'+zip+' - <a id="change_location_link" href="#">Change Zip Code</a></span>') ;
                
                for (var variable in data_zip )
                  {
                    $('#'+variable).html('Price: $ '+data_zip[variable]);
                  }
                        
                $.getJSON(
                  '/admin/stafforder/update-cart-zip/'+zip,
                  function (data) {
                    if( ! data.success)
                      alert(data.message);
                  }
                );
                
                $('#enter_zip').hide();
                $('#intro').show();
                
            }
          }
        );
    },

    'navigate': function() {
       
        window.location = $(this).attr('href');
    },
    
    // LIGHTBOX PAGE IF ENABLED
    'showLightboxPage': function (  ) {
        href = $(this).attr('href').replace(/\/cart\/add\//, '');
        href = '/messages/lightbox/' + href + (($("#product-quantity").val()) ? $("#product-quantity").val() : '');
        Shadowbox.open({
            player: 'iframe',
            title: '',
            content: href,
            height: 350,
            width: 500
        });
    },
    
    'shoppingCart': function( href ) {
        if (typeof href != 'string') {
            href = $(this).attr('href');
            if ($("#product-quantity").val()) 
                href = href + $("#product-quantity").val();
        }
        result = $.ajax({url: href, data: "", async: false}).responseText;
        if (result == '{ success: 1 }')
            window.location = '/cart';
        else
            alert('Sorry, some error has occured');
    },
    
    'changeImage': function() {
        if (!$("#large-image").attr('currentsrc')) {
            src = $(this).attr('largesrc');
            $("#large-image").attr('currentsrc', src);
            $("#large-image").attr('src', '/upload/images/' + src);
        }
        $("#large-image").attr('src', '/upload/images/' + $(this).attr('largesrc'));
    },
    
    'returnImage': function() {
        $("#large-image").attr('src', '/upload/images/' + $("#large-image").attr('currentsrc'));
    },
    
    'freezeImage': function() {
        src = $(this).attr('largesrc');
        $("#large-image").attr('currentsrc', src);
        $("#large-image").attr('src', '/upload/images/' + src);
    },
    
    'changeLocation': function() {
        $("#intro").hide();
        $("#enter_zip").show();
        $("#change_location_box").html('');
    }
    
};

$(document).ready(function(){
  Catalog.bindEvents();
    
});
