$(document).ready(function() {
	/* Search */
/*	var $doc = $(document), $win = $(window), $chaser, $forch,
		forchBottom, visible;
	function shown() {
		visible = true;
	}
	function hidden() {
		visible = false;
	}
	
	$chaser = $('#cart').clone().hide().appendTo(document.body).wrap("<div class='chaser'></div>");
	$forch = $('.forchaser').first();
	forchBottom = $forch.height() + $forch.offset().top;
	hidden();
	$win.bind('scroll', function () {
		var top = $doc.scrollTop();
		if (!visible && top > forchBottom) {
			$chaser.slideDown(300, shown);
		} else if (visible && top < forchBottom) {
			$chaser.slideUp(200, hidden);
		}
	});*/
	
	$('.button-search').bind('click', function() {
		url = $('base').attr('href') + 'index.php?route=product/search';
				 
		var filter_name = $('input[name=\'filter_name\']').attr('value')
		
		if (filter_name) {
			url += '&filter_name=' + encodeURIComponent(filter_name);
		}
		
		location = url;
	});
	
	$('#header input[name=\'filter_name\']').keydown(function(e) {
		if (e.keyCode == 13) {
			url = $('base').attr('href') + 'index.php?route=product/search';
			 
			var filter_name = $('input[name=\'filter_name\']').attr('value')
			
			if (filter_name) {
				url += '&filter_name=' + encodeURIComponent(filter_name);
			}
			
			location = url;
		}
	});
	
	$('#slider-container li').bind('mouseover', function() {
													  $(this).find('.hover-container').fadeIn();
													  });
	$('#slider-container li').bind('mouseleave', function() {
													  $(this).find('.hover-container').fadeOut();
													  });
	
	/* Ajax Cart */
	$('#cart-button').bind('mouseover', function() {
		$(this).addClass('active');
		
		$('#cart .content').slideDown();
		
		$.ajax({
			url: 'index.php?route=checkout/cart/update',
			dataType: 'json',
			success: function(json) {
				if (json['output']) {
					$('#cart .content').html(json['output']);
				}
			}
		});			
		
		$('#cart').bind('mouseleave', function() {
			$('#cart .content').slideUp(function(){
												 $('#cart-button').removeClass('active');
												 });
		});
	});
	
	/* Mega Menu */
	$('#menu ul > li > a + div').each(function(index, element) {
		// IE6 & IE7 Fixes
		if ($.browser.msie && ($.browser.version == 7 || $.browser.version == 6)) {
			var category = $(element).find('a');
			var columns = $(element).find('ul').length;
			
			$(element).css('width', (columns * 143) + 'px');
			$(element).find('ul').css('float', 'left');
		}		
		
		var menu = $('#menu').offset();
		var dropdown = $(this).parent().offset();
		
		i = (dropdown.left + $(this).outerWidth()) - (menu.left + $('#menu').outerWidth());
		
		if (i > 0) {
			$(this).css('margin-left', '-' + (i + 5) + 'px');
		}
	});

	// IE6 & IE7 Fixes
	if ($.browser.msie) {
		if ($.browser.version <= 6) {
			$('#column-left + #column-right + #content, #column-left + #content').css('margin-left', '195px');
			
			$('#column-right + #content').css('margin-right', '195px');
		
			$('.box-category ul li a.active + ul').css('display', 'block');	
		}
		
		if ($.browser.version <= 7) {
			$('#menu > ul > li').bind('mouseover', function() {
				$(this).addClass('active');
			});
				
			$('#menu > ul > li').bind('mouseout', function() {
				$(this).removeClass('active');
			});	
		}
	}
	
	$('.success img, .warning img, .attention img, .information img').live('click', function() {
		$(this).parent().fadeOut('slow', function() {
			$(this).remove();
		});
	});
	
	/*if($("#twitter_div").length>0 && $("#twitter_div li").length>1){
	setInterval(function(){
		$("#twitter_div li").filter(':visible').fadeOut(1000,function(){
			if($(this).next('li').size()){
				$(this).next().delay(2000).fadeIn(1000);
			}else{
				$("#twitter_div li").eq(0).delay(2000).fadeIn(1000);
			}
		});
	},1000);
	}*/
	
	/*var timer;
	var counter = 0;
	var track = 0;
	var content;
	var total;

	if($("#twitter_div").length>0 && $("#twitter_div li").length>1){
	var content = $("#twitter_div ul").html();
	var total = $("#twitter_div ul li").length;
  	clearTimeout(timer);
  	timer = setTimeout(eval(rotate_tweets),"7000");
	}
	
	function rotate_tweets(){
	var top = $("#twitter_div ul").css('top');
	counter++;
	track++;
	$("#twitter_div ul").animate({"bottom":-30*track},300).effect("bounce", { direction:'down', times:3 }, 200);
	if(counter == (total-1)){
	$("#twitter_div ul").append(content);
	counter = -1;
	}
	clearTimeout(timer);
	timer = setTimeout(eval("rotate_tweets"),"7000");
	}*/
	
});

