var TB = TB || {};

TB.Fit = VG.utils.Fit;
TB.Center = VG.utils.Center;
TB.CenterPointer = VG.utils.CenterPointer;
TB.Zenone = VG.utils.Zenone;
TB.isStarted = true;

$(document).ready(function() {
	
	TB.findDevice();
	TB.startSlider();
	TB.randomBackground();
	TB.startTabs();
	TB.scrollable();
	TB.resize();
	TB.formsValidator();
	TB.subNavCollection();
	
	$(".radio-coll a").click(function() {								  
		$(this).next().children(".radio").attr("checked", "checked");
		return false;
	});
	
});

$(window).load(function() {
	$('.pages').css("visibility", "visible");
	$('#preloader').hide();
});

$(window).resize(function() {
	TB.resize();
});

$(window).scroll(function() {
	TB.resize();
});

TB.resize = function() {
	
	var win = $(window);
	
	var container = $('#container');
	var scroller = $('#main');
	var section = $('.section');
	var divider = $('.divider');
	
	var wh = win.height();
	var ww = win.width();
	var sh = wh - 25;
	
	container.height( wh );
	scroller.height( sh );
	section.height( sh );
	divider.height( sh );
	
	switch (TB.device) {
		case VG.C.IPAD:
		case VG.C.IPHONE:
		case VG.C.IPOD:
		case VG.C.ANDROID:
		
			divider.width(0);
		
		break;
		
		case VG.C.WIN:
		case VG.C.MAC:
		case VG.C.SOME_DEVICE:
			
			var dw = 1000;	
			var sw = section.width();
			
			if (ww > sw) {
				divider.width( dw + (ww - sw) );			 
			}
			
		break;
	}
	
	
	if (!TB.isStarted) {
	
		var scrollable = $("#main").data("scrollable");
	
		if(scrollable) {
			scrollable.seekTo( scrollable.getIndex(), 0 );
		}
	}
}

TB.subNavCollection = function() {
	
	$("#close-sub-nav a").click(function() {
		$("#id-navigator").slideUp("fast");
		return false;
		
	});

};

TB.colorNumber = function(color) {
	$('.numero-colorato').css('background', color);	
}


TB.startTabs = function() {
	
	TB.aUrbanColors = new Array();		
	TB.aUrbanColors[0] = "#D2DFEA";
	TB.aUrbanColors[1] = "#F5B25E";
	TB.aUrbanColors[2] = "#9E5448";
	
	$("#tabs-urban").children("li").each(function(i) {
		$('a', this).css('color', TB.aUrbanColors[i]);	
		$("#panes-urban").children("div.panes-child-item").each(function(j) {
			$('.btn-go a', this).css('background', TB.aUrbanColors[j]);	
		});	
	});
	
	$(".box-collections #tabs-urban").tabs("#panes-urban > div.panes-child-item", {
		effect: 'fade'
	});
	
	
	/* ---------------------------------------------- */
	
	TB.aBontonColors = new Array();		
	TB.aBontonColors[0] = "#DBDE8A";
	TB.aBontonColors[1] = "#F5E15E";
	TB.aBontonColors[2] = "#C2A1DC";
	
	$("#tabs-bonton").children("li").each(function(i){
		$('a', this).css('color', TB.aBontonColors[i]);
		$("#panes-bonton").children("div.panes-child-item").each(function(j) {
			$('.btn-go a', this).css('background', TB.aBontonColors[j]);	
		});	
	});
	
	$(".box-collections #tabs-bonton").tabs("#panes-bonton > div.panes-child-item", {
		effect: 'fade'
	});
	
	/* ---------------------------------------------- */
	
	TB.aContemporaryColors = new Array();		
	TB.aContemporaryColors[0] = "#F8F185";
	TB.aContemporaryColors[1] = "#E5DE74";
	TB.aContemporaryColors[2] = "#F295F3";
	
	$("#tabs-contemporary").children("li").each(function(i){
		$('a', this).css('color', TB.aContemporaryColors[i]);
		$("#panes-contemporary").children("div.panes-child-item").each(function(j) {
			$('.btn-go a', this).css('background', TB.aContemporaryColors[j]);	
		});	
	});
	
	$(".box-collections #tabs-contemporary").tabs("#panes-contemporary > div.panes-child-item", {
		effect: 'fade'
	});
	
	/* ---------------------------------------------- */
	
	TB.aLifestyleColors = new Array();		
	TB.aLifestyleColors[0] = "#F1F885";
	TB.aLifestyleColors[1] = "#CCCCCC";
	TB.aLifestyleColors[2] = "#B8E4BE";
	TB.aLifestyleColors[3] = "#E8DB4E";
	
	$("#tabs-lifestyle").children("li").each(function(i){
		$('a', this).css('color', TB.aLifestyleColors[i]);
		$("#panes-lifestyle").children("div.panes-child-item").each(function(j) {
			$('.btn-go a', this).css('background', TB.aLifestyleColors[j]);	
		});	
	});
	
	$(".box-collections #tabs-lifestyle").tabs("#panes-lifestyle > div.panes-child-item", {
		effect: 'fade'
	});
	
	TB.aTabsAllColors = TB.aUrbanColors.concat( TB.aBontonColors, TB.aContemporaryColors, TB.aLifestyleColors );
	
}

