// JavaScript Document
var $j = jQuery.noConflict(), tea_Fullpat = '', tea_currPath = '', tea_Stat = true, notificationCount=0, get_bloginfo = new Array();
$j(document).ready(function(){	
	query='c_action=c_bloginfo';		
	$j.get(tea_Fullpat, query, function(data){
		//get_bloginfo = data;
		var s =data;
		s = s.split(',');
		for(q=0;q<=s.length;q++){			
			if(s[q]){
				var r = s[q];
				r = r.split('|||');
				get_bloginfo[r[0]] = r[1];
			}
		}		
		c_global_Loader();
	});	

	remove_select();
});

function c_global_Loader(){
	
 $j('link').each(function(){
  if($j(this).attr('rel') == 'index')tea_Fullpat = $j(this).attr('href'); 
  if($j(this).attr('rel') == 'canonical')tea_currPath = $j(this).attr('href');
 });
(document.getElementById('tea_dialog'))?'':$j('body').prepend('<div id="tea_dialog"></div>');
 
 c_contact_us();
 //c_settings();
 
}


function ac(a,b,params){
 var filepath = tea_Fullpat+'/settings';
// var filepath = tea_Fullpat+'/wp-content/themes/lawyer-websites/library/includes/output.php';
 //var filepath = global_path_output;
 var q = 'c_action='+a+params;
 var process = filepath;
 $j.get(process, q, function(data){ 
 	//return data;
	$j("#"+b).html(data);
	
 });
}
// -- toggle div --
function toggle_div(obj, togg){
   if(document.getElementById){
	   var el = document.getElementById(obj);
	   if(el){
		   if(togg == 'toggle'){
			  if(el.style.display != "block"){      
				 el.style.display = "block";
			  }else{
				 el.style.display = "none";
			  } 
		   }
		   
		   if(togg == 'hide'){
			  el.style.display = "none";
		   }
		   
		   if(togg == 'show'){
			  el.style.display = "block";      
		   }
	   }
   }
   
}

function confirmation(Txt, a,b,params) {
	var answer = confirm(Txt)
	if (answer){
		toggle_div('portfolio_preloader','show');
		ac(a,b,params);
	}
}

// -- multiple add new file field. --
function ini_addfield(){
	$j('#file_container').empty();
	$j('#file_container').append('<div id="t1" class="fileform"><p><span>Banner Image:</span> <input name="thumb-1" type="file" id="thumb-1" /></p><p><span>Title: </span><input type="text" name="thumb-name-1" id="thumb-name-1" /></p><p><span> Description: </span><textarea name="thumb-desc-1" id="thumb-desc-1" ></textarea></p></div>');
	$j("#num").val(1);
}

function add_field(){
	var n = parseInt($j('#num').val());
	var a = n+1;
	
	$j('#file_container').append('<div id="t'+a+'" class="fileform"><p><span>Banner Image:</span> <input name="thumb-'+a+'" type="file" id="thumb-'+a+'"/></p><p><span>Title: </span><input type="text" name="thumb-name-'+a+'" id="thumb-name-'+a+'" /></p><p><span> Description: </span><textarea name="thumb-desc-'+a+'" id="thumb-desc-'+a+'" ></textarea></p><a onclick="remove_field('+a+')"> remove </a></div>');

	$j("#num").val(a);
	if(a>1){
		a=a-1;
		$j('#t'+a+' a').remove();
	}
}

function remove_field(v){
	var n = parseInt($j('#num').val());
	var b = n-1;
	$j('#t'+v).remove(); 
	$j("#num").val(b);
	if(b>1){
		$j('#t'+b).append('<a onclick="remove_field('+b+')"> remove </a>');
	}
}

function cancel_field(){
	$j('#file_container').empty();
	$j("#num").val(0);
}

