// JavaScript Document

// EDIT THESE VALUES IF REQUIRED
var alertText = 'Are you sure you wish to perform this action' + "\n"; //DEFAULT TEXT DISPLAYED ON CONFIRM BUTTONS/LINKS WHEN NO ALT/TITLE
var newWindowLink = true; //OPEN EXTERNAL LINKS BY DEFAULT IN A NEW WINDOW (TRUE/FALSE)?


var woms = new Array(); //CREATE ARRAY OF FUNCTIONS TO LAUNCH ONLOAD

//ONLOAD MANAGER
function womOn(){
  window.onload = womGo;
}

function womGo(){
  for(var i = 0;i < woms.length;i++)
    eval(woms[i]);
}

function womAdd(func){
  woms[woms.length] = func;
}

function CreateBookmarkLink() {

	title = document.title;
	url = window.location.href;
	
	if (window.sidebar) { 
		window.sidebar.addPanel(title, url,"");
	} else if(window.external) {
		window.external.AddFavorite( url, title); 
	} else if(window.opera && window.print) {
		return true;
	}
	
}

function objToggle(obj) { 

	var obj = document.getElementById(obj);

	if(obj.style.display == "block") { 
		obj.style.display = "none";
	} else { 
		obj.style.display = "block";
	}

}

//Toggles the tickboxes that appear on the basket grid
/*function basketEffects() {
	
	if($('#basket_form').length == 0) return false; //Check to see if the page has the basket form on it
	
	//var labels = $('#basket_form label'); //Get the labels that appear on the form
	var inputs = $('#basket_form input[type="checkbox"]');
	var id = '';

	inputs.focus(function() {

	//labels.click(function () { //Listen to see if the label has been clicked on
			
		var id = $(this).attr('id');
		var label = $('label[for="' + id + '"]');
			
		if(label.attr('class') != 'added' && label.attr('class') != 'no_stock') { //If the product isn't already in the users basket					   
			label.attr('class', 'added'); //Apply the added class
					 
		} else if(label.attr('class') != 'no_stock') { //The user has deselected the item so need to show the appropriate icon
		
			var attribute = label.attr('for'); //Get the labels for attribute, this way we'll not which variation and size is selected
			var attr_bits = attribute.split('_'); //Split the attr up on _
			var variation_id = attr_bits[1]; //The 2 part of the array is the variation id
			var size_id = attr_bits[2]; //The 3 part of the array is the size id
		
			//Use AJAX to get the current stock level of the item selected
			$.ajax({
				
				type: 'GET',
				url: 'libs/components/ajax/basket_status.php?variation_id='+variation_id+'&size_id='+size_id,
				success: function(class_name){
					
					$('label[for="selection_'+variation_id+'_'+size_id+'"]').attr('class', class_name); //Update the class name of the label to display the correct status
					
				}
	
			});
		
		}
						   
	});
	
}*/

function basketEffects() {
	
	if($('#basket_form').length == 0) return false; //Check to see if the page has the basket form on it
	$('#selected').removeClass('hidden');
	var labels = $('#basket_form label'); //Get the labels that appear on the form
	//var inputs = $('#basket_form input[type="checkbox"]');
	var id = '';

	//inputs.focus(function() {

	labels.mouseup(function () { //Listen to see if the label has been clicked on
			
		var id = $(this).attr('id');
		//var label = $('label[for="' + id + '"]');
			
		if($(this).attr('class') != 'added' && $(this).attr('class') != 'no_stock') { //If the product isn't already in the users basket					   
			$(this).attr('class', 'added'); //Apply the added class
					 
		} else if($(this).attr('class') != 'no_stock') { //The user has deselected the item so need to show the appropriate icon
		
			var attribute = $(this).attr('for'); //Get the labels for attribute, this way we'll not which variation and size is selected
			var attr_bits = attribute.split('_'); //Split the attr up on _
			var variation_id = attr_bits[1]; //The 2 part of the array is the variation id
			var size_id = attr_bits[2]; //The 3 part of the array is the size id
		
			//Use AJAX to get the current stock level of the item selected
			$.ajax({
				
				type: 'GET',
				url: 'libs/components/ajax/basket_status.php?variation_id='+variation_id+'&size_id='+size_id,
				success: function(class_name){
					
					$('label[for="selection_'+variation_id+'_'+size_id+'"]').attr('class', class_name); //Update the class name of the label to display the correct status
					
				}
	
			});
		
		}
						   
	});
	
}