TB.startSlider = function() {
	if ( $('#slider').length > 0 ) {
		var $nivoSlider = $('#slider').nivoSlider( { 
			effect:'fold',
			slices:10,
			pauseTime:10000,
			directionNavHide:false
		 } );
	 
		 $('.nivo-controlNav a').append( '<span>&nbsp;/&nbsp;' +$('#slider').children("img").length + '</span>');
		 
	}
	
	var props;
		
	switch (TB.device) {
		case VG.C.IPAD:
		case VG.C.IPHONE:
		case VG.C.IPOD:
		case VG.C.ANDROID:
		
			props = {			
				'fitType':VG.C.FIT_IN	
				,'closePuls':'assets/imgs/zoom-close.png'
				,'containerId':'zoomHolder'
				,'bgColor':'#FFF'
				,'scrollMode':'fixed'
				,'bBoxMargin':{'marginX':'-20','marginY':'-20'}
			};
		
		break;
		
		case VG.C.WIN:
		case VG.C.MAC:
		case VG.C.SOME_DEVICE:
			
			props = {			
				'fitType':VG.C.FIT_OUT	
				,'closePuls':'assets/imgs/zoom-close.png'
				,'containerId':'zoomHolder'
			};
			
		break;
	}
	
	$(".zoom-btn a").click(function() {		
		var slideNum = Number($('#slider').data('nivo:vars').currentSlide);		
		var root = document.getElementById('slider');
		var all = root.getElementsByTagName('*')[slideNum];
		var imgUrl = all.src;
		imgUrl = imgUrl.replace('slide', 'big');
		imgUrl = imgUrl.replace('slide', 'big');
		var z = new VG.Zoomer("myZoomer", imgUrl, props);
		return false;
	});
}

TB.findDevice = function() {
	TB.device = VG.utils.SniffDevice();
}

TB.scrollable = function() {
	
	$("#main").scrollable( {
			keyboard: 'static',
			circular: false, 
			mousewheel: false,
			speed: 0,
			onBeforeSeek: function() {
				if (!TB.isStarted) $("#main").data("scrollable").getConf().speed = 800;
				TB.isStarted = false;
			}
			
		 	}).navigator ({
		 		navi: "#navigation",
				naviItem: 'a',
				activeClass: 'current',
				history: true
	});
	
	$(".news-preview a").click( function() {		
		$("#main").data("scrollable").move(1);
	});
	
	
}

