$(function() {
		/// cache bmlform	
		$.get(window.location,{task:"popdb"},function(data){});
		///
		
		$("#bmltabs" ).tabs();
		
		$("#bml_create_forms_list" ).tabs();
		
		$("#bml_startdate").datepicker({
			changeMonth: true,
			changeYear: true
		});
		
		$("#bml_endate").datepicker({
			changeMonth: true,
			changeYear: true
		});
		
		$("#bml_endate").focusout(function(){
			var endDate = new Date($("#bml_endate").val());							
			var startDate = new Date($("#bml_startdate").val());
			
			if(endDate < startDate){
				$("#bml_date_error").html("end date should be greater than start date");	
			}else{
				$("#bml_date_error").html("");
			}
		});
		
		$("#bml_startdate").focusout(function(){
			var endDate = new Date($("#bml_endate").val());							
			var startDate = new Date($("#bml_startdate").val());
			if(endDate < startDate){
				$("#bml_date_error").html("end date should be greater than start date");
				$("#bml_date_error").html("");
			}
		});
	
		////////////////
		// modal iframe upload
				
		$("#bml_iframe_upload").hide();
				
		$("#bml_iframe_upload_button").click(function(){
			$("#bml_iframe_upload").dialog({
				height: 150,
				modal: true,
				resizable:false,
			});
			$("#bml_iframe_upload").show();
		});
		
		//////
		// identify template if it allow paragraph  on field
		tpl_paragraph_field = function(tplid){
			//tplParagraphField global.js.php
			v = tplParagraphField[tplid];
			if( parseInt(v) > 0 ){
				$("#bml_field_paragraph1").show();
			} else {
				$("#bml_field_paragraph1").hide();
			}
		}
		
		//////
		// identify template if it allow paragraph two field
		tpl_paragraph_field_two = function(tplid){
			//tplParagraphField global.js.php
			v = tplParagraphFieldTwo[tplid];
			if( parseInt(v) > 0 ){
				$("#bml_field_paragraph2").show();
			} else {
				$("#bml_field_paragraph2").hide();
			}
		}
		
		//////
		// identify template if it allow paragraph two field
		tpl_video_field = function(tplid){
			//tplParagraphField global.js.php
			v = tplVideoField[tplid];
			if( parseInt(v) > 0 ){
				$("#bml_field_video").show();
			} else {
				$("#bml_field_video").hide();
			}
		}
		
		/////
		// identify if image field show
		tpl_image_field = function(tplid){
			//tplParagraphField global.js.php
			v = tplImageField[tplid];
			if( parseInt(v) > 0 ){
				$("#bml_field_image").show();
			} else {
				$("#bml_field_image").hide();
			}
		}
		
		/////
		// identify if name field show
		tpl_name_field = function(tplid){
			//tplParagraphField global.js.php
			v = tplNameField[tplid];
			if( parseInt(v) > 0 ){
				$("#bml_field_name_default").show();
			} else {
				$("#bml_field_name_default").hide();
			}
		}
		
		/////
		// identify if name field show
		tpl_email_field = function(tplid){
			//tplParagraphField global.js.php
			v = tplEmailField[tplid];
			if( parseInt(v) > 0 ){
				$("#bml_field_email_default").show();
			} else {
				$("#bml_field_email_default").hide();
			}
		}
		
		
		/////
		// identify if name field show
		tpl_submiturl_field = function(tplid){
			//tplParagraphField global.js.php
			v = tplUrlField[tplid];
			if( parseInt(v) > 0 ){
				$("#bml_field_submiturl").show();
			} else {
				$("#bml_field_submiturl").hide();
			}
		}
		/////
		// identify if html field show
		tpl_html_field = function(tplid){
			//tplParagraphField global.js.php
			v = tplHtml[tplid];
			i = tplHtmlField[tplid];
			
			if( parseInt(v) > 0 ){
				$("#bml_html_box").children().show();
			} else {
				$("#bml_html_box").children().hide();
				if(parseInt(i) > 0){
					$("#bml_html_box").children("#bml_html_field").show();
				}
			}
		}
		
		////
		// 
		tpl_button = function(tplid){
			v = tplButton[tplid];
			
			if( parseInt(v) > 0){
				$("#bml_style_button_color").show();
				$("#bml_field_submit_button").show();
				$("#bml_button_message").html("");
				$("#bml_listitems_button_message").html("");
				$("#bml_listitems_button").show();
				$(".bml_list_info").show();
			}else if(parseInt(tplid) >= 29 && parseInt(tplid) <= 32){ 
				$("#bml_button_message").html("<strong>&nbsp;None</strong>");
				$("#bml_listitems_button_message").html("");
				
				$("#bml_style_button_color").hide();
				$("#bml_field_submit_button").hide();
				
				$("#bml_listitems_button").hide();
				$(".bml_list_info").hide();
				
			}else{
				$("#bml_style_button_color").hide();
				$("#bml_field_submit_button").hide();
			}
		}
		//
		
		////
		// 
		tpl_header = function(tplid){
			v = tplFieldHeader[tplid];
			if( parseInt(v) > 0){
				$("#bml_field_form_header").show();
			} else{
				$("#bml_field_form_header").hide();
			}
		}
		//
		
		//
		// show/hide form fields that is required
		$("#bml_style_template").change(function(){
			tplid = $("select#bml_style_template option:selected").val();
			tpl_paragraph_field(tplid);
			tpl_paragraph_field_two(tplid);
			tpl_video_field(tplid);
			tpl_image_field(tplid);
			tpl_name_field(tplid);
			tpl_email_field(tplid);
			tpl_submiturl_field(tplid);
			tpl_html_field(tplid);
			tpl_button(tplid);
			tpl_header(tplid);
		});
		
		
		
		$("select#bml_style_template option:selected").each(function(){
			tplid = $(this).val();
			tpl_paragraph_field(tplid);
			tpl_paragraph_field_two(tplid);
			tpl_video_field(tplid);
			tpl_image_field(tplid);
			tpl_name_field(tplid);
			tpl_email_field(tplid);
			tpl_submiturl_field(tplid);
			tpl_html_field(tplid);
			tpl_button(tplid);
			tpl_header(tplid);
		});
		
		
		//
		/////
		
		
		preview_style_tab = function(){
			
       		t = $('select.bml_style_template option:selected').val();
			
			b = $('select.bml_style_button_color option:selected').val();
					
			r = $('input:radio[name=bml[color_options]]:checked').val();
			
			$("#bml_style_template_val").html(t);
			
			$("#bml_style_button_val").html(b);
				
			$("#bml_style_color_option_val").html(color_options[r-1]);
			
			if(t < 10){
				t = "00"+t;
			} else if(t < 100 && t >= 10 ){
				t = "0"+t
			}
			
			if(b < 10){
				b = "0"+b;
			}
						
			if(r < 10 ){
				r = "0"+r;
			}
			
			if(screen.width <= 1024 && screen.width > 800){
				img_width =  "style = 'width:360px'";
			}else if(screen.width <= 800){ 
				img_width =  "style = 'width:180px'";
			}else {
				img_width = "";
			}
			
			$("#bml_style_preview").html("<img "+img_width+" src='"+tplPreviewUrl+"t"+t+"b"+b+"c"+r+".png' />");
			
			color_option_selected_bg(color_options[r-1]);
		}
		
		mouseover_preview = function(id){
			t = $('select.bml_style_template option:selected').val();
			
			b = $('select.bml_style_button_color option:selected').val();
					
			r = $('input:radio[name=bml[color_options]]:checked').val();
			
			
			if(t < 10){
				t = "00"+t;
			} else if(t < 100 && t >= 10 ){
				t = "0"+t
			}
			
			if(b < 10){
				b = "0"+b;
			}
						
			if(id < 10 ){
				id = "0"+id;
			}
			
			if(screen.width <= 1024 && screen.width > 800){
				img_width =  "style = 'width:360px'";
			}else if(screen.width <= 800){ 
				img_width =  "style = 'width:180px'";
			}else {
				img_width = "";
			}
					
			$("#bml_style_preview").html("<img "+img_width+" src='"+tplPreviewUrl+"t"+t+"b"+b+"c"+id+".png' />");
			color_option_selected_bg(color_options[id-1]);
		}
		
		color_option_selected_bg = function(val){
			
			for(var i=0; i < color_options.length; i++){
				if(color_options[i] == val){
					
					$("#bml_style_color_options_radio_"+color_options[i]).css("background-color","#c9e6f0");
					
				} else {
					$("#bml_style_color_options_radio_"+color_options[i]).css("background-color","transparent");
				}
			}
			
			
			
		}
		
		preview_style_tab();
		
		///////////////////// validate fields///////////////
		
		validate_fields = function(){
			err = false;
			
			//n = $("#bml_name_box").val();
			//e = $("#bml_email_box").val();
			//u = $("#bml_action").val();
			t = $("#bml_submit_tagline_inpt").val();
			d = $("#bml_submit_description_inpt").val();
			
			/*$(".error_msg").html("");
			if(n == null){
				err = true;
				$(".error_msg").append("<li>HTML name should not be empty</li>");	
			} 
			
			if(e == null){
				err = true;
				$(".error_msg").append("<li>HTML email should not be empty</li>");	
			}
			
			if(u == ""){
				err = true;
				$(".error_msg").append("<li>HTML url should not be empty</li>");	
			} */
			
			if(t == ""){
				err = true;
				$(".error_msg").append("<li>Tagline should not be empty</li>");	
			} 
			
			if(d == ""){
				err = true;
				$(".error_msg").append("<li>Description should not be empty</li>");	
			} 
			
			return err;
					
		}
		
		////////////////////////////////////////////////////
		
		///
		// diaglog box for submit form///////////
		$( "#bml_submit_error_box" ).hide();
		$( "#bml_submit_dialog_confirm" ).hide();
		submit_bml_confirm = function (){
			
			err= validate_fields();		
		
			if(!err){
				
				$( "#bml_submit_dialog_confirm" ).dialog({
					resizable: false,
					height:140,
					modal: true,
					buttons: {
						"Go": function() {
							submit_bml_create_form();
						},
						Cancel: function() {
							$( this ).dialog( "close" );
						}
					}
				});
			
			} else {
				$( "#bml_submit_error_box" ).dialog({
					resizable: false,
					
					modal: true,
					buttons: {
						"close": function() {
							$( this ).dialog( "close" );
						}
					}
				});	
			}
		}
		/////////////////////////////////////////
		
		
		submit_bml_create_form = function (){
			bml = $("#bml_create_form").serialize();
			document.bml_create_form.submit();
		}
		
		refresh_preview = function (){
			//style
			t = $('select.bml_style_template option:selected').val();
			b = $('select.bml_style_button_color option:selected').val();
			r = $('input:radio[name=bml[color_options]]:checked').val();
			
			$("#bml_preview_style_template_val").html(t);
			
			btn_clr = button_color[b-1].toLowerCase();
			$("#preview_field_button").removeClass();
			$("#preview_field_button").addClass(btn_clr+"-button")
			
			$("#bml_preview_style_button_val").html(b);
			
			color_option = color_options[r-1].toLowerCase();
			
			
		//	
			$("#div_lightbox_main").removeClass();
			$("#div_lightbox_main").addClass("lightbox-main lightbox-color-"+color_option);
			
			//html
			f = $("#bml_formhtml").val();
			n = $("#bml_name_box").val();
			e = $("#bml_email_box").val();
			u = $("#bml_action").val();
			
			$("#bml_preview_html_formhtml").html(f);
			$("#bml_preview_html_name").html(n);
			$("#bml_preview_html_formurl").html(u);
			$("#bml_preview_html_email").html(e);
			
			//Fields
			ti = $("#bml_field_title_field").val();
			fh = $("#bml_field_form_header_field").val();
			fs = $("#bml_field_footer_note_field").val();
			nd = $("#bml_field_name_default_field").val();
			ed = $("#bml_field_email_default_field").val();
			sb = $("#bml_field_submit_button_field").val();
		
			$("#bml_preview_field_title").html(ti);
			$("#bml_preview_field_formheader").html(fh);
			$("#bml_preview_field_footer_note").html(fs);
			$("#preview_field_name").val(nd);
			$("#preview_field_email").val(ed);
			$("#preview_field_button").val(sb);
			
			
			
			$('.li_remove').remove();
			//lists
			$('.bml_lists_arr').each(function () { 
				li = $(this).val();
				$("#bullet-list-preview").append("<li class='li_remove'>"+li+"</li>");
			});
			
			$.post("../wp-content/plugins/bmlplugin/view/view_ajax_preview.php", $("#bml_create_form").serialize(),
			   function(data) {
				 $("#ajax_preview").html(data);
			   }
			 );
		
			
		}
		
	change_selects = function(){
	
		$('#bml_name_box option, #bml_email_box option').remove();
		
		var tags = ['a','iframe','frame','frameset','script'], reg, val = "<style></style>"+$('#bml_formhtml').val(),
			hdn = $('#bml_hdn_div2'), action = $('#bml_action'), hdn2 = $('#bml_hdn_div');
		
	    action.val('');
		
		if($.trim(val) == '') return false;
			
		hdn2.html('');
		hdn.html('');
			
		/*var tmp = $(val);
		tmp.find('a,iframe,frame,frameset,script,img').remove();
		tmp.find('input[type="image"]').attr('src','');*/
		for(var i=0;i<5;i++){
			reg = new RegExp('<'+tags[i]+'([^<>+]*[^\/])>.*?</'+tags[i]+'>', "gi");
			val = val.replace(reg,'');
			
			reg = new RegExp('<'+tags[i]+'([^<>+]*)>', "gi");
			val = val.replace(reg,'');
		}
		var tmpval;
		try {
			tmpval = decodeURIComponent(val);
		} catch(err){
			tmpval = val;
		}
		
		hdn.html(tmpval);
		autresp = $("#bml_autoresponder_type").val();
		
		if(parseInt(autresp) == 1){
			hdnInput = ":text";
		}else if(parseInt(autresp) == 3){ 
			hdnInput = ":text";
		}else if(parseInt(autresp) == 4){ 
			hdnInput = ":text";
		}else if(parseInt(autresp) == 5){ 
			hdnInput = ":text";
		}else if(parseInt(autresp) == 6){ 
			hdnInput = ":text";
		}else {
			hdnInput = "input";
		}
		
		$(hdnInput,hdn).each(function(){
									 
			var name = $(this).attr('name');
			
			$('#bml_name_box').append('<option value="'+name+'">'+name+'</option>');
			
			$('#bml_email_box').append('<option value="'+name+'">'+name+'</option>');
			
		});
		
		$("select#bml_name_box option").each(function(){
			name_val = $(this).val().toLowerCase();
		
			if(name_val == "name" || name_val == 'fields_fname'){
				$(this).attr("selected","selected");	
			}
		});
		
		$("select#bml_email_box option").each(function(){
			email_val = $(this).val().toLowerCase();
			if(email_val == "email" || email_val == 'fields_email'){
				$(this).attr("selected","selected");	
			}
		});
		
		
		$('input:hidden',hdn).each(function(){
			if(typeof $(this).attr('name') != 'undefined'){
				
				if( $(this).attr('name') != "name" && $(this).attr('name') != "email"){
				
					hdn2.append($('<input type="hidden" name="field_name[]" />').val($(this).attr('name')));
					hdn2.append($('<input type="hidden" name="field_vals[]" />').val($(this).val()));
				}
			}
		});
		$('img',hdn).each(function(){
			hdn2.append($('<input type="hidden" name="field_img[]" />').val($(this).attr('src')));
		});
		console.log(hdn);
		//check_select('#bml_name_box');
		action.val($('form',hdn).attr('action'));
		
		hdn.html('');
	};
	
	
	// begin fields count chars
	
	
	$("#bml_field_title_field").keypress(function(){
		x = $("#bml_field_title_field").val().length;
		$("#bml_title_note").html( 50 - (parseInt(x) + 1 ));
	});
	
	$("#bml_field_form_header_field").keypress(function(){
		x = $("#bml_field_form_header_field").val().length;
		$("#bml_header_note").html( 50 - (parseInt(x) + 1 ));
	});
	
	$("#bml_field_footer_note_field").keypress(function(){
		x = $("#bml_field_footer_note_field").val().length;
		$("#bml_footer_note").html( 60 - (parseInt(x) + 1 ));
	});
					
	// end fields
				
	var p;
	var orig_iframe_src;
	display_bml_form = function(id){
				video = $("#lightboxIframeVideo"+id).val();
				iframe_src = $(video).attr("src");	
				$("#"+id+" .lightbox-iframe-video").html(video);
				$("#"+id+" .lightbox-iframe-video iframe").attr("src",iframe_src+"?fs=1&autoplay=1");
				$(".bml_form_templates").css("display","none");
				$("#"+id).css("display","block");
			}
	close_bml_forms = function(){
				$(".lightbox-iframe-video").children().remove();
				$(".bml_form_templates").hide();	
			}	
	$("#div_confirm_delete").hide();
	confirm_delete = function(url){
	   $( "#div_confirm_delete" ).html("Are you sure you want to delete this form?");
		$( "#div_confirm_delete" ).dialog({
			resizable: false,
			height:140,
			modal: true,
			buttons: {
				"Ok": function() {
					window.location = url;
				},
				Cancel: function() {
					$( this ).dialog( "close" );
				}
			}
		});
	}	
	
	delList = function (element){
		i = $(element).parent().remove();
	}
	
	
	$(".bmlicon").hover(function(){
		$(this).addClass('ui-state-hover');							 
		},
		function(){
			$(this).removeClass('ui-state-hover');								
		}
	);
	
	testxxx = function(){
		test = $("#bml_formhtml").val();
		test = $.base64Decode(test);
		alert(test);
	}
	
	
	
	
	$(".bmlhelpvideo").click(function(){
		var helpVideo = '<iframe width="560" height="315" src="http://www.youtube.com/embed/Z60mKfOZ6zI" frameborder="0" allowfullscreen></iframe>';
		$(".help_video_box").dialog({height:380,width:590,
			close:function(event,ui){
				$(".help_video_content").html("");	
			}
		});
		$(".help_video_content").html(helpVideo);
	});
	
	showHelpVideo = function(url){
		var helpVideo = '<iframe width="560" height="315" src="'+url+'" frameborder="0" allowfullscreen></iframe>';
		$(".help_video_box").dialog({height:380,width:590,
			close:function(event,ui){
				$(".help_video_content").html("");	
			}
		});
		$(".help_video_content").html(helpVideo);
	}
});

