// JavaScript Document
currentRecipient = 0;

$(document).ready(function(){
	
	setupZoom();
	$('#flash_box_link').fancyZoom(); 
	
	var container = $('div.container'); 
	$('#sendPostCard').validate({
								   
							 
								errorLabelContainer: $('div.errorContainer'),
							  
								rules: {
										recipient: "required",
										recipientemail: {
											required: true,
											email: true
										},
										uname:"required",
										youremail: {
											required: true,
											email: true
										},
										agree: "required",
										postcardImg:"required"
									},
								messages: {
										
								 		recipient: {
											required: 'Please enter recipient\'s name'
										},
										recipientemail: {
											required: 'Please enter recipients Email',
											email: 'Please enter a valid recipient\'s Email' 
										} ,
										uname: {
											required: 'Please enter your name'
										},
										youremail: {
											required: 'Please enter your Email',
											email: 'Your Email must be valid' 
										},
										agree: {
											required: 'You must agree to the sweepstakes rules of Spread The Word promotion.'
										},
										postcardImg: "You must select an image"
									}
								
								
								});

	 

	$('#postcard a, .postcard').click(function(e){
			target = e.srcElement || e.target;
			
			e.stopPropagation();
			tag = target.tagName.toLowerCase() ;
			if( tag == 'a' && $(target).attr('id').length > 1) return false;
			
			if(tag != 'input')e.preventDefault();
			
			lnk =  $(target).parent('a').get(0) ||  target;
			 
			
			if($(lnk).attr('class') == 'postcard'){ 
				$(lnk).attr('checked',true).siblings('.imgPrv').click();  
			} 
				 
			
			img = $(lnk).children('img');
			 
			
			$('#postcard a').removeClass('imgCurrent');
			currentIMG = $(lnk).addClass('imgCurrent').attr('href');
			currentThumb =   $(img).attr('src');
			 
			$('#postcardImg').val(currentIMG);
			$('#postcardThumbImg').val(currentThumb);
			
			$('.postcard').attr('checked', false);
			$(lnk).siblings('.postcard').attr('checked', true);
			 
			
	});
	
	$('#addname').click(function(){
		$('#deletename:hidden').show();						 
		addRecipient(++currentRecipient)							 
	});
	
	$('#deletename').click(function(){
								 
		removeRecipient(currentRecipient)							 
	});
	
	
	$('#postcardImg').val('');
	$('#postcardThumbImg').val('');
	$('.postcard').attr('checked', false);
});

function addRecipient($number){
	 		
			className = ($number % 2)?'odd':'';
			
			$newRecipient = $('<ul id="recipienthldr['+$number+']" class='+className+'><li><label for="recipient['+$number+']">Name of Recipient</label><input type="text" name="recipient['+$number+']" id="recipient['+$number+']" value="" /></li><li><label for="recipeintemail['+$number+']">Email of recipient</label><input type="text" name="recipientemail['+$number+']" id="recipientemail['+$number+']" value="" /></li></ul>');
			
			$('#newRecipients').append($newRecipient);
			
			$($newRecipient).children('li:first').children('input').focus();
	 		 
	 
	}
	
function removeRecipient($number){
	 	 
		$addedRecipient = 	$('#newRecipients').children() ;
		
		$($addedRecipient).each(function(i,item){
		 	if($(item).attr('id') == 'recipienthldr['+$number+']'){
				$(item).remove();
				currentRecipient--;
				if(currentRecipient == 0) $('#deletename').hide();
			}
		});
		
	}
