/**
 * Some onload stuff
 */
jQuery(document).ready(function() {
	
	jQuery("a[rel='external']").attr("target", "_blank");
	
	if(jQuery('#productgroep').size()){
		jQuery("#producttype").chained("#productgroep"); 
		jQuery("#product").chained("#producttype"); 
		
		jQuery("input#button_product").attr('disabled', 'disabled');
		
		jQuery("select#product").change(function () {
			jQuery("input#button_product").removeAttr('disabled');
		});
		
		
		jQuery("input#button_product").click(function () {
			var form_url = jQuery("select#product").val();
			
			if(form_url == ""){
			return false;
			}
			
			jQuery("form#form1").attr("action",form_url);
			//submit the form
			jQuery("form#form1").submit();
		}); 
		
		
		
		
		
		
	}
	
	/* http://malsup.com/jquery/cycle/goto.html */

	  if(jQuery('#galleryimage').size()){
			jQuery(".wrapper-gallery .image").click(function() {
				var image = $(this).attr("rel");
				jQuery('#galleryimage').hide();
				jQuery('#galleryimage').fadeIn('slow');
				jQuery('#galleryimage').html('<img src="' + image + '"/>');
				return false;
			});
	}

	// Contactform validation
	if(jQuery('form#contactform').size()){
		jQuery("form#contactform").validate({
			rules: {
				name: { required: true },
				city: { required: true },
				email: { required: true, email: true }
			},
			messages: {
				name: " ",
				city: " ",
				email: " "
			},
			unhighlight: validateUnhighlight,
			errorPlacement: validateErrorPlacement
		});
	}

	if(jQuery('#goto1').size()){
		jQuery('.tab-container').cycle({ 
			timeout: 0, 
			speed:   600, 
			startingSlide: 0
		});
		
		jQuery('#goto1').hover(function() {
				jQuery('.tab-container').cycle(1);
				return false;  
			},function() {
				jQuery('.tab-container').cycle(0);
				return false; 
			}
		);
	
		jQuery('#goto2').hover(function() { 
				jQuery('.tab-container').cycle(2);
				return false;  
			},function() {
				jQuery('.tab-container').cycle(0);
				return false;  
			}
		);
	}
	
	if(jQuery('#tabs').size()){
		jQuery('#tabs').tabs({
			select: function(event, ui) {
				if(ui.index == 0) { window.location.hash = ''; }
				else { window.location.hash = ui.tab.hash; }
			}
		});
		
		
		var $quicklinks = jQuery('#tabs').tabs(); // first tab selected

		jQuery('#tab-ql-modellen').click(function() { // bind click event to link
			$quicklinks.tabs('select', 0); // switch to third tab
			return false;
		});
		jQuery('#tab-ql-highlights').click(function() { // bind click event to link
			$quicklinks.tabs('select', 1); // switch to third tab
			return false;
		});
		jQuery('#tab-ql-specificaties').click(function() { // bind click event to link
			$quicklinks.tabs('select', 2); // switch to third tab
			return false;
		});
		/* special */
		jQuery('div.tab-ql-specificaties').click(function() { // bind click event to link	
			jQuery('div.container_model_data').find('div.active').removeClass("active");
			jQuery('div.container_model_data').find('div.' + jQuery(this).attr("id")).addClass("active");
			
			
			//alert(jQuery(this).attr("rel"));'div.model_tech_data:eq(' + jQuery(this).attr("rel") +')'
			
			var newLock = (jQuery(this).attr("rel") - 1) * 120; 
			
			//alert("{top:'0', left:'+=" + newLock + "'}");
			
			jQuery('div.scrolltainer').scrollTo({top:'0', left:'+=' + newLock + ''}, 800 );
			
			$quicklinks.tabs('select', 2); // switch to third tab
			return false;
		});
			
		jQuery('#tab-ql-afbeeldingen').click(function() { // bind click event to link
			$quicklinks.tabs('select', 3); // switch to third tab
			return false;
		});
		jQuery('#tab-ql-video').click(function() { // bind click event to link
			$quicklinks.tabs('select', 4); // switch to third tab
			return false;
		});
		jQuery('#tab-ql-downloads').click(function() { // bind click event to link
			$quicklinks.tabs('select', 5); // switch to third tab
			return false;
		});
	}
	
	jQuery('.quicklinks a, #tab-ql-highlights').click(function() {
		$.scrollTo( '#tabs', 800 );
	});
	
	jQuery('#searchtag').watermark('Zoekterm');
});

jQuery(window).load(function() {
	equalHeight(jQuery(".blc-link.product"));
	equalHeight(jQuery(".col-content .block-row h6"));
	equalHeight(jQuery(".eq"));
	equalHeight(jQuery(".wrapper-product-group .product-group"));
	
	equalHeight(jQuery(".product-group span.h3"));
	
	if(jQuery('#tabs').size()){
		numOftModels(jQuery('div.model_holder'),jQuery('div.container_model_data'));
	}
	
});


function numOftModels(elmnt,elment){
	
	var NumDivs = elmnt.find('div.model_tech_data').length;
	//var nTr = elmnt.find('tr').length;
	var counta = (NumDivs * 140) + 130;
	var countb = NumDivs * 125;
	
	//elmnt.css('width',counta + 'px');
	elment.css('width',countb + 'px');
}


function equalHeight(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = jQuery(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
} 

/**
 * Validate methods to place the errors in the TD behind the TD of the input field
 */
function validateUnhighlight(element, errorClass, validClass) {
	jQuery(element).removeClass(errorClass).addClass(validClass);
	jQuery(element).parent().next().addClass(validClass);
}

function validateErrorPlacement(error, element) {
	// group checks
	if(element.hasClass("group-check")) {
		element.parent("td").next("td").html('');
	}
	
	error.appendTo( element.parent("td").next("td") );
}