$(window).load(function() {

//////////////////////////////////////////////////////////////////////////
// SOCIAL ICONS SMALL SLIDER
//////////////////////////////////////////////////////////////////////////
	$('li.social-icons > a').click(function() {
		var $t = $(this).next();
		if($(this).hasClass('opened')) {
			$t.parent().animate({width:$(this).width()+'px'}, 300, 'easeOutExpo');
			$(this).removeClass('opened');
		}else{
			$t.parent().animate({width:($t.find('.inner').outerWidth()+$(this).width())+'px'}, 300, 'easeOutExpo');
			$(this).addClass('opened');
		}
		return false;
	});


//////////////////////////////////////////////////////////////////////////	
// SEARCH FIELD SMALL SLIDER
//////////////////////////////////////////////////////////////////////////
	$('li.search-box > a').click(function() {
		var $t = $(this).next();
		if($(this).hasClass('opened')) {
			$t.parent().animate({width:$(this).width()+'px'}, 300, 'easeOutExpo');
			$(this).removeClass('opened');
		}else{
			$t.parent().animate({width:($t.find('input[type=text]').outerWidth()+$(this).width())+'px'}, 300, 'easeOutExpo');
			$(this).addClass('opened');
		}
		return false;
	});

//////////////////////////////////////////////////////////////////////////	
// TRIGGER TO SHOW THE HIDDEN MAP
//////////////////////////////////////////////////////////////////////////
	$('.map_link').click(function(e) {
		e.preventDefault();
		var $m = $('.hidden-map-wrapper');
		if($m.hasClass('opened')) {
			$m.stop().animate({height:0}, {queue: false, duration: 300, easing: "easeOutExpo"});
			$m.removeClass('opened');
		}else{
			$m.stop().animate({height:'350px'}, {queue: false, duration: 300, easing: "easeOutExpo"});
			$('html, body').animate({scrollTop: '5000px'}, 300,'easeOutExpo');
			$m.addClass('opened');
		}
	})
	$('.close-map').click(function(e) {
		e.preventDefault();
		var $m = $('.hidden-map-wrapper');
		$m.stop().animate({height:0}, {queue: false, duration: 300, easing: "easeOutExpo"});
		$m.removeClass('opened');
	})
});

function addToCart(product_id) {
	$.ajax({
		url: 'index.php?route=checkout/cart/update',
		type: 'post',
		data: 'product_id=' + product_id,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information, .error').remove();
			
			if (json['redirect']) {
				location = json['redirect'];
			}
			
			if (json['error']) {
				if (json['error']['warning']) {
					$('#notification').html('<div class="warning" style="display: none;">' + json['error']['warning'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
					
					$('.warning').fadeIn('slow');
					
					$('html, body').animate({ scrollTop: 0 }, 'slow');
				}
			}	 
						
			if (json['success']) {
				$('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				
				$('.success').fadeIn('slow');
				
				$('#cart_total').html(json['total']);
				
				$('html, body').animate({ scrollTop: 0 }, 'slow'); 
			}	
		}
	});
}

function removeCart(key) {
	$.ajax({
		url: 'index.php?route=checkout/cart/update',
		type: 'post',
		data: 'remove=' + key,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information').remove();
			
			if (json['output']) {
				$('#cart_total').html(json['total']);
				
				$('#cart .content').html(json['output']);
			}			
		}
	});
}

function removeVoucher(key) {
	$.ajax({
		url: 'index.php?route=checkout/cart/update',
		type: 'post',
		data: 'voucher=' + key,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information').remove();
			
			if (json['output']) {
				$('#cart_total').html(json['total']);
				
				$('#cart .content').html(json['output']);
			}			
		}
	});
}

function addToWishList(product_id) {
	$.ajax({
		url: 'index.php?route=account/wishlist/update',
		type: 'post',
		data: 'product_id=' + product_id,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information').remove();
						
			if (json['success']) {
				$('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				
				$('.success').fadeIn('slow');
				
				$('#wishlist_total').html(json['total']);
				
				$('html, body').animate({ scrollTop: 0 }, 'slow'); 				
			}	
		}
	});
}

function addToCompare(product_id) { 
	$.ajax({
		url: 'index.php?route=product/compare/update',
		type: 'post',
		data: 'product_id=' + product_id,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information').remove();
						
			if (json['success']) {
				$('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				
				$('.success').fadeIn('slow');
				
				$('#compare_total').html(json['total']);
				
				$('html, body').animate({ scrollTop: 0 }, 'slow'); 
			}	
		}
	});
}

