var submitVars = function (zoom,xoffset,yoffset){
	//function called in flash image editor to set the zoom, xoffset and yoffset values
	//alert(zoom+','+xoffset+','+yoffset);
	$('input#zoom').val(zoom);
	$('input#xoffset').val(xoffset);
	$('input#yoffset').val(yoffset);
}


var passimagetoflash = function (){
	//if($('p.newfilename', window.frames['upl'].document).text().length) {
		
		var url = $('p.newfilename', window.frames['upl'].document).text();
		//alert(url);
		if($('input#imgurl').length){
			$('input#imgurl').val(url)
		} else {
			$('.mugshot-field').append('<input type="hidden" name="imgurl" value="'+url+'" id="imgurl" />');
		}
		if(navigator.appName == "Microsoft Internet Explorer"){
			window.document.imager_name.receiveURL(url);
		} else {
			document.imager_name.receiveURL(url);
		}
	//}
			
}

var replaceselects = function(){
	//replace all the select lists with a container class of selector
	$('.selector').each(function(i){
		var curr = $(this);
		var ltext = $(curr).children('label').text();
		$(curr).children('label').before('<p class="0"><span>'+ltext+'</span></p>');
		$(curr).children('label').remove();
		var inputname = $(curr).children('select').attr('name');
		var inputid = $(curr).children('select').attr('id');
		$(curr).children('select').before('<ul></ul><input type="hidden" name="'+inputname+'" value="0" id="'+inputid+'" />');
		$(curr).children('select').children('option').each(function(j){
		
			var opttext = $(this).text();
			var optval = $(this).val();
			if(optval != '0') {
				$(curr).children('ul').append('<li class="'+optval+'"><span>'+opttext+'</span></li>');
			}		
		});
		
		$(curr).children('select').remove();
		
	});
	selectors();
	$('.selector').removeClass('selector').addClass('selectored');
	
}

var deletefriend = function(){

	if($('.friendholder').length == 1) {	
		$('a.deletefriend').hide();
	} else {
		$('a.deletefriend').show();
	}
	
	$('a.deletefriend').unbind('click').click(function(){
		
		if($('.friendholder').length > 1) {
			$(this).parents('.friendholder').animate({'height':0,'opacity':0},function(){
				$(this).remove();
				deletefriend();
			});
		}
		
		return false;
	});

}

var selectors = function(){
	//custom selectors
	$('.selector p').click(function(){
		//close all the others first
		
		var sp = $(this);
		var slist = $(this).siblings('ul');
		if($(slist).css('display') == 'none') {
			$('.selectored ul').hide();
			$('.selectored').css({'z-index':1});
			$(slist).parents('.selectored').css({'z-index':200});
			$(slist).focus().show().children('li').hover(function(){
				$(this).siblings('li').removeClass('hover');
				$(this).addClass('hover');
			},function(){
				$(this).removeClass('hover');	
			}).click(function(){
				$(this).removeClass('hover');
				var newtext = $(this).text();
				var newval = $(this).attr('class');
				$(sp).children('span').text(newtext)
				$(sp).removeClass().addClass(newval); 
				$(sp).siblings('input').val(newval);
				$(slist).hide();
				$(slist).parents('.selectored').css({'z-index':1});
				
			});
			$(slist).blur(function(){
				//alert('yep');
			});
		
		} else {
			
			$(slist).hide();
			$(slist).parents('.selectored').css({'z-index':1});
		}
	});
}
var textinputs = function(){

	$('.friendholder input.text').each(function(k){
	
		var lbltext = $(this).siblings('label').text();
		if($(this).val() == '') {
			$(this).val(lbltext);
		}
	
	}).focus(function(){
	
		var lbltext = $(this).siblings('label').text();
		if($(this).val() == lbltext) {$(this).val('');}
		
	}).blur(function(){
	
		var lbltext = $(this).siblings('label').text();
		if($(this).val() == lbltext || $(this).val() == '') {$(this).val(lbltext);}

	
	});
	
	

}