function remove_obj(id){
	$j('#'+id).remove();
}

					
// -- edit banner form --
function edit_banner_form(bid,uid,n,d,path){
	var desc = cleaners(d);
	$j('#editbannerform_'+bid).append('<div class="field_content_edit"></div>');
	$j('#editbannerform_'+bid+' .field_content_edit').wrap('<div class="field_edit">');
	
	$j('#editbannerform_'+bid+' .field_content_edit').append('<p><span class="banner_thumb">Banner Image:</span><input type="file" name="p" id="p"/></p>');
	$j('#editbannerform_'+bid+' .field_content_edit').append('<div class="bpreview"><img src="'+path+'" width="150px" /></div>');
	$j('#editbannerform_'+bid+' .field_content_edit').append('<p><span>Title:</span> <input type="text" name="bn" id="bn" value="'+n+'" /></p>');
	$j('#editbannerform_'+bid+' .field_content_edit').append('<p><span>Description:</span> <textarea name="bd" id="bd">'+d+'</textarea></p>');
	$j('#editbannerform_'+bid+' .field_content_edit').append('<input type="hidden" name="banID" value="'+bid+'" />');
	$j('#editbannerform_'+bid+' .field_content_edit').append('<input type="hidden" name="banuid" value="'+uid+'" />');
	$j('#editbannerform_'+bid+' .field_content_edit').append('<input type="submit" name="CA_edit_banner" value="Update changes" /><input type="button" value="Cancel" onclick="cancel_edit_banner(\'editbannerform_'+bid+'\',\''+bid+'\');" />');
	
	//$j('#editbannerform_'+bid).append(' <input type="button" value="Cancel" onclick="cancel_edit_banner(\'editbannerform_'+bid+'\',\''+bid+'\');" />');
	//$j('#editbannerform_'+bid+' input[type=button]').wrap('<div class="field_content_submit"></div>');
}

function cancel_edit_banner(obj,bid){
	$j('#'+obj).empty();
	toggle_div('row_'+bid,'show');
}

// -- add new portfolio --
function ini_portfield(){
	$j('#portfolio_container').empty();
	$j('#portfolio_container').append('<div id="port" class="fileform"><p><span>Title:</span> <input type="text" name="assoc_name" id="assoc_name" /></p><p><span>Badge/Logo:</span> <input name="port_thumb" type="file" id="port_thumb" /></p><p><span>Location: </span> <input type="text" name="assoc_loc" id="assoc_loc" /></p><p><span> Description: </span> <textarea name="assoc_desc" id="assoc_desc" ></textarea></p></div>');
}

// -- edit portfolio form --
function edit_portfolio_form(pid,puid,pname,ploc,pdesc,ppath){
	$j('#editportform_'+pid).append('<div class="field_content_edit"></div>');
	$j('#editportform_'+pid+' .field_content_edit').wrap('<div class="field_edit">');
	
	$j('#editportform_'+pid+' .field_content_edit').append('<p><span class="banner_thumb">Banner Image:</span><input type="file" name="portImg" id="portImg"/></p>');
	$j('#editportform_'+pid+' .field_content_edit').append('<div class="bpreview"><img src="'+ppath+'" height="70px" /></div>');
	$j('#editportform_'+pid+' .field_content_edit').append('<p><span>Title:</span> <input type="text" name="portname" id="portname" value="'+pname+'" /></p>');
	$j('#editportform_'+pid+' .field_content_edit').append('<p><span>Location:</span> <input type="text" name="portloc" id="portloc" value="'+ploc+'" /></p>');
	$j('#editportform_'+pid+' .field_content_edit').append('<p><span>Description:</span> <textarea name="portdesc" id="portdesc">'+pdesc+'</textarea></p>');
	$j('#editportform_'+pid+' .field_content_edit').append('<input type="hidden" name="portID" value="'+pid+'" />');
	$j('#editportform_'+pid+' .field_content_edit').append('<input type="hidden" name="portuid" value="'+puid+'" />');
	$j('#editportform_'+pid+' .field_content_edit').append('<input type="submit" name="CA_edit_port" value="Update changes" /><input type="button" value="Cancel" onclick="cancel_edit_banner(\'editportform_'+pid+'\',\''+pid+'\');" />');
	
	//$j('#editportform_'+pid).append(' <input type="button" value="Cancel" onclick="cancel_edit_banner(\'editportform_'+pid+'\',\''+pid+'\');" />');
	//$j('#editportform_'+pid+' input[type=button]').wrap('<div class="field_content_submit"></div>');
}
function empty_obj(obj){
	$j('#'+obj).empty();
}

