//status vars
var startCoins = true;
var price = {
	'silber_1' 	: new Array('500', '1.000'),
	'silber_2' 	: new Array('500', '1.300'),
	'silber_3' 	: new Array('500', '1.500'),
	'gold_1'	: new Array('520', '2.000'),
	'gold_2'	: new Array('520', '2.100')
};

(function($) {

	/* this type="text" autoclear (standard value has to be in title attr) */
	$.fn.autoclear = function() {

		if(this.attr('title')){
			if(this.attr('value') == '')
				this.attr('value', this.attr('title'));

			if(this.attr('title') == this.attr('value'))
				this.addClass('isStdValue');
			
			this.focus(function(){
				var input = $(this);
				if(input.attr('title') == input.attr('value')){
					input.attr('value', '');
					input.addClass('used');
					
					if(input.hasClass('isStdValue'))
						input.removeClass('isStdValue');
				}
			});

			this.blur(function(){
				var input = $(this);
				input.attr('value', jQuery.trim(input.attr('value')));
				
				if(input.attr('value') == ''){
					input.attr('value', input.attr('title'));
					
					if(input.hasClass('used'))
						input.removeClass('used');
					
					if(!input.hasClass('isStdValue'))
						input.addClass('isStdValue');
				}
			});
		}
	};
	
	
	
	
	
	/* switches the content on home*/
	$.fn.tabChange = function () {
		if(this.length != 0){
			
			
			this.click(function () {
				var elem = $(this);
				var parentLi = $(elem.parents('li').get(0));

				if(parentLi.attr('id') == "ihreIdee")
					window.location.href = $(parentLi.find('a').get(0)).attr('href');				
				else {
					$('#teaser-tabs li').removeClass("active");				
					parentLi.addClass("active");
				}
				return false;
			});			
				
			this.mouseover(function () {$(this).addClass("hover")});
			this.mouseout(function () {$(this).removeClass("hover")});
		}
	};

	
	/* switches the coins motivs*/
	$.fn.motivSwitcher = function () {
		this.find('li a').each(function(index, el){		
			var element = $(el);
			element.click(function () {
				var li = $(this).parent();
				var liparent = li.parents('.formContent');
				liparent.attr('class', 'formContent vorlage_'+ li.attr('class'));
				liparent.find('.hidden_motiv').attr('value', li.attr('class'));
				return false;
			});			
		});		
	};

	
	
	/* switches the coins on order */
	$.fn.coinSwitch = function () {
		if(this.length != 0){
				var taleroptik = document.getElementById("talerOptik");
				
				//default value
				if(startCoins){
					taleroptik.actualColor = 'silber';
					$(taleroptik).addClass(taleroptik.actualColor);
					startCoins = false;
				}
				
				//checken ob ein radio button schon gescheckt ist
				this.each(function(index, elem){
					var radio = $($(elem).find('input'));
					if(radio.attr('checked') == true){
						var indexOfUnderscore = radio.attr('value').indexOf('_');
						var color = radio.attr('value').substr(0, indexOfUnderscore);
						$(taleroptik).removeClass(taleroptik.actualColor);
						$(taleroptik).addClass(color);
						taleroptik.actualColor = color;
						//price switchen
						$('#price_1').attr('value', price[radio.attr('value')][0] + '€');
						$('#price_2').attr('value', price[radio.attr('value')][1] + '€');
					}
				});
					
				this.click(function () {
					var taleroptik = document.getElementById('talerOptik');
					var elem = $(this);
					
					//delete all possible classes
					if(taleroptik.actualColor)
						$(taleroptik).removeClass(taleroptik.actualColor);
					
					var input = $(elem.find('input').get(0));
					var indexOfUnderscore = input.attr('value').indexOf('_');
					var color = input.attr('value').substr(0, indexOfUnderscore);
					$(taleroptik).addClass(color);
					taleroptik.actualColor = color;
					
					//price switchen
					$('#price_1').attr('value', price[input.attr('value')][0] + '€');
					$('#price_2').attr('value', price[input.attr('value')][1] + '€');
					
				});
		}
	};
	
	/* switches the content on home*/
	$.fn.createUploadify = function () {
		if(this.length != 0){
		this.uploadify({
			'uploader'	: 'uploadify/uploadify.swf',
			'script'	: 'inc/upload.php',
			'cancelImg'	: 'images/uploadify/cancel.png',
			'folder'    : 'tmp_files',
			'scriptData': {'form_id': _FORM_ID, 'type' : this.attr('id')},
			'multi'		: false,
			'auto'		: true,
			'width'		: 109,
			'height'	: 27,
			'rollover'	: true,
			'fileExt' 	: '*.ai;*.jpg;*.jpeg;*.gif;*.png;*.bmp;*.psd;*.eps;*.cdr',
			'fileDesc'	: '*.ai;*.jpg;*.jpeg;*.gif;*.png;*.bmp;*.psd;*.eps;*.cdr',
			'sizeLimit'	: 15728640, //15Mb
			'wmode'		: 'transparent',
			'buttonImg' : 'images/layout/upload-image_button.png',
			'onComplete': function(event, queueID, fileObj, response){
				if(response == "1"){	
					$('#'+ event.target.id +'_filename').attr('value', fileObj.name);
					$('#'+ event.target.id +'_filetype').attr('value', fileObj.type);
					//status-> datei hochgeladen
					$('#'+ event.target.id +'_status').html('<span class="filename">" '+ fileObj.name + ' "</span> hochgeladen');					
				}else{
					//fehler ausgeben
					$('#'+ event.target.id +'_status').html('Fehler beim Hochladen');	
				}
				
			},
			'onInit'	: function(){
				if($('#' + this.id +'_status').length == 0)
					$('#' + this.id).after('<div id="' + this.id  + '_status"></div>');			
			}
		});
		}
	};

})(jQuery); 



$('#front').createUploadify();
$('#back').createUploadify();

$('#teaser-tabs li h2 span').tabChange();
$('#selectCoinColor dd label').coinSwitch();
$('input.autoclear').each(function(index, elem){$(elem).autoclear();});
$('.motivVorlagen ul').each(function(index, elem){$(elem).motivSwitcher();});
$('.imgpreview').each(function(index, elem){$(elem).imgPreview(); $(elem).click(function(){return false;});});
	


