function changeTab(new_tab) {
	if (new_tab == '1') {
		var old_tab = '2';
		var new_class = 'small-active';
		var old_class = 'big';
	} else {
		var old_tab = '1';
		var new_class = 'big-active';
		var old_class = 'small';
	}
	$('#bottom-tab-' + old_tab).hide();
	$('#tab-' + old_tab).attr('class', old_class);
	$('#tab-' + new_tab).attr('class', new_class);
	$('#bottom-tab-' + new_tab).show();
}

var login_visible = false;
function showLogin() {
	if (login_visible) {
		$('#login-form').hide();
		login_visible = false;
	} else {
		var offset = $('#login-btn').offset();
		$('#login-form').css('left', (offset.left - 173));
		$('#login-form').show();
		login_visible = true;
	}
}

var country_selector_visible = false;
function showCountrySelector() {
	if (country_selector_visible) {
		$('#country-selector-form').hide();
		country_selector_visible = false;
	} else {
		var offset = $('#country-selector').offset();
		$('#country-selector-form').css('left', (offset.left - 214));
		$('#country-selector-form').show();
		country_selector_visible = true;
	}
}



function clearText(thefield){
	if (thefield.defaultValue==thefield.value){thefield.value = "";}
}
var popupStatus = 0;

function loadPopup(){
	if(popupStatus==0){
		$('#backgroundPopup').css({
			'opacity': '0.7'
		});
		$('#backgroundPopup').fadeIn('medium');
		$('#popupWindow').fadeIn('medium');
		popupStatus = 1;
		$('body').css('overflow','hidden');
	}
};
function disablePopup(){
	if(popupStatus==1){
		$('#backgroundPopup').fadeOut('medium');
		$('#popupWindow').fadeOut('medium');
		popupStatus = 0;
		$('body').css('overflow','auto');
	}
};

function centerPopup(vertical_pos){
	if (vertical_pos == null) vertical_pos = 'top';
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $('#popupWindow').height();
	var popupWidth = $('#popupWindow').width();
	
	if (vertical_pos == 'top') {
		
		$('#popupWindow').css({
			'position': 'fixed',
			'top': '20px',
			'left': windowWidth/2-popupWidth/2
		});
	} else if (vertical_pos == 'bottom') {
		$('#popupWindow').css({
			'position': 'absolute',
			'left': windowWidth/2-popupWidth/2
		});
	}
	
	$('#backgroundPopup').css({
		'height': windowHeight
	});
};
var hoverColour = '#ffffff';

function changeCart( e) {
	var id = e.attr('rel'), url = e.attr('href');
	$.ajax({
		'type': 'POST', 
		'url': url, 
		'data': 'id='+id,   
		'dataType': 'json', 
		'success': function(data){
			if(e.hasClass('mini')) {
				$('#cart-box-products-holder').html(data.html);
				$('#cart-box-total').html(data.total);
			} else {
				$('#cartContent').html(data.html);
				if(data.checkout_ready == true) {
					$('#forward-to-checkout').show();
				} else {
					$('#forward-to-checkout').hide();
				}
			}
			if(!e.hasClass('cart-remove')) {
				$('#quantity-' + id).effect('highlight', {}, 2000);
				$('#total-' + id).effect('highlight', {}, 2000);
			}
		  } 
	  });
};


function ajaxSubmit(e, holder) {
	var myForm = e.closest('form');
	$.ajax({
		'type': 'POST', 
		'url': myForm.attr('action'), 
		'data': myForm.serialize(), 
		'dataType': 'json',
		'success': function(data){
			if(data.update != 'null') {
				disablePopup();
				if(data.update == 'deliveryAddressData' || data.update == 'invoiceAddressData' ) {
					$('#'+data.update).html(data.html).effect('highlight', {}, 2000);
				} 
			} else {
				holder.html(data.html);
			}
		  } 
	  });
}

function toggleCategoryNavigation(e) {
	if((e.hasClass('visible'))) {
		
		e.children('ul').slideUp('fast');
		e.removeClass('visible');
		e.children('span').text('▼');
	} else {
		e.children('ul').slideDown('fast');
		e.addClass('visible');
		e.children('span').text('▲');
	}
};

function activeCategoryNavigation(e) {
	 if(!e.hasClass('level-1')) {
		 var p= e.closest('ul').closest('li');
		 p.addClass('active visible');
		 p.children('span').text('▲');
	 } else {
		 e.adClass('active');
	 }
};

