var map = null;

$(document).ready(function() {
	var hasta = new Date($('.product_timer_countdown .data').text() * 1000);
	$.countdown.setDefaults($.countdown.regional['es']);
	var customlayout = '<div class="hour">{hn} hs</div><div class="minute">{mn} min</div><div class="second">{sn} seg</div>';
	var customformat = "HMS";
	
 	$('.product_timer_countdown .data ').countdown({until: hasta, layout: customlayout, format: customformat, onExpiry: reloadWindow});

 	$('.other_products_timer_countdown .data ').each(function(idx) {
 		var customlayout_otherprods = '<div class="hour"><small>HS</small><br/>{hn}</div><div class="minute"><small>MIN</small><br/>{mn}</div><div class="second"><small>SEG</small><br/>{sn}</div>';
 		var hasta_otherprods = new Date($(this).text() * 1000);
 		$(this).countdown({until: hasta_otherprods, layout: customlayout_otherprods, format: customformat, onExpiry: reloadWindow})
 	});
 	
 	$("#main_product_gallery .img_strip .strip_item").bind("click", function(event) {
 		$("#main_product_gallery .main_img .main_product_picture").attr("src", $(this).attr("src"));
 	});
 	
 	$("#main_product_gallery .img_strip .strip_item").bind("mouseenter", function(event) {
 		$("#main_product_gallery .main_img .main_product_picture").attr("src", $(this).attr("src"));
 	});
 	
	$('#share_on_fb').popupWindow({ 
			height:400, 
			width:620, 
			centerScreen:1 
	});  	
	
 	$('#btn_comprar').bind('mouseenter', function() {
 		$(this).attr('src', '/images/product_btn_comprar_over.png');
 	}).bind('mouseleave', function() {
 		$(this).attr('src', '/images/product_btn_comprar.png');
 	});
 	
	if ($("#main_product_map_canvas").attr('id')) {
		var myOptions = {
				zoom: 13,
				mapTypeId: google.maps.MapTypeId.ROADMAP,
				disableDefaultUI: true,
				navigationControl: true,
				mapTypeControl: true,
				scaleControl: false
		}
		map = new google.maps.Map(document.getElementById("main_product_map_canvas"), myOptions);
		
		var lat = $('#hidden_location_latitude').text();
		var lon = $('#hidden_location_longitude').text();

		var location = new google.maps.LatLng(lat, lon);
		map.setCenter(location);
		var marker = new google.maps.Marker({
			map: map, 
			position: location
		});
	}
});

function reloadWindow() {
	if ($('#product_url').val() != '') {
		window.open($('#product_url').val(), '_self');
	} else {
		window.location.reload();
	}
}
