$.fn.clearfocus = function() {
	return this.focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
};

/**
 * File Input Fix
 *
 * @author Jann Glasmachers  <glasmachers@gimmixx.de>
 * @copyright 2007 Jann Glasmachers
 */
fixFileInputSize = function (element, reachableWidth){
	if(element.style.width != 'auto'){
		element.style.width = 'auto';
	}

	while (element.offsetWidth < reachableWidth){
		element.size += 1;
	}

	element.size -= 1;
};



$(document).ready(function(){

	$("a").focus(function() {
		$(this).blur();
	});

	$.fn.superbgimage.options = {
		transition: 1,
		slideshow: 1,
		slide_interval: 15000,
		speed: 'slow'
	};

	// initialize SuperBGImage
	$('#thumbs').superbgimage().hide();

	$("#pageinformation").constantfooter();
	

	$('.jswarning').remove();

	$.get("/token/",function(txt){
	  $("form.secured").append('<input type="hidden" name="ts" value="'+txt+'" />');
	});

	// Social Bar Hover Effect
	$('#social-bar a').hover(function() {
		$(this).stop().animate({'margin-left' : '0px'}, 200);
	}, function(){
		$(this).stop().animate({'margin-left' : '-25px'}, 200);
	});

	calcTapatioOrder();

	// Social Bar Tooltips
	$("#social-bar a").hover(function(e) {
		var y = $(this).offset().top - $("#social-bar").offset().top;
		var content = $(this).attr('title');
		var id = $(this).attr('id');

		$(this).attr('title', '');

		$("#social-bar").append("<div id='tooltip'>" + content + "</div>");
		$("#social-bar #tooltip").css('opacity', 0.7).css('top', y + 20).css("left", 65).data('trigger', id).fadeIn();
	},
		function() {
			var trigger = $("#social-bar #tooltip").data('trigger');
			var content = $("#social-bar #tooltip").html();
			$('#' + trigger).attr('title', content);
			$("#social-bar #tooltip").remove();
		}
	);

});

if( document.addEventListener ) document.addEventListener( 'DOMContentLoaded', contactform, false );

function contactform(){
  // Hide forms
  $( 'form.contactform' ).hide().end();

  // Processing
  $( 'form.contactform' ).find( 'ol.blockstyle>li>label' ).not( '.nogx' ).each( function( i ){
    var labelContent = this.innerHTML;

    var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );

    var labelSpan = document.createElement( 'span' );
        labelSpan.style.display = 'block';
        labelSpan.style.width = labelWidth;
        labelSpan.innerHTML = labelContent;
    this.style.display = '-moz-inline-box';
    this.innerHTML = "";
    this.appendChild( labelSpan );
  } ).end();

  // Show forms
  $( 'form.contactform' ).show().end();
}

if( document.addEventListener ) document.addEventListener( 'DOMContentLoaded', tapatioform, false );

function tapatioform(){

  // Hide forms
  $( 'form.tapatioform' ).hide().end();

  // Processing
  $( 'form.tapatioform' ).find( 'ol.blockstyle>li>label' ).not( '.nogx' ).each( function( i ){
    var labelContent = this.innerHTML;


    var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );

    var labelSpan = document.createElement( 'span' );
        labelSpan.style.display = 'block';
        labelSpan.style.width = labelWidth;
        labelSpan.innerHTML = labelContent;
    this.style.display = '-moz-inline-box';
    this.innerHTML = "";
    this.appendChild( labelSpan );
  } ).end();

  // Show forms
  $( 'form.tapatioform' ).show().end();
}

function isInteger(s) {
return (s.toString().search(/^-?[0-9]+$/) == 0);
}

function calcTapatioOrder(){

	var factor = new Array();
	factor["qt1"] = 4.95
	factor["qt2"] = 2.69;
	factor["qt3"] = 4.95;
	factor["qt4"] = 13.25;
	factor["qt5"] = 2.00;
	factor["qt6"] = 1.50;
	factor["qt7"] = 16.99;

	var results = new Array();

	$("ol.quantity>li>select").each(function (){
		results[$(this).attr("id")] = factor[$(this).attr("id")] * $(this).val();
		var singleresult = results[$(this).attr("id")].toFixed(2);
		$("#r" + $(this).attr("id")).text( singleresult.replace(/\./, ",")  + " €");
	});

	var totalresult = 0;

	for(i in results){
		totalresult += results[i];
	}

	$("#result span#total").text( totalresult.toFixed(2).replace(/\./, ",")  + " €");
	$("#calcresult").val(totalresult);

	calcAdditional(totalresult);

	$("ol.quantity>li>select").change(function (){
		results[$(this).attr("id")] = factor[$(this).attr("id")] * $(this).val();
		var singleresult = results[$(this).attr("id")].toFixed(2);
		$("#r" + $(this).attr("id")).text( singleresult.replace(/\./, ",")  + " €");

		var tmptotalresult = 0;

		for(i in results){
			tmptotalresult += results[i];
		}

		var totalresult = tmptotalresult;

		var shipping = calcAdditional(totalresult);

		var complete = totalresult + parseFloat(shipping);


		$("#result span#total").text( totalresult.toFixed(2).replace(/\./, ",")  + " €");
		$("#result span#complete").text(complete.toFixed(2).replace(/\./, ",")  + " €");

		$("#calcresult").val(totalresult);
	});
}

function calcAdditional(totalresult) {

	var ct = new Array();
	ct["1"] = $("#qt1").val();
	ct["2"] = $("#qt2").val();
	ct["3"] = $("#qt3").val();
	ct["4"] = $("#qt4").val();
	ct["5"] = $("#qt5").val();
	ct["6"] = $("#qt6").val();
	ct["7"] = $("#qt7").val();

	if(ct["1"] == 1
	&& ct["2"] == 0
	&& ct["3"] == 0
	&& ct["4"] == 0
	&& ct["5"] == 0
	&& ct["6"] == 0
	&& ct["7"] == 0) {
		var shipping = "1,90";
	} else if(ct["1"] == 0
	&& ct["2"] == 1
	&& ct["3"] == 0
	&& ct["4"] == 0
	&& ct["5"] == 0
	&& ct["6"] == 0
	&& ct["7"] == 0) {
		var shipping = "1,90";
	} else if(ct["1"] == 0
	&& ct["2"] == 0
	&& ct["3"] == 0
	&& ct["4"] == 0
	&& ct["5"] == 0
	&& ct["6"] == 0
	&& ct["7"] == 0) {
		var shipping = "0,00";
	} else {
		var shipping = "4,35";
	}



	var mwst = totalresult - (totalresult / 1.07);

	$("#result span#mwst").text( mwst.toFixed(2).replace(/\./, ",")  + " €");
	$("#result span#shipping").text( shipping  + " €");
	$("#shippingresult").val(shipping);

	return shipping.replace(/\,/, ".");
}
