var TD = {
	
	checkCategories: function() {
			
		if(jQuery('#categories:visible').length) {
			
			jQuery('#wrapper-search .col2 .show').hide();
			jQuery('#wrapper-search .col2 .hide').show();

		} else {
			
			jQuery('#wrapper-search .col2 .show').show();
			jQuery('#wrapper-search .col2 .hide').hide();

		}
		
	},
	
	flashSummary: function() {
		
		jQuery('div.summary').animate({'backgroundColor':'#76B542','color':'#fff'},900, function(){setTimeout("jQuery('div.summary').animate({'backgroundColor':'#D6E0FD','color':'#000'},900);", 2000);});

	},
	
	getPredefinedDimensions: function(type) {
		
		// is type correct?
		if(type == 'aluminium') {
			
			// define some vars
			var dropdown_x = 'select[name="width_predefined"]';
			var dropdown_y = 'select[name="height_predefined"]';
			var value = (jQuery(dropdown_x+' option').length) ? jQuery(dropdown_x).attr('value') : 0; // 0 = initial
			var parameters = new Array();
			
			// define params
			parameters[parameters.length] = 'action=get_predefined_dimensions';
			parameters[parameters.length] = 'type='+type,
			parameters[parameters.length] = 'value='+value;
			parameters[parameters.length] = 'selected='+((jQuery(dropdown_y+' option').length) ? jQuery(dropdown_y).attr('value') : 0);
			
			// get new dimensions through ajax
			jQuery.ajax({
				type: 'GET',
				url: '/assets/bin/services.php',
				data: parameters.join('&'),
				success: function(html){
					
					// initial value?
					if(!value) { jQuery(dropdown_x).html(html); TD.getPredefinedDimensions(type); }
					else { jQuery(dropdown_y).html(html); TD.getPrice({type:type+'-predefined'}); }
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {}
			});
			
		}
		
	},
	
	getPrice: function(obj) {
			
		// define some vars
		var type = obj.type;
		var source = (obj.source!=undefined)?obj.source:'x';
		var validate = (obj.validate!=undefined)?obj.validate:false;
		var x = 0;
		var y = 0;
		var parameters = [];
		var passed = true;
		var regex_x_y = /^\d+$/;

		// fix type for aluminium?
		if((type == 'aluminium-predefined') && (jQuery('input[name="dimensions_type"]:checked').val() == 1)) type = 2;
		
		// what type?
		if(type == 'aluminium-predefined') {

			if((jQuery('select[name="width_predefined"] option').length) && (jQuery('select[name="height_predefined"] option').length)) {

				x = jQuery('select[name="width_predefined"]').attr('value');
				y = jQuery('select[name="height_predefined"]').attr('value');

			}

		} else {

			if((jQuery('input[name="width"]').length) && (jQuery('input[name="height"]').length)) {

				x = jQuery.trim(jQuery('input[name="width"]').attr('value'));
				y = jQuery.trim(jQuery('input[name="height"]').attr('value'));

			}
		}

		// do we have correct values?
		if((regex_x_y.test(x)) && (regex_x_y.test(y))) {
			
			// first check dimensions if they are correct
			// define params
			parameters.push('action=check_dimensions');
			parameters.push('type='+type),
			parameters.push('x='+x);
			parameters.push('x_correct='+jQuery('input[name="width_correct"]').attr('value'));
			parameters.push('y='+y);
			parameters.push('y_correct='+jQuery('input[name="height_correct"]').attr('value'));
			parameters.push('source='+source);
			
			// get new dimensions through ajax
			jQuery.ajax({
				type: 'GET',
				async: false,
				url: '/assets/bin/services.php',
				data: parameters.join('&'),
				dataType: 'json',
				success: function(data) {

					// set passed variable
					jQuery('input[name="passed"]').attr('value',data['passed']);

					// alert user?
					if((validate) && (!data['passed'])) alert(data['message']);

					// set values
					jQuery('input[name="width"]').attr('value', data['x_original']);
					jQuery('input[name="width_correct"]').attr('value', data['x_original']);
					jQuery('input[name="height"]').attr('value', data['y_original']);
					jQuery('input[name="height_correct"]').attr('value', data['y_original']);
					
					// change links in tabs
					jQuery('ul#doek_types a').each(function() {
						
						// do we already have width in our href?
						if(/width=\d+/.test(jQuery(this).attr('href'))) {
							
							jQuery(this).attr('href', jQuery(this).attr('href').replace(/width=\d+/, 'width='+jQuery('input[name="width"]').attr('value')));
						
						} else {
							
							jQuery(this).attr('href', jQuery(this).attr('href')+'&width='+jQuery('input[name="width"]').attr('value'));
						
						}
						
						// do we already have height in our href?
						if(/height=\d+/.test(jQuery(this).attr('href'))) {
							
							jQuery(this).attr('href', jQuery(this).attr('href').replace(/height=\d+/, 'height='+jQuery('input[name="height"]').attr('value')));
						
						} else {
							
							jQuery(this).attr('href', jQuery(this).attr('href')+'&height='+jQuery('input[name="height"]').attr('value'));
						
						}

					});

					// get price
					// define params
					parameters = [];
					parameters.push('action=get_price');
					parameters.push('product_id='+jQuery('input[name="products_id"]').attr('value')),
					parameters.push('type='+type),
					parameters.push('x='+data['x_original']);
					parameters.push('y='+data['y_original']);
					
					// get price through ajax
					jQuery.ajax({
						type: 'GET',
						url: '/assets/bin/services.php',
						data: parameters.join('&'),
						dataType: 'json',
						success: function(data){
							jQuery('.summary .value').html(data['price_formatted']);
							jQuery('input[name="product_price"]').attr('value', data['price']);
							jQuery('.summary .extra_text').html(data['extra']);
							TD.flashSummary();
						},
						error: function(XMLHttpRequest, textStatus, errorThrown) {}
					});

				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {}
			});

		} else {
			
			// reset values to the last correct ones
			if(!regex_x_y.test(x)) jQuery('input[name="width"]').attr('value', jQuery('input[name="width_correct"]').attr('value'));
			if(!regex_x_y.test(y)) jQuery('input[name="height"]').attr('value', jQuery('input[name="height_correct"]').attr('value'));
			
			// alert message
			alert('De gegevens moeten gehele getallen zijn zoals 100, 125, 150, ..\nProbeer opnieuw aub.');
		}

	},
	
	toggleCategories: function(q) {
		
		switch(q) {
			case 'hide':
				jQuery('#categories').slideUp('fast', function() { TD.checkCategories(); });
				break;
			case 'show':
				jQuery('#categories').slideDown('fast', function() { TD.checkCategories(); });
				break;
			case 'toggle':
				jQuery('#categories').slideToggle('fast', function() { TD.checkCategories(); });
				break;
		}
		
	},
	
	toggleEigenFoto: function() {

		jQuery('#eigen-foto-uploaden').slideToggle('fast');

	},
	
	toggleJohnDeWolf: function() {
		
		jQuery.fancybox({
			'hideOnContentClick':true,
			'href':'/Site/Media/wonen-met-john.html',
			'overlayColor':'#000',
			'overlayOpacity':0.8,
			'type':'iframe',
			'height':220
		});
		
	},
	
	toggleSearch: function(e) {
		
		if(e.which == 13) form.quick_find.submit();
	
	},
	
	toggleSearchInput: function(q, s) {
		
		var quick_find = 'form[name="quick_find"] input[name="keywords"]';
		if(q == 'focus') { if(jQuery.trim(jQuery(quick_find).val()) == s) jQuery(quick_find).attr('value', ''); }
		else { if(jQuery.trim(jQuery(quick_find).val()) == '') jQuery(quick_find).attr('value', s); }
	
	},
	
	toggleSubmit: function(event, type) {
		
		// when enter is pressed, get price
		if(event.keyCode==13) { TD.getPrice({type:type}); return false; }
		else return true;
		
	},

	validateForm: function(obj) {

		// first validate dimensions and price
		TD.getPrice(obj);

		// now return value of the passed field
		return (jQuery('input[name="passed"]').attr('value')=='true')?true:false;

	}
	
}

jQuery(document).ready(function() {

	// rotators
	if(jQuery('#header-left-rotator').length) jQuery('#header-left-rotator').bxSlider({auto:true,controls:false,mode:'fade',pager:false,speed:3000});
	if(jQuery('#header-right-rotator').length) jQuery('#header-right-rotator').bxSlider({auto:true,controls:false,mode:'fade',pager:false,speed:3000});
	if(jQuery('#pics-right-rotator').length) jQuery('#pics-right-rotator').bxSlider({auto:true,controls:false,mode:'fade',pager:false,speed:3000});
	
	// bind events on search field
	var quick_find = 'form[name="quick_find"] input[name="keywords"]';
	if(jQuery.trim(jQuery(quick_find).val()) == '') jQuery(quick_find).attr('value', 'Zoekwoord..');
	jQuery(quick_find).bind('focus', function() { TD.toggleSearchInput('focus', 'Zoekwoord..'); });
	jQuery(quick_find).bind('blur', function() { TD.toggleSearchInput('blur', 'Zoekwoord..'); });
	jQuery(quick_find).bind('keyup', function() { TD.toggleSearch(event); });
	
	// bind event to toggle categories
	jQuery('#toggle-categories-show').bind('mouseover', function() { TD.toggleCategories('show'); });
	jQuery('#toggle-categories-hide').bind('mouseover', function() { TD.toggleCategories('hide'); });
	
	// fancybox
	jQuery('a[rel="fancybox"]').fancybox({
		'hideOnContentClick' : true,
		'overlayColor' : '#000',
		'overlayOpacity' : 0.8
	});
	jQuery('a[rel="fb-product"]').fancybox({
		'hideOnContentClick' : true,
		'overlayColor' : '#000',
		'overlayOpacity' : 0.8,
		'showCloseButton' : false,
		'titlePosition': 'outside',
		'titleFormat': function(title) {
			var html = '';
			title = title.split('|');
			
			html += '<table width="100%" cellpadding="0" cellspacing="0">';
			html += '<tr>';
			html += '<td align="left"><button onclick="javascript:location.href=\''+title[0]+'\';" class="purple-light"><div>Kies uw ophangsysteem</div></button></td>';
			html += '<td align="right"><a href="javascript:jQuery.fancybox.close();">Sluit venster</a></td>';
			html += '</tr>';
			html += '<tr>';
			html += '<td align="left">'+title[1]+'</td>';
			html += '<td align="right">';
			html += '<a href="javascript:jQuery.fancybox.prev();"><img src="/assets/img/fancybox/fancy_nav_left.png" border="0" alt="Vorige" /></a>&nbsp;&nbsp;&nbsp;&nbsp;';
			html += '<a href="javascript:jQuery.fancybox.next();"><img src="/assets/img/fancybox/fancy_nav_right.png" border="0" alt="Volgende" /></a>';
			html += '</td>';
			html += '</tr>';
			html += '</table>';
			return html;
		},
		'onComplete': function() {
			jQuery("#fancybox-outer").css({'top':parseInt(jQuery("#fancybox-title").height() - 10)+'px'});
			jQuery("#fancybox-title").css({'position':'absolute','top':'0px','bottom':'auto','color':'#000','background-color':'#fff'});
		}
 
	}); 
	
});
