var TP = {
	
	flashSummary: function() {
		
		jQuery('div.summary').animate({'backgroundColor':'#602287','color':'#fff'},900, function(){setTimeout("jQuery('div.summary').animate({'backgroundColor':'#D6E0FD','color':'#000'},900);", 2000);});
		jQuery('div.summary strong').animate({'color':'#fff'},900, function(){setTimeout("jQuery('div.summary strong').animate({'color':'#B1009C'},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); TP.getPredefinedDimensions(type); }
					else { jQuery(dropdown_y).html(html); TP.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('.total-price .value').html(data['price_formatted']);
							jQuery('input[name="product_price"]').attr('value', data['price']);
							jQuery('.total-price .extra_text').html(data['extra']);
							TP.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.');
		}

	},
	
	toggleEigenFoto: function() {

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

	},
	
	toggleJohnDeWolf: function() {
		
		jQuery("div#john-de-wolf").hide();
		jQuery("div#john-de-wolf-film").show();
		
	},
	
	toggleSubmit: function(event, type) {
		
		// when enter is pressed, get price
		if(event.keyCode==13) { TP.getPrice({type:type}); return false; }
		else return true;
		
	},

	validateForm: function(obj) {

		// first validate dimensions and price
		TP.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-rotator ul').length) jQuery('#header-rotator ul').bxSlider({auto:true,controls:false,mode:'fade',pager:false,speed:3000});
	if(jQuery('ul#banner-rotator').length) jQuery('ul#banner-rotator').bxSlider({auto:true,controls:false,mode:'fade',pager:false,speed:3000});
	if(jQuery('ul#right-rotator').length) jQuery('ul#right-rotator').bxSlider({auto:true,controls:false,mode:'fade',pager:false,speed:3000});
	
	// 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'});
		}
 
	}); 
	
});