$(document).ready(function() {
	$('#backgroundPopup').click(function(){
		disablePopup();
	});	
	
	$('.navigation-parent p').click(function(){
		toggleCategoryNavigation($(this).closest('li'));
	});
	
	$('a.cart-change').live('click',function() {
		changeCart($(this));
		return false;
	});
	
	$('#cart-box-holder').hover(
	  function () {
		  $('#cart-box-hover').slideDown('fast')
	  }, 
	  function () {
		  $('#cart-box-hover').slideUp('fast')
	  }
	);
	
	$('#add-to-cart').click(function() { 
		if(!$(this).hasClass('disabled')) {
			addToCart($(this));
		} else {
			alert($(this).attr('title'));
		}
		return false;
	});
	if($('#add-to-cart-value').length>0) {
		$('#add-to-cart-value').keyup(function(e) {
			if(e.keyCode == 13) {
				$("#add-to-cart").trigger('click');
			}
		});
	};
	
	$('a.submit-form').live('click', function(){
		$(this).closest('form').submit();		
	});
	
	if($('li.active-category-item').length>0) {
		activeCategoryNavigation($('li.active-category-item'));
	};

	if($('.edit-address').length>0) {
		$('.edit-address').click(function() {
		var url = $(this).attr('href'), input = null;
		if($(this).hasClass('edit-delivery')) {
			input = 'id='+ $('#address_select_delivery').val() + '&type=delivery';
		} else if($(this).hasClass('edit-invoice')) {
			input = 'id='+ $('#address_select_invoice').val() + '&type=invoice';
		};
		$.ajax({
			'type': 'GET', 
			'url': url, 
			'data': input,
			'dataType': 'json',
			'success': function(data){
					centerPopup();
					loadPopup();
					$('#popupContent').html(data.html);
			  } 
		  });
		return false;
	});
	};
	
	$('.close-popup').live('click',function() {
		disablePopup();
		return false;
	});
	
	$('.ajax-submit').live('click',function() {
		ajaxSubmit($(this), $('#popupContent'));
		return false;
	});
	$('#addressCreated').live('click', function() {
		var myForm = $(this).closest('form');
		$.ajax({
			'type': 'POST', 
			'url': myForm.attr('action'), 
			'data': myForm.serialize(), 
			'success': function(data){
				$('#deliveryAddressBox').html(data.delivery);
				$('#invoiceAddressBox').html(data.invoice);
				$('#checkout_delivery').val(data.id);
				$('#checkout_invoice').val(data.id);
				disablePopup();
			  } 
		  });
		return false;
	});
	
	$('.address-select').live('change', function() {
		if($(this).attr('id') == 'address_select_delivery') {
			$('#checkout_delivery').val($(this).val());
			$.ajax({
				'type': 'POST', 
				'url': $(this).attr('data'), 
				'data': 'id='+$(this).val(), 
				'success': function(html){
					$('#deliveryAddressData').html(html).effect('highlight', {}, 2000);
				  } 
			  });
		} else {
			$('#checkout_invoice').val($(this).val());
			$.ajax({
				'type': 'POST', 
				'url': $(this).attr('data'), 
				'data': 'id='+$(this).val(), 
				'success': function(html){
					$('#invoiceAddressData').html(html).effect('highlight', {}, 2000);;
				  } 
			  });
		}
	});
	
	$('#confirm-order').click(function(){
		var cssObj = {'height':220,'width':300,'text-align':'center'}, cssheight = 178;
		if($(this).hasClass('credit_card')) {
			cssObj = {'height':680,'width':460,'text-align':'center'};
			cssheight = 638;
			$('#popupContent').html('<iframe src="' + $(this).attr('data') + '" width="400" height="600" frameborder="0"></iframe>');
		} else {
			$('.loader').css({'margin':'10px 0 10px 0'});
			$('#popupContent').html(jQuery('#confirm_loading').css({'display':'block'}));
			$('#confirm-form').submit();
		}
		$('html, body').animate({scrollTop:0}, 'slow');
		centerPopup(); loadPopup();
		
	});
	
});

$(window).resize(function() {
	if ($('#login-form').length) {
		var offset = $('#login-btn').offset();
		$('#login-form').css('left', (offset.left - 173));
	}
	if ($('#country-selector-form').length) {
		var offset = $('#country-selector').offset();
		$('#country-selector-form').css('left', (offset.left - 214));
	}
});