var flashrecorder = function(){

	$('a.mic').unbind('click').click(function(){
		
		var name = $(this).parents('.friendholder').find('h4').attr('class');
		var flashid = 'record' + name;
		
		$(this).parents('.qholder').hide().siblings('.micflashholder').show().children('div').attr({"id":flashid});
		
		var record = new SWFObject("flash/Recorder.swf", "record"+name, "430px", "138px", "9", "#000000");
		record.addParam("wmode", "transparent");
		//record.addParam("allowscriptaccess", "always");
		record.addVariable("name", name);
		record.write(flashid);
		
		return false;
	
	});


}
var errorFLV = function(name){
	var divid = 'record'+name;
	var holder = $('#' + divid).parents('.micflashholder');
	$(holder).hide().html('<div></div>');
	$(holder).siblings('.qholder').show();
	$('input#question'+name).val('0');
}
var submitFLV = function(filename,name){
	$('input#question'+name).val(filename).siblings('.qholder').find('input[name^="fquestion"]').val('0').siblings('p').attr({'class':'0'}).children('span').text('Choose from list');
		
}
var validatefriend = function(){

	$('.friendholder input.name').unbind('blur').bind('blur',function(){
		if(!ncheck($(this).val()) || $(this).val() == 'Enter their full name') {$(this).addClass('invalid')} else {$(this).removeClass('invalid')}	
	});

	$('.friendholder input.email').unbind('blur').bind('blur',function(){
		if(!echeck($(this).val()) || $(this).val() == 'Enter their email address') {$(this).addClass('invalid')} else {$(this).removeClass('invalid')}	
	});
	
	$('.selectored input').unbind('change').change(function(){
		$(this).sibling('p').removeClass('invalid');
	});
	$('input[name^=questionfriend]').unbind('change').change(function(){
		//alert('value entered');
		$(this).sibling('.qholder').find('p').removeClass('invalid');
	});
	$('button.send').unbind('click').click(function(){
		
		$('.friendholder').each(function(l){
			if($(this).children('.selectored').children('input').val() == '0') {
				$(this).children('.selectored').children('p').addClass('invalid');
			}
			var question = $(this).children('.question');
			
			if(($(question).children('input[@name^="questionfriend"]').val() == '0') && ($(question).children('.qholder').children('.selectored').children('input[name^=fquestion]').val() == '0')){
				$(question).children('.qholder').children('.selectored').children('p').addClass('invalid');
			} else {
				$(question).children('.qholder').children('.selectored').children('p').removeClass('invalid')
			}
		});
			$('input').trigger('blur');
		
			if($('input').hasClass('invalid') || $('p').hasClass('invalid')) {
				return false;
			} else {
				return true;
			}

		
	});
}
var validateyou = function(){
	$('.sender_details input.name').bind('blur',function(){
		if(!ncheck($(this).val())) {$(this).addClass('invalid')} else {$(this).removeClass('invalid')}	
	});

	$('.sender_details input.email').bind('blur',function(){
		if(!echeck($(this).val())) {$(this).addClass('invalid')} else {$(this).removeClass('invalid')}	
	});

	
	$('.sender_details a.friend-details, div.mugshot-field a').click(function(){
		$('.sender_details').find('input').trigger('blur');
		if($('.sender_details input.invalid').length){
			return false;
		} else {
			$('.mugshot-flashholder').css({'top':'-1000px'});
			$('.sender_details').hide();
			$('.target_details').fadeIn('fast');
			//tagCertifica(18830,'/sites/dexterhitlist/form_target');
			return false;
		}
		
	});
}
//email checker
//email address must have one (@) and atleast one (.)
//no spaces or extra '@'s are allowed, nor is a (.) just before or after the @
//must be at least one (.) after the @
function echeck(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
		
	if (str.indexOf(at)==-1){return false;}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){return false;}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){return false;}
	if (str.indexOf(at,(lat+1))!=-1){return false;}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){return false;}
	if (str.indexOf(dot,(lat+2))==-1){return false;}
	if (str.indexOf(" ")!=-1){return false;}

	return true;					
}

