$(document).ready(function() {
	$('a.delete').click(function() {
		if (jQuery(this).attr('title').indexOf("el ") > -1)
			return confirm('\u00BFDesea borrar '+jQuery(this).attr('title')+' seleccionado?');
		else
			return confirm('\u00BFDesea borrar '+jQuery(this).attr('title')+' seleccionada?');
	}); 
	if($.fn.fancybox) {	
		$("a.fancybox").fancybox({ 'overlayShow': true, 'zoomSpeedIn': 300, 'zoomSpeedOut': 300 });
	}
	$('a[rel="external"]').click( function() {
		this.target = "_blank";
	});
	$("#chart-holder-items").click(function() {
		if ($("#chart-holder-carrito").css("display") == "none") {
			manageChart('show');
		}/* else {
			manageChart('hide');
		}*/
	});
	$('p.banner-musica a').click(function() {
		window.open('/music/index.php','Player','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=480,height=500');
		return false;
	});
	$('li.musica a').click(function() {
    	window.open('/music/index.php','Player','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=480,height=500');
		return false;
	});	
/*
	if ($('#selector-idiomas').length) {
		$('#selector-idiomas').change(function() {
			window.open('/default.php?lang='+$('#selector-idiomas').val(),"self");
		});
	}
*/
	if ($('#detail-shirt-gallery').length) {
		$("#detail-shirt-gallery li a").click(function() {
			var laImagen = $(this).attr("href").split('/');
			var imgTitle = '/documentos/imagenes/slideshow/'+laImagen[3];
			var imgTitleModalBox = '/documentos/imagenes/'+laImagen[3];
			$(".detail-shirt-image img").fadeOut(1000, function() {
				$(".detail-shirt-image img").attr('src', imgTitle);
				$(".detail-shirt-image a").attr('href', imgTitleModalBox);				
				$(".detail-shirt-image img").fadeIn(500);
			});
			return false; 
		});
	}	
	if ($('#addShirt').length) {
		modificador = parseInt($('.detail ul#detail-shirt-gallery').css('width'));
		if (!modificador) { modificador = 0; }
		$('.detail p.shirt-price').css('right', 340+modificador+'px');
		if ($.browser.msie) {
			if ($.browser.version.substr(0,1)>=7) {
				$('#trigger-last-comments').click(function() {
					$('#listado-comentarios').slideToggle();
					if ($('p.paginacion').is(":visible")) $('p.paginacion').hide();
					else $('p.paginacion').show();
				});
			}
		} else {
			$('#trigger-last-comments').click(function() {
				$('#listado-comentarios').slideToggle();
				if ($('p.paginacion').is(":visible")) $('p.paginacion').hide();
				else $('p.paginacion').show();
			});
		}
		$('form#addShirt label').css('display', 'none');
		if ($("#lang").val() == 'es') {
			$('form#addShirt select#size').prepend('<option value="" selected="selected">Elige la talla</option>');
			$('form#addShirt select#color').prepend('<option value="" selected="selected">Elige primero la talla</option>');
			$('form#addShirt select#iAmount').prepend('<option value="" selected="selected">Elige la cantidad</option>');	
		} else {
			$('form#addShirt select#size').prepend('<option value="" selected="selected">Choose the size</option>');
			$('form#addShirt select#color').prepend('<option value="" selected="selected">First choose size</option>');
			$('form#addShirt select#iAmount').prepend('<option value="" selected="selected">Choose quantity</option>');		
		}
		
		$('#size').sSelect();
		$('#color').sSelect();
		$('#iAmount').sSelect();
		$('#iAmount').css('z-index','600');
		
		$('#addShirt').submit(function() {
			var title;
			var message;
			var message2;
			
			if ($('#size').getSetSSValue() == '') {
		  		if ($("#lang").val() == 'es') {
		  			title = '¡Algo no salió bien!';
					message = 'Antes de añadir la camiseta al carrito debes elegir la talla';
					message2 = 'Cerrar';
				} else {
		  			title = 'Something went wrong!';
					message = 'Before adding the shirt you choose the size';
					message2 = 'Close';				
				}
				$('body').append("<div id='submitForm'></div>");
				$('div#submitForm').qtip({
					content: {
						title: {
							text: title,
							button: message2
						},
						text: message							
					},
					position: {
						target: $(document.body),
						corner: 'center'
					},
					show: {
						when: 'click',
						solo: true
					},
					hide: false,
					style: {
						width: 500,
						padding: '14px',
						border: {
							width: 9,
							radius: 9,
							color: '#666666'
			         	},
			         	name: 'light'
					}
				});
				$("div#submitForm").trigger("click");
		  		return false;
		  	}
		  	if ($('#color').getSetSSValue() == '') {
				if ($("#lang").val() == 'es') {
		  			title = '¡Algo no salió bien!';
					message = 'Antes de añadir la camiseta al carrito debes elegir el color';
					message2 = 'Cerrar';
				} else {
		  			title = 'Something went wrong!';
					message = 'Before adding the shirt you choose the color';
					message2 = 'Close';				
				}
				$('body').append("<div id='submitForm'></div>");
				$('div#submitForm').qtip({
					content: {
						title: {
							text: title,
							button: message2
						},
						text: message							
					},
					position: {
						target: $(document.body),
						corner: 'center'
					},
					show: {
						when: 'click',
						solo: true
					},
					hide: false,
					style: {
						width: 500,
						padding: '14px',
						border: {
							width: 9,
							radius: 9,
							color: '#666666'
			         	},
			         	name: 'light'
					}
				});
				$("div#submitForm").trigger("click");
		  		return false;
		  	}
		  	if ($('#iAmount').getSetSSValue() == '') {
		  		if ($("#lang").val() == 'es') {
		  			title = '¡Algo no salió bien!';
					message = 'Antes de añadir la camiseta al carrito debes elegir la cantidad';
					message2 = 'Cerrar';
				} else {
		  			title = 'Something went wrong!';
					message = 'Before you add the shirt to cart you choose the amount';
					message2 = 'Close';				
				}
				$('body').append("<div id='submitForm'></div>");
				$('div#submitForm').qtip({
					content: {
						title: {
							text: title,
							button: message2
						},
						text: message							
					},
					position: {
						target: $(document.body),
						corner: 'center'
					},
					show: {
						when: 'click',
						solo: true
					},
					hide: false,
					style: {
						width: 500,
						padding: '14px',
						border: {
							width: 9,
							radius: 9,
							color: '#666666'
			         	},
			         	name: 'light'
					}
				});
				$("div#submitForm").trigger("click");
		  	}
		});
	}
	
	$("#size").change(function() {
		$.ajax({
			url: "/includes/colorsbysize.php",
			async:true,
			contentType: "application/x-www-form-urlencoded",
	        dataType: "html",
			data: ({shirt : $("#shirt").val(), option: $('#size').getSetSSValue(), lang: $("#lang").val()}),
	        global: true,
	        ifModified: false,
	        processData:true,
	        timeout: 3000,
	        type: "POST",
	       	success: function(datos){
				$("#color").html(datos);
				$("#color").resetSS();
			}
		});
	});
	
	$("p#detail-shirt-votes span").hover(
		function () {
			if ($(this).attr("id") != '') {
				var id = parseInt($(this).attr("id").substring(4,5));
				$('body').css('cursor', 'pointer'); 
				for (i=1; i<=id; i++) {
					$('#vote'+i).css('background-position', 'bottom');
				}
			}
		},
		function () {
			if ($(this).attr("id") != '') {
				$('body').css('cursor', 'default');
				var id = parseInt($(this).attr("id").substring(4,5));
				for (i=1; i<=id; i++) {
					if ($('#vote'+i).hasClass('star')) {
						$('#vote'+i).css('background-position', 'center left');
					} else {
						$('#vote'+i).css('background-position', 'top');
					}
				}				
			}
		}
	);
	$("p#detail-shirt-votes-contest span").hover(
		function () {
			if ($(this).attr("id") != '') {
				var id = parseInt($(this).attr("id").substring(4,5));
				$('body').css('cursor', 'pointer'); 
				for (i=0; i<=id; i++) {
					$('#vote'+i).css('background-position', 'bottom');
				}
			}
		},
		function () {
			if ($(this).attr("id") != '') {
				$('body').css('cursor', 'default');
				var id = parseInt($(this).attr("id").substring(4,5));
				for (i=0; i<=id; i++) {
					if ($('#vote'+i).hasClass('star')) {
						$('#vote'+i).css('background-position', 'center left');
					} else {
						$('#vote'+i).css('background-position', 'top');
					}
				}				
			}
		}
	);
	$("p#detail-shirt-votes span").click(function() {
		if ($(this).attr("id") != '') {
			$.ajax({
				url: "/includes/voteshirt.php",
		        async:true,
				contentType: "application/x-www-form-urlencoded",
		        dataType: "html",
		        data: ({shirt : $("#shirt").val(), user: $("#user").val(), vote: $(this).attr("id").substr(4)}),
		        global: true,
		        ifModified: false,
		        processData:true,
		        timeout: 3000,
		        type: "POST",
		       	success: function(datos){
					var title;
					var message;
					var message2;
					if (datos != 'ko') {						
						if ($("#lang").val() == 'es') {
							title = '¡Perfecto!';
							message = 'Ya tenemos tu voto registrado';
							message2 = 'Cerrar';
						} else {
							title = 'Perfect!';
							message = 'We have recorded your vote';
							message2 = 'Close';
						}
						$("p#detail-shirt-votes").html(datos);
					} else {
						if ($("#lang").val() == 'es') {
							title = '¡Algo no salió bien!';
							message = 'Parece que ha habido algún problema con tu voto. Inténtalo de nuevo';
							message2 = 'Cerrar';
						} else {
							title = 'Something went wrong!';
							message = 'Looks like there was a problem with your vote. Try again';
							message2 = 'Close';
						}
					}
					$('body').append("<div id='submitForm'></div>");
					$('div#submitForm').qtip({
							content: {
							title: {
								text: title,
								button: message2
							},

							text: message							
						},
						position: {
							target: $(document.body),
							corner: 'center'
						},
						show: {
							when: 'click',
							solo: true
						},
						hide: false,
						style: {
							width: 500,
							padding: '14px',
							border: {
								width: 9,
								radius: 9,
								color: '#666666'
				         	},
				         	name: 'light'
						}
					});
					$("div#submitForm").trigger("click");
					$('div#submitForm').remove();
				}
			});
		}
	});
	$("p#detail-shirt-votes-contest span").click(function() {
		if ($(this).attr("id") != '') {
			$.ajax({
				url: "/includes/votecontestshirt.php",
		        async:true,
				contentType: "application/x-www-form-urlencoded",
		        dataType: "html",
		        data: ({shirt : $("#shirt").val(), user: $("#user").val(), vote: (parseInt($(this).attr("id").substr(4))+1)}),
		        global: true,
		        ifModified: false,
		        processData:true,
		        timeout: 3000,
		        type: "POST",
		       	success: function(datos){
					var title;
					var message;
					var message2;
					if (datos != 'ko') {						
						if ($("#lang").val() == 'es') {
							title = '¡Perfecto!';
							message = 'Ya tenemos tu voto registrado';
							message2 = 'Cerrar';
						} else {
							title = 'Perfect!';
							message = 'We have recorded your vote';
							message2 = 'Close';
						}
						$("p#detail-shirt-votes-contest").html(datos);
					} else {
						if ($("#lang").val() == 'es') {
							title = '¡Algo no salió bien!';
							message = 'Parece que ha habido algún problema con tu voto. Inténtalo de nuevo';
							message2 = 'Cerrar';
						} else {
							title = 'Something went wrong!';
							message = 'Looks like there was a problem with your vote. Try again';
							message2 = 'Close';
						}
					}
					$('body').append("<div id='submitForm'></div>");
					$('div#submitForm').qtip({
							content: {
							title: {
								text: title,
								button: message2
							},

							text: message							
						},
						position: {
							target: $(document.body),
							corner: 'center'
						},
						show: {
							when: 'click',
							solo: true
						},
						hide: false,
						style: {
							width: 500,
							padding: '14px',
							border: {
								width: 9,
								radius: 9,
								color: '#666666'
				         	},
				         	name: 'light'
						}
					});
					$("div#submitForm").trigger("click");
					$('div#submitForm').remove();
				}
			});
		}
	});
	$('#user-panel #buscador img').click(function() {
		$('#user-panel #buscador').submit();
	});
	
	$('#user-panel #login img').click(function() {
		$('#user-panel #login form').submit();
	});
/*
	$('#language').hover(function() {
		$('#selector-idiomas').stop(true, true).slideDown();
	}, function() {
		$('#selector-idiomas').stop(true, true).slideUp();
	});
*/
	
	if ($('#status-panel table').length) {
		$('#status-panel p.chart-total').append('<span class="close"><img src="/images/btn-cerrar-carrito-'+$('body').attr('class').substr(0,2)+'.png" alt="" /></span>');
		$('p#chart-holder-items').click(function() {
			$('#status-panel table').stop(true, true).fadeIn();
			$('#status-panel p.chart-total').stop(true, true).fadeIn();
		});
		$('#status-panel p.chart-total span.close').click(function() {
			$('#status-panel table').stop(true, true).fadeOut();
			$('#status-panel p.chart-total').stop(true, true).fadeOut();		
		});
	}
	
	$('#str_referral').change(function() {
		if ($('#str_referral').val() == 'otros') {
			$('#str_referral_text').css('display', 'block');
		} else {
			$('#str_referral_text').css('display', 'none');		
		}
	})
	
	if ($('li.musica').length) {
		$('li.musica [title]').qtip({
			content: {
				text: false
	      	},
	      	position: {
				corner: {
					tooltip: 'rightMiddle',
					target: 'leftMiddle'
				}
			},
			style: {
				border: {
				width: 5,
				radius: 10
				},
				padding: 10, 
	      		textAlign: 'center',
	      		tip: true,
	      		name: 'cream'
	   		}
		});
	}
	if ($('p.banner-musica').length) {
		$('p.banner-musica').qtip({
			content: {
				text: false
	      	},
			position: {
				corner: {
					tooltip: 'rightMiddle',
					target: 'leftMiddle'
				}
			},      	
			style: {
				border: {
				width: 5,
				radius: 10
				},
				padding: 10, 
	      		//textAlign: 'center',
	      		tip: true,
	      		name: 'cream'
	   		}
		});
	}
});

function popUnder() {
	popmusic = window.open("/music/index.php",'MusicPlayer'/*,"toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=480,height=500"*/);
	popmusic.focus();
}


function showPreview(coords) {	
	if (parseInt(coords.w) > 0) {
		var rx = 470 / coords.w;
		var ry = 352 / coords.h;

		if ($("#shirt_image_name").length) {
			$('#preview').css({
				width: Math.round(rx * $('#shirt_image_name').width()) + 'px',
				height: Math.round(ry * $('#shirt_image_name').height()) + 'px',
				marginLeft: '-' + Math.round(rx * coords.x) + 'px',
				marginTop: '-' + Math.round(ry * coords.y) + 'px'
			});
		}	
		jQuery('#x').val(coords.x);
		jQuery('#y').val(coords.y);
		jQuery('#w').val(coords.w);
		jQuery('#h').val(coords.h);		
	}
};
