jQuery.noConflict();
(function($) {
	$.updateProfileScore = function() {
		$.getJSON('/?eID=motormaatjefeuser_json', {'action':'score'}, function(data) {
			$('#login .profile-complete .progressbar').animate({'width': data.width});
			$('#login .profile-complete .percentage').html(data.percentage + '%');
		});
	};
	$(document).ready(function() {
		if (jQuery.browser.safari && document.readyState != "complete"){
			//console.info('ready...');
			setTimeout(arguments.callee, 100);
			return;
		}
		/* Update profile score */
		if ($('#login #status').length > 0) {
			setTimeout('jQuery.updateProfileScore()', 2000);
		}
		
		/* Embed Flash background */
		/*
		if ($('#background').length > 0) {
			var flashVars = {fvImageXml:"/index.php?id=6%26type=800"};
			var flashParams = {wmode: "transparent"};
			swfobject.embedSWF("/fileadmin/templates/html/flash/background.swf?v=1.4", "background", "100%", "100%", "9.0.0", "/fileadmin/templates/html/flash/expressInstall.swf", flashVars, flashParams);
		}
		*/
		/* Suggest */
		$('select#tx-srfeuserregister-pi1-tx-motormaatje-motor-brand').attr('onchange', '');
		$('select#tx-srfeuserregister-pi1-tx-motormaatje-motor-brand').bind('change', function(evt) {
			evt.preventDefault();
			evt.stopImmediatePropagation();
			$.getJSON('/?eID=motormaatje_json', {'action':'type','brand':$(this).attr('value')}, function(data) {
				var options = '';
				$.each(data, function(i, item) {
					options += '<option value="' + i + '">' + item + '</option>';
				});
				$('select#tx-srfeuserregister-pi1-tx-motormaatje-motor-type').html(options);
			});
		});
		
		/* Search box */
		if ($('#sword').length > 0) {
			var swordInitial = 'Zoeken in website';
			if ($('#sword').attr('value') == '') {
				$('#sword').attr('value', swordInitial);
			}
			$('#sword').bind('focus', function(){
				if (this.value == swordInitial) {
					this.value = '';
				}
			})
			$('#sword').bind('blur', function(){
				if (this.value == '') {
					this.value = swordInitial;
				}
			});
		}
		
		/* Body right */
		$('.csc-frame-frame1').corners('16px transparent');
		$('.csc-frame-frame2').corners('16px transparent');
		$('.news-list-image').prepend('<div class="overlay"></div>');
		var contentHeight = $('#container').height() + $('#top').height();
		if (contentHeight > $('body').height()) {
			setTimeout("jQuery('#background').css('height', jQuery('#container').height() + jQuery('#top').height())", 100);
		}
		
		/* Newsletter form */
		if ($('#newsletter').length > 0) {
			// Apply rounded corners
			$('#newsletter').corners('10px transparent');
			
			var initialValues = {'tt_address_name':'', 'tt_address_email':''};
			
			// Name & email fields
			$('input#tt_address_name,input#tt_address_email').each(function(index) {
				if (this.value == '' || this.value.match('^###')) {
					this.value = initialValues[this.id];
				}
				$(this).bind('focus', function(evt) {
					if (this.value == initialValues[this.id]) {
						this.value = '';
					}
				}).bind('blur', function() {
					if (this.value == '') {
						this.value = initialValues[this.id];
					}
				});
			});
			// Don't submit name field when value equals initial value
			$('#newsletter input[type=submit]').bind('click', function(evt){
				if ($('input#tt_address_name').attr('value') == initialValues['tt_address_name']) {
					$('input#tt_address_name').attr('value', '');
				}
			});
		}
		/* Disable some form elements on profile page */
		if ($('#tx-srfeuserregister-pi1-fe_users_form').length > 0) {
			$('#tx-srfeuserregister-pi1-tx_motormaatje_public_fields').attr('disabled', 'disabled');
		}
		/* Forum post images */
		$('.tx-mmforum-pi1-listpost-text img[alt^=http]').each(function() {
			if ($(this).width() > 750) {
				$(this).removeAttr('height');
				$(this).attr('width', '750').wrap('<a href="'+$(this).attr('src')+'" rel="external"></a>');
			}
		});
		
		/* Ambassador items */
		if ($('.ambassador-list').length > 0) {
			$('.ambassador-list div.ambassador:even').each(function() {
				var next = $(this).next('.ambassador');
				if (next) {
					maxHeight = Math.max($(this).height(), $(next).height());
					$(this).height(maxHeight);
					$(next).height(maxHeight);
				}
			});
		}
		
		$('a[href^=http://]').attr('rel', 'external');
		$('a[rel=external]').attr('target', '_blank');
		/* Search alternating rows */
		$('.tx-indexedsearch-res .res-tmpl-css:even').addClass('even');
		
		/* UI elements */
		$.datepicker.setDefaults({
			dateFormat:'dd-mm-yy',
			dayNames: ['zondag','maandag','dinsdag','woensdag','donderdag','vrijdag','zaterdag'],
			dayNamesMin: ['zo','ma','di','wo','do','vr','za'],
			dayNamesShort: ['zon','maa','din','woe','don','vri','zat'],
			monthNames: ['januari','februari','maart','april','mei','juni','juli','augustus','september','oktober','november','december'],
			monthNamesShort: ['jan','feb','mrt','apr','jun','jul','aug','sep','okt','nov','dec']
		});
	})
	/* Helper functions */
	$.strPad = function(i, l, s) {
		var o = i.toString();
		if (!s) { s = '0'; }
		while (o.length < l) {
			o = s + o;
		}
		return o;
	};
})(jQuery);