//name checker
//name must be at least 2 characters long
function ncheck(str) {
	if (str.length < 2) {return false;} else {return true;}
}
var preview = function(){

	$('.preview a').unbind('click').click(function(){
		
		if($(this).hasClass('close')) {
		
			$(this).siblings('.previewholder').empty().animate({'height':'0px'},'fast','swing');
			$(this).removeClass('close');
			$(this).siblings('.preview')
			return false;
		
		} else {
		
			var currfriend = $(this).parents('.friendholder');
				
			if($(currfriend).children('.selectored').children('input').val() == '0') {
				$(currfriend).children('.selectored').children('p').addClass('invalid');
			}
			
			var question = $(currfriend).children('.question');
				
			if(($(question).children('input[@name^="questionfriend"]').val() == '0') && ($(question).children('.qholder').children('.selectored').children('input[name^=fquestion]').val() == '0')){
				$(question).children('.qholder').children('.selectored').children('p').addClass('invalid');
			} else {
				$(question).children('.qholder').children('.selectored').children('p').removeClass('invalid')
			}
			
			$(currfriend).find('input').trigger('blur');
			
			if($(currfriend).find('input').hasClass('invalid') || $(currfriend).find('p').hasClass('invalid')) {
				return false;
			} else {
				$(this).siblings('.previewholder').animate({'height':'335px'},'fast','swing',function(){
				
					
					var fid = $(this).parents('.preview').siblings('h4').attr('class');
					$(this).append('<div id="preview'+fid+'"></div>');		
					
					var friend = $(this).parents('.friendholder');
					
					var fname = $(friend).find('input.name').val();			
					if($('input#imgurl').length){var photo = $('input#imgurl').val();} else {var photo = '0';}
					if($(friend).find('input[@name^="questionfriend"]').val() == 0) {
						var question = $(friend).find('.question .qholder .selectored input[@name^="fquestion"]').val();
						var filename = 0;
					} else {
						var question = 0;
						var filename = $(friend).find('input[@name^="questionfriend"]').val();
					}
					
					var zoom = $('input#zoom').val();
					var xoffset = $('input#xoffset').val();
					var yoffset = $('input#yoffset').val();
					var age = $(friend).children('div:eq(2)').children('input').val();
					var job = $(friend).children('div:eq(3)').children('input').val();
					
					
					$.post('fileprocess.php',{'imageurl':photo,"zoom":zoom,"xoffset":xoffset,"yoffset":yoffset,"questionfriend":filename},function(data){
											
						var image = $(data).find('image').text();
						var sound = $(data).find('sound').text();
		
						var preview = new SWFObject("flash/Player.swf", "preview"+fid, "400px", "335px", "9", "#333333");
						
						preview.addVariable("fname",fname);
						preview.addVariable("image",image);
						preview.addVariable("question",question);
						if(question == 0){preview.addVariable("questionsound",sound);}
						preview.addVariable("age",age);
						preview.addVariable("job",job);
						preview.addParam("wmode", "transparent");
						preview.write("preview"+fid);
					
					
					},'xml');
					
				
				});
				$(this).addClass('close');
				return false;
			}
		}		
	});

}
var mugshotchange = function(){
	//alert('mugshot change');	
	$('.mugshot-input',window.frames['upl'].document).unbind('change').change(function(){
		
		//alert('working');
		
		if(navigator.appName == "Microsoft Internet Explorer"){
			window['imager_name'].loading(); 		
		} else {
			document.imager_name.loading();
		}			
		
		$('button#uploader',window.frames['upl'].document).trigger('click');
		
	});
}
$(document).ready(function(){
	//var randomnumber=Math.floor(Math.random()*100001);
	var imgr = new SWFObject("flash/Imager.swf", "imager_name", "206px", "146px", "9", "#000000");
	imgr.addParam("wmode", "transparent");
	imgr.addParam("AllowScriptAccess", "always");
	//imgr.addVariable("num",randomnumber);
	imgr.write("imager");

	replaceselects();
	flashrecorder();
	validatefriend();
	validateyou();
	textinputs();
	preview();
	deletefriend();
	$('a.skip').click(function(){
		
		$('.container').hide().load('inc_sendon1.php',function(){
			$('body').removeClass('intro').addClass('sendon');
			$('.container').fadeIn('slow');
		});
		
		return false;
	
	});
	$('a.back').click(function(){
		$('.target_details').hide();
		$('.sender_details').fadeIn('fast',function(){
			$('.mugshot-flashholder').css({'top':'260px'});
		});
		//tagCertifica(18830,'/sites/dexterhitlist/form_sender');
		return false;
	});
	$('a.addfriend').click(function(){
		var num = $('input#friendcount').val();
		num++;
		$('input#friendcount').val(num);
		$.post('inc_friendholder.php',{'friendno':num},function(data){
			$('fieldset.target_details .buttons').before(data);
			replaceselects();
			flashrecorder();
			validatefriend();
			textinputs();
			preview();
			deletefriend();
		},'html');
		return false;
	});
	

	
		mugshotchange();

	
});