function remove_obj(id){
	$j('#'+id).remove();
}


// --change class --
function change_class(obj, clas){
	if(document.getElementById){
	   var el = document.getElementById(obj);
	   el.className = clas;
   }
}

function cleaners(value_text){
	var value_text = value_text.replace(/&/g, '$$_and_$$');
	value_text = value_text.replace(/#/g, '$$_sharp_$$');	
	value_text = value_text.replace(/\n/g, '<br>');
	value_text = value_text.replace(/\//g, '$$_slash_$$');
	value_text = value_text.replace(/"/g, '$$_dquote_$$');
	value_text = value_text.replace(/'/g, '$$_quote_$$');
	//value_text = value_text.replace(/</g, '$$_uno_$$');
	//value_text = value_text.replace(/>/g, '$$_dos_$$');
	
	return value_text;
}


/* --- Banner slideshow --- */
function slideShow() {


	//Set the opacity of all images to 0
	$j('#gallery a').css({opacity: 0.0});
	
	//Get the first image and display it (set it to full opacity)
	$j('#gallery a:first').css({opacity: 1.0});
	
	//Set the caption background to semi-transparent
	$j('#gallery .caption').css({opacity: 0.5});

	//Resize the width of the caption according to the image width
	$j('#gallery .caption').css({width: $j('#gallery a').find('img').css('width')});
	
	//Get the caption of the first image from REL attribute and display it
	/*$j('#gallery .content').html($j('#gallery a:first').find('img').attr('rel'))
	.animate({opacity: 0.9}, 400);*/
	$j('#gallery .content').html($j('#gallery a:first').find('img').attr('rel'))
	.animate({opacity: 1.0}, 400);
	
	//Call the gallery function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('gallery()',6000);
	
}

function gallery() {
	
	//if no IMGs have the show class, grab the first image
	var current = ($j('#gallery a.show')?  $j('#gallery a.show') : $j('#gallery a:first'));

	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('caption'))? $j('#gallery a:first') :current.next()) : $j('#gallery a:first'));	
	
	//Get next image caption
	var caption = next.find('img').attr('rel');	
	
	//Set the fade in effect for the next image, show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
	
	//Set the opacity to 0 and height to 1px
	$j('#gallery .caption').animate({opacity: 0.5}, { queue:true, duration:100 });	
	
	//Animate the caption, opacity to 0.7 and heigth to 100px, a slide up effect
	//$j('#gallery .caption').animate({opacity: 0.5},100 )
	
	//Display the content
	$j('#gallery .content').html(caption);
	
	
	/*
	------------------------------------------------------------
	//if no IMGs have the show class, grab the first image
	var current = ($j('#gallery a.show')?  $j('#gallery a.show') : $j('#gallery a:first'));

	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('caption'))? $j('#gallery a:first') :current.next()) : $j('#gallery a:first'));	
	
	//Get next image caption
	var caption = next.find('img').attr('rel');	
	
	//Set the fade in effect for the next image, show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
	
	//Set the opacity to 0 and height to 1px
	//$j('#gallery .caption').animate({opacity: 0.0}, { queue:false, duration:0 }).animate({height: '1px'}, { queue:true, duration:300 });	
	
	//Animate the caption, opacity to 0.7 and heigth to 100px, a slide up effect
	//$j('#gallery .caption').animate({opacity: 0.7},100 ).animate({height: '100px'},500 );
	$j('#gallery .caption').animate({opacity: 0.5},500 );
	$j('#gallery .caption .content h3').animate({opacity: 1.0},500 );
	
	
	//Display the content
	$j('#gallery .content').html(caption);*/
	
	
}



function c_EmailStat(o){	
	var email = o.val();
	var passed;
	var blnRetval, intAtSign, intDot, intComma, intSpace, intLastDot, intDomain, intStrLen;
	intAtSign=email.indexOf("@");
	intDot=email.indexOf(".",intAtSign);
	intComma=email.indexOf(",");
	intSpace=email.indexOf(" ");
	intLastDot=email.lastIndexOf(".");
	intDomain=intDot-intAtSign;
	intStrLen=email.length;
	// *** CHECK FOR BLANK EMAIL VALUE
	
	if (intAtSign == -1){
		c_default_error_box(o);
	}else if (intComma != -1){
		c_default_error_box(o);
	}else if (intSpace != -1){
		c_default_error_box(o);
	}else if ((intDot <= 2) || (intDomain <= 1)  || (intStrLen-(intLastDot+1) < 2)){
		c_default_error_box(o);
	}
};



function c_EmptyStat(o){
	if(o.val() == '' || o.val() == o.attr('title')){
		c_default_error_box(o);
	}
}

function c_default_error_box(obj){
	tea_Stat = false;
	$j(obj).css({
		//'background-color':'#4a2e1c',	
		'border':'#c77742 1px solid'	
	});
}


function c_scroll_anywhere(setheight){

	$j.fn.extend({
		autoscroll: function(options) {
			return this.each(function() {
				var $this = $j(this);
				$this.css({overflow:'hidden'});
				if(options.direction == 'horizontal') $this.mousemove(function(e) {
					var width = $this.width();
					$this.attr({ scrollLeft: ($this.attr('scrollWidth')-width)*(0.5-Math.cos(Math.PI*(e.pageX-$this.offset().left)/width)/2) });
				});
				else if(options.direction == 'vertical') $this.mousemove(function(e) {
					var height = $this.height();
					$this.attr({ scrollTop: ($this.attr('scrollHeight')-height)*(0.5-Math.cos(Math.PI*(e.pageY-$this.offset().top)/height)/2) });
				});
				else $this.mousemove(function(e) {
					var width = $this.width(), height = $this.height();
					$this.attr({ scrollLeft: ($this.attr('scrollWidth')-width)*(0.5-Math.cos(Math.PI*(e.pageX-$this.offset().left)/width)/2), scrollTop: ($this.attr('scrollHeight')-height)*(0.5-Math.cos(Math.PI*(e.pageY-$this.offset().top)/height)/2) });
				});
			});
		}
	});
	$j('html, body').animate({scrollTop: setheight}, "fast", 'linear');
}


function c_contact_us(){
	//alert(tea_Fullpat+'='+tea_currPath);
	tea_load_defaults($j('#contact_us_form input[type=text], #contact_us_form textarea'));
	$j('#contact_us_form').unbind('submit').submit(function(){
		var dForm = $j(this);
		tea_Stat = true;
		
		$j('#contact_us_form input[type=text], #contact_us_form textarea').css({
			'background-color':'#ffffff',	
			'background-image':'',
			'border':'#abadb3 1px solid'	
		});
		
		c_EmptyStat($j('#contact_us_form input[name=fname]'));
		c_EmailStat($j('#contact_us_form input[name=femail]'));
		c_EmptyStat($j('#contact_us_form input[name=fzip]'));
		c_EmptyStat($j('#contact_us_form textarea[name=fcomment]'));
		c_EmptyStat($j('#contact_us_form input[name=fvalidate]'));
		
		
		if(tea_Stat == true){
		
			if($j('#contact_us_form input[name=disclaimer]').attr('checked') != 'checked'){
				
				$j("#tea_dialog").html('Make sure that you have red and understand the disclaimer by checking the checkbox.');
				$j("#tea_dialog:ui-dialog").dialog( "destroy" );
				$j("#tea_dialog").attr('title', 'Notification');
				$j("#tea_dialog").dialog({
					resizable: false,
					modal: true,
					buttons: {
						"Okay" : function() {		
							$j( this ).dialog( "close" );
						}
					}
				});
			}else{
				$j('#contact_us_form .preloader').css({'visibility':'visible'});
				
				query='c_action=c_contact_us_check&captcha='+$j('#contact_us_form input[name=fvalidate]').val();		
				$j.get(tea_Fullpat, query, function(data){	
					if(data == 'success'){
						urlu=tea_currPath+'?c_action=c_contact_us';	
						$j.ajax({
						  type: 'POST',
						  url: urlu,
						  cache: false,
						  data: dForm.serialize(),
						  success: function(data){
							$j('#contact_us_form .preloader').css({'visibility':'hidden'});
							notificationCount++;
							dForm.prepend('<div class="alert_notification" id="alert_notification_'+notificationCount+'">'+data+'</div>');	
							
							$j('#contact_us_form input[type=text], #contact_us_form textarea').val('');
							$j('#contact_us_form select').val('DID NOT RESPOND');
							$j('#contact_us_form input[name=disclaimer], #contact_us_form input[name=femail_contact], #contact_us_form input[name=fphone_contact]').removeAttr('checked');
							
							setTimeout(function(){
								$j('#alert_notification_'+notificationCount).animate({
									opacity: 0
								}, 3000, function(){
									$j(this).remove();	
								});	
							}, 4000);
							c_scroll_anywhere(970);
							$j('.validation_refresh_icon').click();	
						  }
						});
					}else{
						$j('#contact_us_form .preloader').css({'visibility':'hidden'});
						$j("#tea_dialog").html('Validation code is not correct.');
						$j("#tea_dialog:ui-dialog").dialog( "destroy" );
						$j("#tea_dialog").attr('title', 'Notification');
						$j("#tea_dialog").dialog({
							resizable: false,
							modal: true,
							buttons: {
								"Okay" : function() {		
									$j( this ).dialog( "close" );
								}
							}
						});
					}
				});
			}
		}else{
			c_scroll_anywhere(970);	
		}
		
		return false;
	
	});
	
	$j('.validation_refresh_icon').unbind('click').click(function(){
		var ic = $j(this);
		var s = $j('.captcha_creator_content img').attr('src');
		s = s.split('=');
		
		var icm = ic.attr('src');
		icm = icm.substring(0, (icm.length - 3));
		
		ic.attr('src', icm+'gif');
		
		query='c_action=captcha_creator_content&co='+(parseInt(s[2])+1);	
		$j.get(tea_Fullpat, query, function(data){	
			$j('.captcha_creator_content').css({opacity:0});
			$j('.captcha_creator_content').html(data);
			c_contact_us();
			$j('.captcha_creator_content').animate({opacity:1},"slow",function(){
				ic.attr('src', icm+'png');
			});
		});
	});
}

function c_default_error_box(obj){
	tea_Stat = false;
	$j(obj).css({
		'background-color':'#ffece5',	
		'border':'#be0108 3px solid',
		'background-image':'url('+get_bloginfo['template_url']+'images/fields_error.png)',
		'background-repeat':'no-repeat',
		'background-position':'right'	
	});
}




function tea_load_defaults(b){
	$j(b).each(function(){									   
		var v = $j(this);
		v.focus(function(){
			if(v.val() == v.attr('title')){
				v.val('');		
			}
		});
		v.blur(function(){
			if(v.val() == ''){
				v.val(v.attr('title'));
			}else if(v.val() == v.attr('title')){
				v.val(v.attr('title'));
			}
		});		
		
	});	
}


function remove_select() {
	/*Removing Settings page in any menu*/
	 $j('li a').each(function(){
	  var s = $j(this);
	  var settings = s.html();
	  if(settings == 'settings'){
	   s.parent().css('display', 'none');
	  }
	 });
}

