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


$(document).ready(function(){

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


	$('a.youtubin').youtubin({
	        swfWidth : 580,
	        swfHeight : 450
	});
	
	$('a.youtubin-small').youtubin({
	        swfWidth : 290,
	        swfHeight : 215
	});

	$('div#scrollbox p').marquee('pointer').mouseover(function () {
	            $(this).trigger('stop');
	        }).mouseout(function () {
	            $(this).trigger('start');
	        }).mousemove(function (event) {
	            if ($(this).data('drag') == true) {
	                this.scrollLeft = $(this).data('scrollX') + ($(this).data('x') - event.clientX);
	            }
	        }).mousedown(function (event) {
	            $(this).data('drag', true).data('x', event.clientX).data('scrollX', this.scrollLeft);
	        }).mouseup(function () {
	            $(this).data('drag', false);
	        });


	//(var randomNum = (Math.round(Math.random()*3) + 1)-1;

	//$("div#bigimageteaser ul li:eq(" + randomNum + ")").addClass("top");
/*
	$("div#smallimageteaser ul li img").click(function(){

		var list = $(this).parent();
		var nodeIndex = $("div#smallimageteaser ul li").index(list);

		$("div#bigimageteaser ul li").removeClass();
		$("div#bigimageteaser ul li:eq(" + nodeIndex + ")").addClass("top");

	});*/

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

	// 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 + 8).css("left", 50).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();
		}
	);

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

	$("#newsletteremail").clearfocus();
	$("#newslettername").clearfocus();

});

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

function orderform(){

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

  // Processing
  $( 'form.orderform' ).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.orderform' ).show().end();
}

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', commentsform, false );

function commentsform(){

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

  // Processing
  $( 'form.commentsform' ).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.commentsform' ).show().end();
}
