$(function(){

	// add class 'js' to body element for progressive enhacement if JS is enabled
	$('body').addClass('js').css('display','block');

	// placeholder de precarga de imagenes (alternativa a una version en CSS)
	$('#mainImages img, body.photo-gallery #main img, body.news #main img').preload({
	    placeholder:'/images/loading-home.gif',
		onFinish: cycle
	});

    // jQuery Cycle plugin	
	function cycle(){
		$('#mainImages ul').cycle({fx: 'fade', timeout: 5500});
	}
	
	// placeholder de precarga de imagenes (alternativa a una version en CSS)
	$('div.product-panel .product-image img').preload({
	    placeholder:'/images/loading-prod.gif',
		onFinish: image_data
	});
	
	function image_data(){
		$('div.product-panel').each(function(){
			var $this = $(this),
			image_src = $('.product-image img', $this).attr('src');
			$this.data('main_image', {src: image_src});
		});
	}
	
	//  localScroll for #nav
	$('#nav').localScroll({
		target: '#nav',
		axis:'x',
		duration:250,
		hash:false
	});
	$('#nav-2').localScroll({
		target: '#nav-2',
		axis:'x',
		duration:250,
		hash:false
	});
			
	
	//  localScroll for body.photo-gallery #main and body.news #main  and body.products #main .panel-scroller
			
	$('body.photo-gallery #main, body.news #main, body.products #main div.panel-scroller').localScroll({
		axis:'x',
		duration:750,
		hash:false,
		offset:-120
	});
	
	//  localScroll for body.products #main
	// inicializando algunas cosas antes de aplicar el scroll.
	
	$('body.article-list #main .product-panel .product-extra-images').css({"opacity":"0","display":"block"});
	$('body.article-list #main .product-panel .product-text').css({"opacity":"0","display":"block"}).prepend('<div class="close">close</div>');;
	
	$('body.products #main .product-panel').localScroll({
		axis:'x',
		duration:750,
		hash:false,
		offset:-265,
		onBefore: function(e, target, scrolled) {
					var $target = $(target),
						current_width = $('#wrapper').width();
						no_extra_images_width = current_width + 248;
						has_extra_images_width = current_width + 348;
					if (!$target.hasClass('open')) {
						if ($target.hasClass('has-extra-images')) {
							$('#wrapper, #content').width(has_extra_images_width);
						} else {
							$('#wrapper, #content').width(no_extra_images_width);
						}
					}
				},
		onAfter: function(el) {
					var $this = $(el),
						$text_column = $('div.text-column', $this),
						$product_text = $('div.product-text', $text_column);
					if ($this.hasClass('has-extra-images')) {
						w = "348px";
					} else {
						w = "248px";
					};
					if (!$this.hasClass('open')) {
						$text_column.css({"display":"block"}).animate({"width": w}, "normal", "linear", function(){
							$product_text.css({"display":"block"}).animate({'opacity':'1'}, function(){
								/* fix IE bold font rendering issue */
								if ($.browser.msie)
									$product_text.get(0).style.removeAttribute('filter');
								$('div.product-extra-images', $this).css({"display":"block"}).animate({"opacity":"1"}, "slow");
								$this.addClass('open');
							});
						});
					}
				}
	});
				
	// cerrar el panel de informacion de un producto
	$('div.product-panel').click(function(event) {
		var $tgt = $(event.target),
			$this = $(this),
			$text_column = $('div.text-column', $this),
			current_width = $('#wrapper').width();
			if ($this.hasClass('has-extra-images')) {
				new_width = current_width - 348 + "px";
			} else {
				new_width = current_width - 248 + "px";
			}
		if ($tgt.hasClass('close')) {
			if ($this.hasClass('has-extra-images')) {
				$('div.product-extra-images', $this).animate({'opacity':'0'}, "slow", "linear", function(){
					$('div.product-extra-images', $this).css({'display':'none'});
					$('div.product-text', $text_column).animate({'opacity':'0'}, "slow", "linear", function() {
						$('div.product-text', $text_column).css({"display":"none"});
						$text_column.animate({"width":"0px"}, "normal", "linear", function(){
							$('.product-image img', $this).attr('src', $this.data('main_image').src);
							$text_column.css({"display":"none"});
							$('#content').width(new_width);
							$('#wrapper').animate({"width": new_width}, "normal", "linear", function(){
								$this.removeClass('open');
							});
						});
					});
				});
			} else {
				$('div.product-text', $text_column).animate({'opacity':'0'},"slow", "linear", function() {
					$('div.product-text', $text_column).css({"display":"none"});
					$text_column.animate({"width":"0px"}, "normal", "linear", function(){
						$text_column.css({"display":"none"});
						$('#content').width(new_width);
						$('#wrapper').animate({"width": new_width}, "normal", "linear", function(){
							$this.removeClass('open');
						});
					});
				});
			}
		}
	});

	
	/* jQuery.Preload  | http://demos.flesler.com/jquery/preload */
	var $links = $('div.product-panel div.product-extra-images a');
	//this mode doesn't require any setting, can have though
	$links.preload();	
	$links.click(function(){
		var $context = $(this).parents('div.product-panel');
		var $preview = $('.product-image img', $context);
		$preview.attr('src', this.href);
		return false;
	});
	
	function vertical_center(){
		$("#wrapper").vCenter();	
	}

	$(window).resize(function(){
		$("#wrapper").vCenter();
	});
	/* It seems that running it as a function fixes some weird behaviour (sometimes it wasn't centering the content) on Safari */
    vertical_center();
});

// creates abbr tag on IE6
if ($.browser.msie && $.browser.version < 7) {
    document.createElement("abbr");
}
