$(document).ready(function() {
	
	/*------ Tooltips ----------------------------*/
	$(".product img.thumb").tooltip({
		effect: 'slide',
		position: "top center", 
		offset: [190, 0],
		predelay: 100
	});

	/*----- Pages dropdown ----------------------------------------------------------------------------------*/	
	$('#pagesDropdown').change(function() {
		link = $(this).val();
		$(location).attr('href',link);
	});

	/*----- Product tabs ---------------------------------------------------------*/
	$("ul.css-tabs").tabs("div.css-panes > div", {
		effect: 'ajax',
		onBeforeClick: function(event, i) { 
			var pane = this.getPanes("div.css-panes > div"); 			
			pane.html('<img class="loader" src="assets/templates/default_tpl/images/ajax-loader.gif" />');
			pane.load(this.getTabs().eq(i).attr("href")); 		    

		} 
	});
	
	//------------ Callback form
	$('#requestCallback').fancybox({
		ajax : {
		    type	: "POST"
		},
		'hideOnOverlayClick' : false,
		'hideOnContentClick' : false,
		'transitionIn'	 	 : 'elastic',
		'transitionOut'	 	 : 'elastic',
		'autoScale'		 	 : true,
		'centerOnScroll' 	 : true,
		'showCloseButton' 	 : true,
		'overlayOpacity'	 : 0.5,
		'overlayColor'		 : '#304C96',
		'scrolling'			 : 'no' 
	});

	$("#fancyContactForm #contactForm").live('submit',function() {
		$.fancybox.showActivity();
		$.ajax({
			type		: "POST",
			cache		: false,
			url			: "index.php?id=372",
			data		: $(this).serializeArray(),
			success		: function(data) {
							$.fancybox(data);
			}
		});
		return false;
	});
	
	$('a.video').fancybox({
		'hideOnOverlayClick' : false,
		'hideOnContentClick' : false,
		'transitionIn'	 	 : 'elastic',
		'transitionOut'	 	 : 'elastic',
		'autoScale'		 	 : true,
		'centerOnScroll' 	 : true,
		'showCloseButton' 	 : true,
		'overlayOpacity'	 : 0.5,
		'overlayColor'		 : '#304C96',
		'scrolling'			 : 'no' 
	});
	
	//----------------------------- Subscribe form 
	$("#subscribeForm").validate({
		 rules: { 
	      "cm-name": { 
			  required: true,
			  checkForDefaultVale: true 
	      },
		  "cm-oihdii-oihdii": {
			  required: true,
			  email: true
		  }
	    }, 
		messages: { 
 	      "cm-name": "Please enter a your full name",
		  "cm-oihdii-oihdii": "Please enter your email address"
 	    },
		submitHandler: function(form) { sumbitForm(form.id); }
	});

	//---------------------------- Equal height for product listing boxes	
	equalHeight($('#productList div.spec'));
	
	//---------------------------- Product dropdown 
/*
	$('select#pagesDropdown').selectmenu({
		style: 'dropdown', 
		width: 200
	});
*/
	


	
});
/*------- Validation additonal methods --------------------------------------------------*/

$.validator.addMethod( "checkForDefaultVale", function(value, element) { 
	data = (element.value == "Full name") ? false : true; 
	return data;
});

function sumbitForm(formId) {
	 data = $('#'+formId).serialize();
	 formAction = $('#'+formId).attr("action");
	 url = 'assets/snippets/campaignMonitor/proxy.php';
	 final = data + "&action=" + formAction;
     $.ajax({
      type: "POST",    
      url: url,
      data: final,
      success: function(msg){
        $('#newsletter_form').html("<span class='error'>Thank you for subscribing to the Mantis Cranes newsletter.<br />You'll receive the first edition by email soon.</span>");		
      }
    });
    return false;
}

function equalHeight(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest+15);	
}


