$(window).ready(function(){
//External Links
$('a[rel="external"]').click(function(){
	this.target = "_blank";
});
//Preload CSS Images
//$.preloadCssImages();
//COLUMNIZE TEXT
$('.wideText').columnize({width:325});


//MENU SITE FUNCTIONS
/***********************************************************************************************************************************************************/
//On Hover Over
function megaHoverOver(){
    $(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
    (function($) {
        //Function to calculate total width of all ul's
        jQuery.fn.calcSubWidth = function() {
            rowWidth = 0;
            //Calculate row
            $(this).find("ul").each(function() { //for each ul...
                rowWidth += $(this).width(); //Add each ul's width together
            });
        };
    })(jQuery); 

    if ( $(this).find(".row").length > 0 ) { //If row exists...

        var biggestRow = 0;	

        $(this).find(".row").each(function() {	//for each row...
            $(this).calcSubWidth(); //Call function to calculate width of all ul's
            //Find biggest row
            if(rowWidth > biggestRow) {
                biggestRow = rowWidth;
            }
        });

        $(this).find(".sub").css({'width' :biggestRow}); //Set width
        $(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin

    } else { //If row does not exist...

        $(this).calcSubWidth();  //Call function to calculate width of all ul's
        $(this).find(".sub").css({'width' : rowWidth}); //Set Width

    }
}
//On Hover Out
function megaHoverOut(){
  $(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
      $(this).hide();  //after fading, hide it
  });
}
//Set custom configurations
var config = {
     sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)
     interval: 10, // number = milliseconds for onMouseOver polling interval
     over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
     timeout: 10, // number = milliseconds delay before onMouseOut
     out: megaHoverOut // function = onMouseOut callback (REQUIRED)
};

$("ul#topnav li .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
$("ul#topnav li").hoverIntent(config); //Trigger Hover intent with custom configurations

//END MENU SITE FUNCTIONS
/***********************************************************************************************************************************************************/



//OVER BRAND SIV ANIMATION IMAGES GALLERY BEGIN
/***********************************************************************************************************************************************************/
$(".hoverBrandDiv").fadeTo(300, 0);
$(".hoverBrandDiv").hover(function(){
	$(this).fadeTo(300, 0.9);
},function(){
	$(this).fadeTo(300, 0);
});

$(".hoverBrandDivProduct").fadeTo(300, 0);
$(".hoverBrandDivProduct").hover(function(){
	$(this).fadeTo(300, 0.9);
},function(){
	$(this).fadeTo(300, 0);
});




/***********************************************************************************************************************************************************/
//OVER BRAND SIV ANIMATION IMAGES GALLERY END



	
	/***********************************************************************************************************************************************************/
//PRETTYPHOTO START

	$("a[rel^='gallery']").prettyPhoto({
		animationSpeed: 'normal', // fast/slow/normal 
		horizontal_padding: 20, /* The padding on each side of the picture */
		opacity: 0.75, // Value betwee 0 and 1 
		showTitle: false, // true/false 
		allowresize: true, // true/false 
		counter_separator_label: '/', // The separator for the gallery counter 1 "of" 2 
		theme: 'dark_rounded',
		social_tools:false,// light_rounded / dark_rounded / light_square / dark_square 
		callback: function(){}
	});
/***********************************************************************************************************************************************************/
//PRETTYPHOTO END



//MAKE HEIGHTS WORK FOR DIV'S BEGIN
/************************************************************************************************************************************************************/
var height = $("#brandItensContainer").height();
var heightContainer = $("#brandsItensList").height();

if(heightContainer<height){
	$("#brandsItensList").height(height);}

var height = $(".containerColumns").height();
var heightContainer = $(".containerColumns .column1").height();

if(heightContainer<height){
	height = height -50;
	$(".containerColumns .column1").height(height);
}

//MAKE HEIGHTS WORK FOR DIV'S END
/*************************************************************************************************************************************************************/





	
	
//SMALL IMAGES GALLERY BEGIN
/*************************************************************************************************************************************************************/
// initialize scrollable
$("#smallImages a").click(function(){
								  
	var newImage = $(this).attr('rev');
	var newImageTitle = $(this).attr('title');
	var oldImage = $("#bigImage img").attr('src');
	
	if(newImage!="" && newImage!=oldImage){
		$("#bigImage img").fadeOut(100, "linear").attr('src',newImage).attr('alt',newImageTitle).fadeIn(400);
	}
	return false; //Prevent the browser jump to the link anchor
});
/*************************************************************************************************************************************************************/
//SMALL IMAGES GALLERY END



//SHOPPING CART HIDE SHOW
/*************************************************************************************************************************************************************/
// initialize scrollable
var showShoppingCart = 0;
var heightDiv = $("#shoppingCartContent").height();
var heightMinus = 38;
var animate =79;

//$("#shoppingCart").css("bottom","-"+animate+"px");


$("#shoppingCart").css("display","block");



$("#showHideShoppingCart").add("#keepShopping").click(function(){
	
	if(showShoppingCart==1){
	$("#shoppingCart").stop().animate({'bottom':'-'+79+'px'},{queue:false, duration:500});
	showShoppingCart = 0;
    $.cookie("example", 1);
	} else if(showShoppingCart==0){
	$("#shoppingCart").stop().animate({'bottom':'0'},{queue:false, duration:500});
	showShoppingCart = 1;
	$.cookie("example", null);
	}
	
	return false; //Prevent the browser jump to the link anchor
});


$('#addButtonItem').click(function(e) {
	$.cookie("example", 0);
	//alert("asdas");
	
	//$("#shoppingCart").stop().animate({'bottom':'0'},{queue:false, duration:500});
    $('.jcart').live('submit',function(e){ });
	$('.jcart').trigger('submit'); 
	location.reload();
	//$('.jcart').live('submit',function(e){ });
   

});


$('.jcart').submit(function() {
	//$.cookie("example", 0);
	
	
	
	// Assign handlers immediately after making the request,
  // and remember the jqxhr object for this request
  var jqxhr = $.get('/ajaxJcart.php', function(data) {
    $("#itemsShoppingCart").html(data);
	//alert(data);
  })
	
	 .success(function() { 
	 	$("#blocoShopping").css("display","block");
		$("#shoppingCart").css("display","block");
		showShoppingCart = 1;
		$("#shoppingCart").stop().animate({'bottom':'0'},{queue:false, duration:500}); 
	})


	/*
	$.get('/ajaxJcart.php', function(data) {
  		$("#itemsShoppingCart").html(data);
		$("#blocoShopping").css("display","block");
		$("#shoppingCart").css("display","block");
		showShoppingCart = 1;
		$("#shoppingCart").stop().animate({'bottom':'0'},{queue:false, duration:500});
	});
*/
	
	
	
});


if($.cookie("example")==0){
	$("#shoppingCart").css("bottom","0px");
	$.cookie("example", 1);
	showShoppingCart = 1;
}



/*************************************************************************************************************************************************************/
//SMALL IMAGES GALLERY END



//SCROLLABLE BEGIN
/*************************************************************************************************************************************************************/
// initialize scrollable
//$(".scrollable").scrollable({ circular: true }).navigator(".navi");

if(circleGallery==1){
$(".scrollable").scrollable({circular: true, mousewheel: true,onSeek: function(event, i) 
		{
			$("#textInfoScrollable").html("asdas");
			var aux ="#textInfoScrollableInfo"+this.getIndex();
			var htmlParse = $(aux).html();
			$("#textInfoScrollable").html(htmlParse);
			
		}}).autoscroll({
		interval: 7000		
	});
}
else
{
	if(noArrows == 1)
	{
		
		


		//$(".scrollable").scrollable({ circular: true });
		$(".scrollable").scrollable({
		circular: true,
		onSeek: function(event, tabIndex) {
			var api = $(".scrollable").data("scrollable");
			numberActual = api.getIndex()+1;
			if(numberActual<=9){numberActual="0"+numberActual;}
			$("#actualNumber").html(numberActual);
		}							
		});

		
		var api = $(".scrollable").data("scrollable");
		if(api){
			numberTotal = api.getItems().size();
			if(numberTotal>1)
			{
				$("#flowtabs").css("display","block");
				if(numberTotal<=9){numberTotal="0"+numberTotal;}
				$("#leftNumbers").html(numberTotal);
				numberActual = api.getIndex()+1;
				if(numberActual<=9){numberActual="0"+numberActual;}
				$("#actualNumber").html(numberActual);
			}
		}

		//$("#actualNumber").html($(".scrollable").getIndex());
		//$("#leftNumbers").html($(".scrollable").getsize());
		/*
			<div class="navi" id="flowtabs">
                            <a class="prev browse left"></a>
                            <div class="numbers"><span id="actualNumber">03</span> / <span id="leftNumbers">99</span></div>
                            <a class="next browse right"></a></div>
                        </div><!--END navi-->
				*/		
						
	}
}

//Scrollable Arrows
/*$("div.scrollable").hover(
  function () {
	$("div.linkArrow").fadeIn(500);
  },
  function () {
	$("div.linkArrow").fadeOut(500);
  }
);*/
/*************************************************************************************************************************************************************/
//SCROLLABLE END






//SCROLL HORIZONTAL BEGIN
/*************************************************************************************************************************************************************/
$("#content-slider").slider({
	animate: true,
	change: handleSliderChange,
	slide: handleSliderSlide
});

function handleSliderChange(e, ui)
{
  var maxScroll = $("#content-scroll").attr("scrollWidth") - $("#content-scroll").width();
  $("#content-scroll").animate({scrollLeft: ui.value * (maxScroll / 100) }, 1000);
}

function handleSliderSlide(e, ui)
{
  var maxScroll = $("#content-scroll").attr("scrollWidth") - $("#content-scroll").width();
  $("#content-scroll").attr({scrollLeft: ui.value * (maxScroll / 100) });
}


//END SCROLL HORIZONTAL BEGIN
/*************************************************************************************************************************************************************/


/*VALIDATE registerConfirmCountry*/
$('#registerConfirmCountry').click(function(e) {
	$('#formCountry').submit();
	
});

/*END VALIDATE registerConfirmCountry*/


/*CLICK ON hover Brand Product Link*/

$(".hoverBrandDivProduct").click(function(event) {
	  event.preventDefault();
	  var str = $(this).find("a").attr("href");
	  if(str.length > 0){
	  window.location = $(this).find("a").attr("href");
	  }
	});





//SLIDER HORIZONTAL BEGIN
/*********************************************************************************************************************************************************************/
/*$(function() {
        
		//vars
		var conveyor = $(".content-conveyor", $(".sliderContent")),
		item = $(".item", $(".sliderContent"));
		
		//set length of conveyor
		conveyor.css("width", item.length * parseInt(item.css("width")));
				
        //config
        var sliderOpts = {
		  max: (item.length * parseInt(item.css("width"))) - parseInt($(".viewer", $(".sliderContent")).css("width")),
		  value:0,
          slide: function(e, ui) { 
            conveyor.css("left", "-" + ui.value + "px");
          }
        };

        //create slider
        $("#slider").slider(sliderOpts);
      });*/
/*********************************************************************************************************************************************************************/
//SLIDER HORIZONTAL END

});