function hideBasketCheckboxs() {

	if($('#basket_form').length == 0) return false;

	$('input[type="checkbox"]').each(function() {
											 
		$(this).addClass('checkbox');
	
	});

}

function useBillingAddress() {

	if($('#use_billing').length == 0) return false;
	
	$('#use_billing').parent().removeClass('hidden'); //Remove the hidden class off the li
	
	$('#use_billing').click(function() { //Listen if the checkbox has changed status
		
		if($(this).is(':checked')) {
		
			$('fieldset input').each(function() {
							
				updateVal($(this), true);	
			
			});
			
			$('fieldset select').each(function() {
											   
				updateVal($(this), true);	
			
			});
		
		} else { //The box is unticked
		
			$('fieldset input').each(function() {
											  
				updateVal($(this), false);
			
			});
			
			$('fieldset select').each(function() {
											  
				updateVal($(this), false);
			
			});
		
		}
		
	});

}

function updateVal(obj, value) {
	
	var elem_id = obj.attr('id'); //Get the id attribute of the input
				
	if(elem_id.indexOf('delivery') < 0 && $('#delivery_' + elem_id).length > 0) { 
		
		var obj_value = '';
		
		if(value == true) {
		
			obj_value = obj.val();
		
		}
		
		$('#delivery_' + elem_id).val(obj_value); //Update the delivery field with the billing information
	
	}
	
}

//Enlarge the product images
function imageEnlarge() {
	
	if($('.enlarge').length == 0) return false;
	
	$('.enlarge').click(function() {
	
		var divs = $('#large_images div');
		divs.removeClass('top_image');
		divs.addClass('under_image');
		
		var id = $(this).attr('id');
		var image_id = 'enlarge_' + id;
		
		$('#' + image_id).removeClass('under_image');
		$('#' + image_id).addClass('top_image');
		
		$('#newsletter_textfield').focus();
		
		return false;
	
	});
	
	
}

//Launch JQuery
$(document).ready(function() {

	hideBasketCheckboxs();
	basketEffects();
	useBillingAddress();
	imageEnlarge();
	var options = {
		zoomWidth: 500,
	    zoomHeight: 450	
	};
	$('.magnify').jqzoom(options);
	
	if ($('.add_child').length>0) {
		$('.add_child').css('display', 'block');
		$('.remove').css('display', 'block');
	}

});

// add child on My Account pages
function add_child() {
	
	// work out the next child element id
	i=0;
	while(document.getElementById('newchild_name'+i)) { i++; }

	// create html of child elements
	newchild = '<li id="newchild'+i+'"><ol>';
	newchild += '<li><a class="remove" style="display:block;" href="javascript:remove_child('+i+', \'new\');">Remove</a></li>';
	newchild += '<li><label for="newchild_name'+i+'">Name <span class="req">*</span></label> <input type="text" name="newchild_name['+i+']" id="newchild_name'+i+'" value="" /></li>';
	newchild += '<li><label for="newchild_age'+i+'">Age <span class="req">*</span></label> <input type="text" maxlength="5" name="newchild_age['+i+']" id="newchild_age'+i+'" value="" /></li>';
	newchild += '<li>';
	newchild += '<label for="newchild_gender'+i+'">Boy/Girl <span class="req">*</span></label>';
	newchild += '<select name="newchild_gender['+i+']" id="newchild_gender'+i+'"> ';
	newchild += '<option value="">Select</option>';
	newchild += '<option value="m">Boy</option>';
	newchild += '<option value="f">Girl</option>';
	newchild += '</select>';
	newchild += '</li>';
	newchild += '</ol></li>';

	// add new child to the ul as an li
	$('#child_list').append(newchild);
}

// remove child with dom id 'i' from My Account page
function remove_child(i, isnew) {
	// hide the li
	$('#'+isnew+'child'+i).css('display', 'none');
	
	// set fields to blank ready to be deleted when the form submits
	$('#'+isnew+'child_name'+i).attr('value', '');
	$('#'+isnew+'child_age'+i).attr('value', '');
	$('#'+isnew+'child_gender'+i).attr('selectedIndex', '0');
}
