// FireBug Proection
try { console.log('init console...'); } catch(e) { console = { log: function() {} } }

// Onload
jQuery(document).ready(function() {
	if($('.page-home').length > 0) {
		init_home();
	}
	if($('#p-image').length > 0) {
		init_product_image_switcher();
	}
	if($('#freeform').length > 0) {
		init_freeform_form();
	}
	if($('#cm-form-wrap').length > 0) {
		init_cm_form_validation();
	}
	console.log('WHAT');
	if($('#print-email').length > 0) {
		init_range_print();
	}
	$('#product-search').click( function () {
		if($(this).val() == 'Product Search') {
			$(this).val('');
		}
	});
	
});

/* ***************************************************************************************************************************
	HOME Functions
*/

function init_home() {
	$('#home-hero-images').cycle({ 
	    fx:     'fade', 
	    speed:  'fast', 
	    timeout: 3000, 
	    next:   '#hero-next', 
	    prev:   '#hero-prev' 
	});
}

/* ***************************************************************************************************************************
	RANGE Functions
*/

function init_product_image_switcher() {
	
	$('#p-image').cycle({ 
	    fx:     'fade', 
	    speed:  'fast', 
	    timeout: 0, 
	    pager:  '#p-image-nav', 
	    pagerAnchorBuilder: function(idx, slide) { 
	        // return selector string for existing anchor 
	        return '#p-image-nav li:eq(' + idx + ') a'; 
	    } 
	});
	
	// if there is only one thumbnail. return false onclick
	if ($('#p-image-nav a').length < 2) {
		$('#p-image-nav a').click( function () {
			return false;
		});
	}
}

function init_range_print() {
	
	//$('#print-email').show();
	
	$('#btn-print').click( function () {
		window.print();
		return false;
	});
	
}

/* ***************************************************************************************************************************
	FORMS validation (client side) :(
*/

function init_freeform_form() {
	$("#freeform").validate();
	
	$('#freeform-submit').click( function () {
		if ($("#freeform").valid()) {
			ajaxCampaignMonitor();
		}
		return false;
	});
}

function init_cm_form_validation() {
	$("#cm-form").validate();
}

/* ***************************************************************************************************************************
	Submit Via ajax to CM then submit regular form
*/

function ajaxCampaignMonitor() {
	
	formAction = $("#cm-form").attr("action");
	
	//set the values from the ee form
	$('#cm-name').val($('#firstname').val() + ' ' + $('#lastname').val());
	$('#hlijjl-hlijjl').val($('#email').val());
	$('#cm-salutation').val($('#salutation').val());
	
	// Serialize form values to be submitted with POST
	var str = $("form#cm-form").serialize();
	
	// Add form action to end of serialized data
	final = str + "&action=" + formAction;
	
	//disable submit 
	$('#freeform-submit').attr('disabled', 'disabled');
	
	//fade form
	$("#freeform").animate({
	  "opacity": 0.25
	}, "fast");
	
	//show loader
	$('#loader').show();
	
	//submit via ajax to CM
	$.ajax({
		url: "/campaign-monitor/proxy.php",
		type: "POST",
		data: final,
		success: function(html){
			$("#freeform").submit();
		}
	});
	
}









