// JavaScript Document
jQuery(document).ready(function($){
	$('#subscribe-btn').click(function(){
		$.post(  
				"/wp-content/themes/webcircle/scripts/newsletter.php",  
				$("#subscribe").serialize(),  
				function(data){ 
					$('#subscribe').append('<p class="hidden">' + data + '</p>');
					//Appending Iframes for tracking pages doesn't seem to work anymore.
					//$('#subscribe').append('<iframe src="http://awebsitedesigner.com.au/newsletter-thankyou.html" height="1" width="1" class="noborder" style="display: none;"></iframe>');
					$('#subscribe p.hidden').fadeIn('slow');
				}
			);  
		
		return false;
	});
	
	$('#newsletter-subscription #subscribe').click(function(){
		$(this).addClass('disabled');
		$(this).attr('disbaled', 'disabled');
		$('#loading, #newsletter-subscription').removeClass('hidden');
		$.post (
				'/wp-content/themes/webcircle/scripts/newsletter.php',
				$('#newsletter-subscription').serialize(),
				function (data) {
					var response = $.parseJSON(data);
					
					// only track page views on successful submits
					if ( response.status == 'success' ) {
						_gaq.push(['_trackPageview', '/newsletter-thankyou.html']);
					}
					
					$('#newsletter-subscription').append('<p class="' + response.status + '">' + response.message + '</p>');
					$('#newsletter-subscription #subscribe').removeClass('disabled');
					$('#newsletter-subscription #subscribe').removeAttr('disabled');
					$('#newsletter-subscription #loading').addClass('hidden');
				}
			);
	});
	//add fancybox class
	$('.fancybox').fancybox({ 'titlePosition' : 'inside' });
	$('.tooltip').qtip({ 
					style : { 
						name : 'blue', 
						tip : 'bottomMiddle' ,
						width : {
							max : 400
						}
					}, hide: { effect : { length : 500 }
					}, position : { 
						corner : { 
							target: 'topMiddle', 
							tooltip : 'bottomMiddle' 
						} 
					}					
				});
	//foucs events for clearing text
	$('#first_name').focusin(function(){
		clear_text( $(this), 'Enter your name' );
	});
	
	$('#first_name').focusout(function(){
		clear_text( $(this), 'Enter your name' );
	});
	
	$('#years_actv').focusin(function(){
		clear_text( $(this), 'Enter your name' );
	});
	
	$('#years_actv').focusout(function(){
		clear_text( $(this), 'Enter your name' );
	});
	
	$('#account_name').focusin(function(){
		clear_text( $(this), "Enter your company's name" );
	});
	
	$('#account_name').focusout(function(){
		clear_text( $(this), "Enter your company's name" );
	});
	
	$('#webtolead_email1').focusin(function(){
		clear_text( $(this), 'Enter your email address' );
	});
	
	$('#webtolead_email1').focusout(function(){
		clear_text( $(this), 'Enter your email address' );
	});
		
	$('#phone_mobile').focusin(function(){
		clear_text( $(this), 'Enter your phone number' );
	});
	
	$('#phone_mobile').focusout(function(){
		clear_text( $(this), 'Enter your phone number' );
	});
	
	$('#web_add').focusin(function(){
		clear_text( $(this), 'Enter the address for your website' );
	});
	
	$('#web_add').focusout(function(){
		clear_text( $(this), 'Enter the address for your website' );
	});
	
	$('#keywords').focusin(function(){
		clear_text( $(this), 'Enter the keywords' );
	});
	
	$('#keywords').focusout(function(){
		clear_text( $(this), 'Enter the keywords' );
	});
	
	$('#initial_contact_c').focusin(function(){
		clear_text( $(this), 'Like to add anything else?' );
	});
	
	$('#initial_contact_c').focusout(function(){
		clear_text( $(this), 'Like to add anything else?' );
	});
	
	//end focus events
	
	$('form.seo-signup').submit(function(e){
		var valid 		= true;
		var el_name 	= $('#first_name');
			el_yearact 	= $('#years_actv');
			el_company	= $('#account_name');
			el_email	= $('#webtolead_email1');
			el_phone	= $('#phone_mobile');
			el_web		= $('#web_add');
			el_key		= $('#keywords');
		//remove invalid before checking again 
		$('input[type="text"]', this).removeClass('invalid');
		$('.error').remove();
		
		//check require fields
		if ( !check_element ( el_name, 'Enter your name' ) ) {
			valid = false;
		}
		
		if ( !check_element ( el_yearact, "Enter how long has your domain been active" ) ) {
			valid = false;
		}
		
		if ( !check_element ( el_email, "Enter your email address" ) ) {
			valid = false;
		}
		
		if ( !check_element ( el_phone, "Enter your phone number" ) ) {
			valid = false;
		}
		
		if ( !check_element ( el_web, "Enter the address for your website" ) ) {
			valid = false;
		}
		
		if ( !check_element ( el_key, "Enter the keywords" ) ) {
			valid = false;
		}
		
		if ( !valid ) {
			$(this).parent().append('<div class="error">Oops! You missed some required fields</div>');
		} else {
			$.post (
				'/wp-content/themes/biznizz/post.php',
				$('form.seo-signup').serialize(),
				function (data) {
					if ( data == 1 ) {
						$('form.seo-signup').parent().append('<div class="success">Thank you! Your are on your way to getting started with SEO</div>');
						_gaq.push(['_trackPageview', '/contact-thankyou.html']);
						$('form.seo-signup').slideUp('slow');
					} else {
						$('form.seo-signup').parent().append('<div class="error">' + data + '</div>');
					}
				}
			);

		}
		
		e.preventDefault();
	});
	
	$('#live-chat-top').bind('click', function(){
		// show container
		
		if ( $('.ClickDeskStatusonline:contains("online")' ) ) {
			// if live chat is online, show the box
			$('#clickdesk_container').css('display','block');
			$('#ClickdeskprechatForm').css('display','block');
			$('#ClickdeskprechatForm').css('z-index','10000');
			$('#ClickdeskofflineForm').css('display','none');
			$('#clickdesk_new_message').css('display','none');
			$('#emailChatTsToVisitorForm').css('display','none');
			$('#clickdesk_chat_session').css('display','none');
			$('#ClickDeskcontrols').css('z-index','10001');
		} else {
			$('#clickdesk_container').css('display','block');
			$('#ClickdeskprechatForm').css('display','none');
			$('#ClickdeskprechatForm').css('z-index','10000');
			$('#ClickdeskofflineForm').css('display','block');
			$('#clickdesk_new_message').css('display','none');
			$('#emailChatTsToVisitorForm').css('display','none');
			$('#clickdesk_chat_session').css('display','none');
			$('#ClickDeskcontrols').css('z-index','10001');
		}
		
		return false;
	
	});
	
});

function clear_text ( element, default_text ) {
	if ( element.val() == default_text ) {
		element.val('');
	} else if ( element.val() == '' ) {
		element.val(default_text);
	}
}

function check_element ( element, default_text ) {
	if ( element.val() == default_text || element.val() == '' ) {
		element.addClass('invalid');
		return false;
	} else {
		element.addClass('valid');
		return true;
	}
}

function newsletter_subscribe( form_id ) {
	var el = $('#' + form_id);
		clicked = false;
	if ( !clicked ) {
		$('#loading,' + form_id).removeClass('hidden');
		$('#subscribe, ' + form_id ).val('Subscribing');
		$('#subscribe, ' + form_id ).addClass('disabled');
		
		$.post (
				'/wp-content/themes/webcircle/scripts/newsletter.php',
				el.serialize(),
				function(data){
					$('#subscribe, ' + form_id).append('<p class="hidden">' + data + '</p>');
					$('p.hidden, ' + form_id).fadeIn('slow');
				}
			);
		
		clicked = true;
	}
	
	return false; //prevent form from submitting
}


