/**
 * This file is contain a map core functionality
 *
 *
 * jQuery Version 1.3.2
 *
 * LICENSE: This source code is copyright Snapp Labs, Co.Ltd. with knowledge that BUZZWOO! GbR will
 * receive all rights to the source code, including changing all comment header blocks and source code.
 *
 * @author     Alongkot Karpchai <akot@snapplabs.com>
 * @license    See Above
 * @version    0.1
 */
 
var headline_count;
var current_headline=0;

$(document).ready(function(){
	
	//lock quantity input accept only digit number
	$('.item').keypress(function(e){
		if(e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) {		
			return false;
		}	
	});	
	
	//lightbox
	$('a[rel*=facebox]').lightBox();
	
	//jquery tabs for content
	$('#tab-content').tabs();
	
	//Add item to carts
	$('#addItem').click(function(){
		
		var items = $(this).prev().attr('value');
		var productId = $(this).prev().attr('name');
		var url = "/product/add/";
		
		if (items.length == 0) {
			Boxy.alert("Please fill quantity of product<br />โปรดระบุจำนวนสินค้า", null, {title: 'Notice'});
   			return false;
		} else {
			//called server to get result
			jQuery.ajax({			
			   	type: "POST",
			   	url: url,
			   	data: ({id: productId, qty: items}),
			   	success: function (data) {		
			   		$('#basket').html(data);
			   		Boxy.alert("Product already added in to your cart", null, {title: 'Success'});
   					return false;
				},
				error:function(){ alert('Can\'t sending Request please check internet connection'); }
			});	
		}
		
	});
	
	$("#left-panel ul.menu li").hover(			
			function () {				
				$(this).stop().animate({ paddingLeft: "25px" }, 200);
			}, 
			function () {
				$(this).stop().animate({ paddingLeft: "18px" });
			}
		);
		
	$("#navigation li a").hover(			
			function () {				
				$(this).stop().animate({ lineHeight: "140px" }, 200);
			}, 
			function () {
				$(this).stop().animate({ lineHeight: "150px" });
			}
		);
		
	$("ul.image-panel li a img").hover(	
		function () {						
			var location = $(this).attr('src').split("/");	
			var src = '/media/img/product/' + location[location.length - 1];
			$(".imageframe	a img").attr('width', 300);
			$(".imageframe	a img").attr('src', src).attr('width', 300);
		}
	);
	
	//plus item in input box
	$('a#item-plus').click(function(){
		var current = parseInt($('#quantity').attr('value'));
		$('#quantity').attr('value', current + 1);
		return false;
	});
	
	//reduce item in input box if it's equal 1 will not reduce any more
	$('a#item-minus').click(function(){
		var current = parseInt($('#quantity').attr('value'));
		if (current > 1) {
			$('#quantity').attr('value', current - 1);
		}
		return false;
	});
	
	//Scroll section	
	headline_count = $("div.headline").size();
	$("div.headline:eq("+current_headline+")").css('top', '5px');
	setInterval(headline_rotate,5000); //time in milliseconds
	
	//for change color
	$('#color-pick li a img').click(function(){
		jQuery.ajax({
			async: false,
		   	type: "POST",
		   	url: "/product/getimage/",
		   	data: "id=" + $('#color-pick').attr('title') + "&color=" + $(this).attr('title'),
		   	success: function (data) {									
				$('#image-bucket').html(data);
				
				$('a[rel*=facebox]').lightBox();
				
				$("ul.image-panel li a img").hover(function () {						
					var location = $(this).attr('src').split("/");	
					var src = '/media/img/product/' + location[location.length - 1];
					$(".imageframe	a img").attr('width', 300);
					$(".imageframe	a img").attr('src', src).attr('width', 300);
				});
			},
			error:function(){ alert('Can\'t sending Request please check internet connection'); }
		});	
	});
	
	if($('#map_canvas').length > 0){
		var map 			= new GMap(document.getElementById("map_canvas"));			
		var latitude 		= 13.790113115526317;
		var longtitude 	= 100.56653022766113;
		var zoom 			= 15;	 
		
		//Intilize Map	
		map.setMapType(G_PHYSICAL_MAP);	
		var mapControl = new GMapTypeControl();
		map.addControl(mapControl);
		map.addControl(new GSmallZoomControl3D());
		map.setCenter(new GLatLng(latitude,longtitude), zoom);    

	}
	
});

function headline_rotate() {
  old_headline = current_headline % headline_count;
  new_headline = ++current_headline % headline_count;
  $("div.headline:eq(" + old_headline + ")").css('top', '210px');
  $("div.headline:eq(" + new_headline + ")").show().animate({top: 5},"slow");     
}
