function setCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function deleteCookie(name) {
    setCookie(name,"",-1);
}
;
var $ = jQuery;

/**
 * Replace header menu links to drupal-admin-disallowed links (hash links + dynamic url's)
 */
function replace_header_menu_links() {
	var counter = 0;
	$('.region.region-header ul.menu li a').each(function(index) {
		counter++;
		switch(counter) {
			case 2:
				$(this).attr('href','/#make_money_methods');
				break;
			case 3:
				$(this).attr('href','/#compare_money_methods');
				break;
			case 4:
				$(this).attr('href','/#testimonials');
				break;
			case 5:
				$(this).attr('href','/'+MakeMoneyMethodAffPath+'/'+1);
				break;
				default:
					break;
		}

	});
}

/* Document ready function */
$(document).ready(function() {
	replace_header_menu_links();	
	$('.money_method_field_common_class.money_method_testimonial_field').first().before('<a name="testimonials"></a>');//add testimonials internal anchor within each page
	
});
/* /Document ready function */;
var default_input_full_name = null;
var default_input_email_address = null;
var default_input_phone_number = null;

/**
 * Set for default values into variables and activate focus & blur events
 */
function activate_form_default_values_events() {
	default_input_full_name=$('#webform-client-form-1 #webform-component-full-name input').val();
	default_input_email_address=$('#webform-client-form-1 #webform-component-email-address input').val();
	default_input_phone_number=$('#webform-client-form-1 #webform-component-phone input').val();
    
	$('#webform-client-form-1 #webform-component-full-name input').focus(function(){
		if($(this).val()==default_input_full_name){$(this).removeAttr("value")}}).blur(function(){if($(this).val()==''){$(this).val(default_input_full_name)}
	});
	
	$('#webform-client-form-1 #webform-component-email-address input').focus(function(){
		if($(this).val()==default_input_email_address){$(this).removeAttr("value")}}).blur(function(){if($(this).val()==''){$(this).val(default_input_email_address)}
	});
	
	$('#webform-client-form-1 #webform-component-phone input').focus(function(){
		if($(this).val()==default_input_phone_number){$(this).removeAttr("value")}}).blur(function(){if($(this).val()==''){$(this).val(default_input_phone_number)}
	});

	$("#webform-client-form-1 #edit-submit").click(function(){
		if( $('#webform-client-form-1 #webform-component-full-name input').val()==default_input_full_name ) return false;
		if( $('#webform-client-form-1 #webform-component-email-address input').val()==default_input_email_address ) return false;
		if( $('#webform-client-form-1 #webform-component-phone input').val()==default_input_phone_number ) return false;
		return true;
	});
}

$(document).ready(function() {
	if ( $('#webform-client-form-1').html() ) {
		activate_form_default_values_events();
	}
});
;
/**
 * Add bottom webform close button
 */
function add_webform_close_button() {	
	if ( $('#block-webform-client-block-1 #webform-client-form-1').html() ) {
		var div_class = '<div class="close_button_img">'+'<img src="/sites/makemoneymethods.net/themes/makemoneymethods/images/layout/makemoneymethods_bottom_close.gif" />'+'</div>';
		$('#block-webform-client-block-1 #webform-client-form-1').prepend(div_class);		
	}	
}

/**
 * Set bottom webform close cookie to prevent webform to appear for a while
 */
function set_bottom_webform_close_cookie(days) {
	if (typeof days === 'undefined') days = 1;
	setCookie('webform_bottom_block_close',true,days);
}

/**
 * Set close cookie of bottom webform if current page is webform bottom thank you page  
 */
function set_bottom_webform_close_cookie_thank_you_page() {
	var thank_you_cookie_days = 5;
	if( $('body.page-node-1').html() ) {
		set_bottom_webform_close_cookie(thank_you_cookie_days);
	}
}

/**
 * Prepare close bottom webform event
 * when webform close button is clicked webform display is fadeout + cookie is defnied to prevent webform to appear for a while
 */
function activate_close_bottom_webform_event() {
	$('#block-webform-client-block-1 .close_button_img').click(function() {
		$('#block-webform-client-block-1').fadeOut(1000);
		$('#clientsidevalidation-webform-client-form-1-errors').fadeOut(1000);
		set_bottom_webform_close_cookie();
	});
}

/**
 * Initialize bottom webform with delay after document loads
 */
function initialize_webform_open_if_necessary() {
	appearance_time = 20000;
	setTimeout('if(!getCookie("webform_bottom_block_close")) $("#block-webform-client-block-1").fadeIn(1000);',appearance_time);
}

$(document).ready(function() {		
	add_webform_close_button();	
	set_bottom_webform_close_cookie_thank_you_page();
	activate_close_bottom_webform_event();
	initialize_webform_open_if_necessary();
});;

