/* ©Copyright 2011 Amanda Davy */
$(document).ready(function() {
	var base_url = 'http://'+window.location.hostname+'/';
	
		 var _gaq = _gaq || [];
	  _gaq.push(['_setAccount', 'UA-24692460-1']);
	  _gaq.push(['_trackPageview']);

	  (function() {
		var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
		ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
		var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	  })();

	$(window).scrollLeft(($(window).width()-$('#container').width())/2*-1);
	overlayControl();

	$(window).resize(function(){
		var autoScroll = ($(window).width()-$('#container').width())/2*-1;
		$(window).scrollLeft(autoScroll);
		overlayControl();
		if($('#page_overlay').is(':visible')) {
			var posLeft = 0;
			if($(window).width()>$('#container').width()) {
				posLeft = ($(window).width()-$('#inner_overlay').width())/2;
			} else {
				posLeft = ($(window).width()-$('#inner_overlay').width())/2+(($(window).width()-$('#container').width())/2*-1);
			}
			$('#inner_overlay').css({ left: posLeft });
		}
	});
	$(window).scroll(function () { 
		$('#inner_overlay').css({ top: $(window).scrollTop()+($(window).height()-$('#inner_overlay').height())/2.5 });
	});
	$('#page_overlay').click(function() {
		hideOverlay();
	});
	$(document).keyup(function(e) {
	  if (e.keyCode == 27) { hideOverlay(); } // enter > 13
	});
	if($('#sidebar').length>0) {
		if($('#innerContent').height() < $('#sidebar').height()) {
			$('#innerContent').removeClass('mediumBorder').removeClass('leftBorder').removeClass('dashedBorder');
			$('#sidebar').addClass('mediumBorder').addClass('rightBorder').addClass('dashedBorder');
		}
	}
	//$(':input[type="text"]').val('');
	$(':input[type="password"]').val('');
	//$(':input[type="checkbox"]').val('');
	
	/* ---------------- OVERLAYS ----------------- */
	$('.image_overlay').live('click',function() {
		if($('#page_overlay').is(':visible')) {
			$('#inner_overlay .content').empty();
			$('#inner_overlay').width('auto').height('auto');
		}
		removeOverlayLoader();
		$('#inner_overlay .content').append('<img src="'+this.href+'"/>');
		/*if($('#inner_overlay').width() <200) {
			$('.close_overlay .mediumSize').addClass('hidden');
		}*/
		setTimeout(function() { showOverlay(); }, 100);
	});
	$('.product_overlay').live('click',function() {
		if($('#page_overlay').is(':visible')) {
			$('#inner_overlay .content').empty();
		}
		$('#inner_overlay').width(700).height(450);
		var id = parseInt($(this).attr('href'));
		$.ajax({
			url: base_url+'action/productDetails/'+id,
			dataType: 'json',
			success: function(data) {	
				removeOverlayLoader();
				$('#inner_overlay .content').append(data[1]);
				setTimeout(function() { 
					var offset = $('#inner_overlay .content img').offset();
					if($('#inner_overlay .content img').width()>350) {
						$('#inner_overlay .content img').offset({
						left:offset.left+(350-$('#inner_overlay .content img').width())/2}); 
					}
				}, 0);
				setTimeout(function() { 
				//alert($('#inner_overlay .content img').attr('src'));
					//$('#inner_overlay .content img').css({left:(350-$('#inner_overlay .content img').width())/2});
					$('#inner_overlay .content img').css({top:(450-$('#inner_overlay .content img').height())/2});
							}, 0);
			}
		}); 
		showOverlay();
	});
	$('.recall_overlay').live('click',function() {
		if($('#page_overlay').is(':visible')) {
			$('#inner_overlay .content').empty();
		}
		$('#inner_overlay').width(700).height(450);
		var id = parseInt($(this).attr('href'));
		$.ajax({
			url: base_url+'action/productRecall/'+id,
			dataType: 'json',
			success: function(data) {	
				removeOverlayLoader();
				$('#inner_overlay .content').append(data[1]);
				setTimeout(function() { 
					var offset = $('#inner_overlay .content img').offset();
					if($('#inner_overlay .content img').width()>350) {
						$('#inner_overlay .content img').offset({
						top:offset.top+(450-$('#inner_overlay .content img').height())/2,
						left:offset.left+(350-$('#inner_overlay .content img').width())/2}); 
					} else {
						$('#inner_overlay .content img').offset({
							top:offset.top+(450-$('#inner_overlay .content img').height())/2}); 
					}
				}, 50);
			}
		}); 
		showOverlay();
	});
	$('.cartButton').live('click',function() {
		var id = parseInt($(this).attr('href'));
		if(isNaN(id)) {
			window.location = base_url+'login';
		}
		if($('#page_overlay').is(':visible')) {
			$('#inner_overlay .content').empty();
		}
		$('#inner_overlay').width(300).height(350);
		$.ajax({
			url: base_url+'action/addToCart/'+id,
			dataType: 'json',
			success: function(data) {	
				removeOverlayLoader();
				if(data[0]=='redirect') {
					window.location = base_url+'login';
				} else {
					$('#inner_overlay .content').append(data[1]);
					storePush();
					$('.featuredHome').empty().load(base_url+'inc/homeLoad');
					var page = location.href.split('/');
					$('.blogProducts').empty().load(base_url+'inc/blogLoad/'+page[page.length-1]);
				}
			}
		}); 
		showOverlay();
	});
	function storePush() {
		$.ajax({
			url: base_url+'action/insertProduct',
			dataType: 'json',
			success: function(data) {}
		}); 
	}
	
	$('.contact_overlay').live('click',function() {
		if($('#page_overlay').is(':visible')) {
			$('#inner_overlay .content').empty();
		}
		$('#inner_overlay').width(400).height(450);
		$.ajax({
			url: base_url+'action/contactInfo',
			dataType: 'json',
			success: function(data) {	
				removeOverlayLoader();
				if(data[0]=='failure') {
					$('#inner_overlay').height('auto');
				} 
				$('#inner_overlay .content').append(data[1]);
			}
		}); 
		showOverlay();
	});
	$('.remove_overlay').live('click',function() {
		hideOverlay();
	});
	
	function removeOverlayLoader() {
		$('#inner_overlay .content .overlayLoader').remove();
	}
	
	/* ---------------- FUNCTIONS ----------------- */
	function overlayControl() {
		if($(window).height()>$('#container').height()) {
			$('#page_overlay').height($(window).height());
		} else {
			$('#page_overlay').height($('#container').height());
		}
		if($(window).width()<1150) {
			$('#page_overlay').width(1150);
		} else {
			$('#page_overlay').width($(window).width());
			$('#page_overlay').css({left:0});
		}
	}
	function showOverlay() {
		var posLeft = 0;
		if($(window).width()>$('#container').width()) {
			posLeft = ($(window).width()-$('#inner_overlay').width())/2;
		} else {
			posLeft = ($(window).width()-$('#inner_overlay').width())/2+(($(window).width()-$('#container').width())/2*-1);
		}
		$('#inner_overlay').addClass('darkBorder').addClass('thickBorder').css({
			top: $(window).scrollTop()+($(window).height()-$('#inner_overlay').height())/2.5,
			left: posLeft
		});
		$('#page_overlay').fadeTo(200, 0.3, function() {  });
		$('#inner_overlay').fadeIn(400);
		$('.close_overlay').clone().removeClass('hidden').appendTo('#inner_overlay .content').width($('#inner_overlay').width()-16).click(function() {
			hideOverlay();
		});
	}
	function hideOverlay() {
		if($('#page_overlay').is(':visible')) {
			$('#inner_overlay').removeClass('darkBorder').removeClass('thickBorder').width('auto').height('auto').css({
				top: 0,
				left: 0
			});
			$('#inner_overlay').fadeOut(200);
			$('#page_overlay').fadeOut(200);
			$('#inner_overlay .content').empty();			
			//$('.close_overlay .mediumSize').removeClass('hidden');
		}
	}
	
});