//////////////////////////////////////////////////////////////////////////
// HIDDEN MAP FUNCTION
//////////////////////////////////////////////////////////////////////////
	function loadHiddenMap() {
	
	// change this coordinates latitude,longitude - use this tool to get coordinates http://itouchmap.com/latlong.html
	var point = new google.maps.LatLng(-27.459365,153.038403);

	var myMapOptions = {
		scrollwheel:false,
		zoom: 15,
		center: point,
		mapTypeControl: true,
		mapTypeControlOptions: {
			style: google.maps.MapTypeControlStyle.DROPDOWN_MENU,
        	position: google.maps.ControlPosition.RIGHT_CENTER },
		navigationControl: true,
		navigationControlOptions: {
			style: google.maps.NavigationControlStyle.SMALL,
        	position: google.maps.ControlPosition.LEFT_CENTER},
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	
	var map = new google.maps.Map(document.getElementById('hidden_map'),myMapOptions);
	
	var image = new google.maps.MarkerImage(
	  'catalog/view/theme/getjack3d/image/marker-images/image.png',
	  new google.maps.Size(188,68),
	  new google.maps.Point(0,0),
	  new google.maps.Point(0,68)
	);
	
	var shadow = new google.maps.MarkerImage(
	  'catalog/view/theme/getjack3d/image/marker-images/image.png',
	  new google.maps.Size(217,68),
	  new google.maps.Point(0,0),
	  new google.maps.Point(0,68)
	);
	
	var shape = {
	  coord: [69,0,80,1,84,2,87,3,90,4,93,5,96,6,98,7,100,8,102,9,104,10,105,11,107,12,108,13,110,14,111,15,112,16,113,17,115,18,115,19,117,20,118,21,119,22,120,23,121,24,122,25,123,26,124,27,124,28,125,29,126,30,127,31,127,32,128,33,129,34,129,35,130,36,130,37,131,38,131,39,132,40,132,41,133,42,133,43,133,44,134,45,134,46,135,47,135,48,135,49,136,50,136,51,136,52,136,53,136,54,137,55,137,56,137,57,137,58,137,59,138,60,138,61,138,62,138,63,138,64,138,65,138,66,138,67,138,68,138,69,138,70,138,71,138,72,139,73,139,74,138,75,139,76,139,77,139,78,139,79,138,80,138,81,138,82,138,83,137,84,137,85,137,86,137,87,137,88,136,89,136,90,136,91,135,92,135,93,135,94,134,95,134,96,133,97,133,98,132,99,132,100,131,101,131,102,130,103,130,104,129,105,128,106,128,107,127,108,127,109,126,110,125,111,124,112,124,113,123,114,122,115,122,116,121,117,120,118,119,119,118,120,117,121,116,122,115,123,114,124,113,125,112,126,110,127,109,128,107,129,106,130,103,131,102,132,100,133,99,134,96,135,93,136,89,137,86,138,83,139,79,140,78,141,77,142,76,143,75,144,75,145,74,146,73,147,72,148,71,149,70,150,70,151,70,152,68,152,67,151,67,150,66,149,65,148,64,147,64,146,63,145,62,144,61,143,60,142,60,141,59,140,56,139,52,138,47,137,45,136,43,135,41,134,39,133,37,132,35,131,33,130,32,129,30,128,29,127,28,126,26,125,26,124,24,123,23,122,22,121,21,120,20,119,18,118,18,117,17,116,16,115,15,114,14,113,13,112,13,111,12,110,11,109,11,108,10,107,10,106,9,105,8,104,8,103,7,102,7,101,7,100,6,99,6,98,5,97,5,96,5,95,4,94,4,93,4,92,3,91,3,90,3,89,2,88,2,87,2,86,2,85,2,84,2,83,1,82,1,81,1,80,0,79,0,78,0,77,0,76,0,75,0,74,0,73,0,72,0,71,0,70,0,69,0,68,0,67,0,66,0,65,0,64,0,63,0,62,1,61,1,60,1,59,1,58,1,57,2,56,2,55,2,54,3,53,3,52,3,51,3,50,4,49,4,48,4,47,5,46,5,45,6,44,6,43,7,42,7,41,8,40,8,39,9,38,9,37,10,36,10,35,11,34,11,33,12,32,13,31,14,30,14,29,15,28,16,27,17,26,17,25,18,24,19,23,20,22,21,21,21,20,22,19,24,18,25,17,26,16,27,15,29,14,30,13,31,12,33,11,35,10,36,9,39,8,41,7,43,6,45,5,49,4,53,3,56,2,65,1,69,0,69,0],
	  type: 'poly'
	};
	
	var marker = new google.maps.Marker({
		draggable:true,
		raiseOnDrag: false,
		icon: image,
		shadow: shadow,
		shape: shape,
		map: map,
		position: point
		
	});
}