TB.formsValidator = function() {
	
	if ( $('#form-contatti').length > 0 ) {
		$("#form-contatti").validate({
						   
			rules: {
				nome: "required",	
				email: { required: true, email: true },
				messaggio: "required"
			},
			messages: {
				nome: "",
				email: "",
				telefono: "",
				messaggio: ""
			},
			submitHandler: function() {
				var results =
				"&nome=" + encodeURI($("#nome").val()) +
				"&email=" + encodeURI($("#email").val()) +
				"&telefono=" + encodeURI($("#telefono").val()) +
				"&messaggio=" + encodeURI($("#messaggio").val());
				
				$.ajax({
					url: "assets/mailer/contatti.php?lang="+lang,
					data: encodeURI(results),
					type: 'post',
					success: function (j) {
						
						$("#nome").val("");
						$("#email").val("");
						$("#telefono").val("");
						$("#messaggio").val("");
						
						$('#form-contatti').hide();
						$('#contatti-container').append(j);
						$('.back-mail-contatti').click(function() {
							$('.mail-responce-contatti').remove();
							$('#form-contatti').show();
							return false;
						});
					},
					error: function (xhr, desc, er) {
						alert("L'iscrizione non è andata a buon fine, riprova.");
					}
				});
				
				return false;
				
			},
			onkeyup: false
		});
		
	}
		
		
	if ( $('#form-rivenditori').length > 0 ) {
		
		$("#form-rivenditori").validate({
			   
			rules: {
				rnome: "required",	
				remail: { required: true, email: true },
				rcomune: "required"
			},
			messages: {
				rnome: "",
				remail: "",
				rtelefono: "",
				rcomune: ""
			},
			submitHandler: function() {
				var results =
				"&nome=" + encodeURI($("#rnome").val()) +
				"&email=" + encodeURI($("#remail").val()) +
				"&telefono=" + encodeURI($("#rtelefono").val()) + 
				"&province=" + encodeURI($("#province").val())+
				"&comune=" + encodeURI($("#rcomune").val())+
				"&collezione=" + encodeURI($("input[@name=collezione]:checked").val());
				
				$.ajax({
					url: "assets/mailer/rivenditori.php?lang="+lang,
					data: encodeURI(results),
					type: 'post',
					success: function (j) {
						
						$("#rnome").val("");
						$("#remail").val("");
						$("#rtelefono").val("");
						$("#rcomune").val("");
						
						$('#form-rivenditori').hide();
						$('#rivenditori').append(j);
						$('.back-mail-rivenditori').click(function() {
							$('.mail-responce-rivenditori').remove();
							$('#form-rivenditori').show();
							return false;
						});
					},
					error: function (xhr, desc, er) {
						alert("L'iscrizione non è andata a buon fine, riprova.");
					}
				});
				
				return false;
				
			},
			onkeyup: false
		});
	
	}
	
	
}

TB.randomBackground = function() {
	
		var $aPatterns = new Array();
		
		$aPatterns[0] = "vert-line-gray.png";
		$aPatterns[1] = "oriz-line.png";
		$aPatterns[2] = "oriz-line-02.png";
		$aPatterns[3] = "oblique-01.png";
		$aPatterns[4] = "vert-line-03.png";
		$aPatterns[5] = "vert-line-02.png";
		$aPatterns[6] = "oriz-line-03.png";
		
		var randNum = Math.floor ( Math.random() * $aPatterns.length + 1 );
		
		$("#cover").css( { background: "url( assets/imgs/pattern/" + $aPatterns[randNum - 1] + ") #000 " } );
		
		$white = "logo-big-white.png";
		$dark = "logo-big-dark.png";
		
		if (randNum > 4) {
			$('#logo-cover').attr('src', "assets/imgs/" + $white);
			$('#cover .btn-lang a').css('color', '#FFF');
			$('#cover .slash').css('color', '#FFF');
		} else {
			$('#logo-cover').attr('src', "assets/imgs/" + $dark);	
			$('#cover .btn-lang a').css('color', '#333');
			$('#cover .slash').css('color', '#333');
		}

}

