






$(document).ready(function() {
	initTracking();
	initIEBugfixes();
	initBallMouseovers();
	initCarousels();
	initOrderForm1();
	initOrderForm2();
});

function openForeignLinksExternally() {
    var patternInternal = new RegExp('^(http://' + location.hostname + '|/|mailto:|#)');
    $('a').each(function() {
        if( !patternInternal.test( $(this).attr('href') ) ) {
            $(this).attr('target', '_blank');
        }
    });
}

function initTracking() {

	if( typeof pageTracker == 'undefined' ) {
		return;
	}

	var patternExternal = new RegExp('^(https?://)(?!' + location.hostname + ')', 'g');
	var extensions = new Array('doc','eps','jpg','png','svg','xls','ppt','pdf','xls','zip','txt','vsd','vxd','js','css','rar','exe','wma','mov','avi','wmv','mp3');

	$("a").bind('click', function(e) {
		var currentUrl = $(this).attr('href');
		var currentExtension = currentUrl.split('.').pop().toLowerCase();

		if( patternExternal.test(currentUrl) ) {
			// track outgoing links
			//util.log( '/extern/'+ currentUrl );
			pageTracker._trackPageview('/extern/'+ currentUrl);

		} else if( currentUrl.indexOf('mailto:') != -1 ) {
			// track emails
			var email = currentUrl.substring( currentUrl.indexOf(':')+1 );
			//util.log( '/email/'+ email );
			pageTracker._trackPageview('/email/'+ email);

		} else if( $.inArray(currentExtension, extensions) != -1 ) {
			// track downloads
			//util.log( '/download/'+ currentUrl );
			pageTracker._trackPageview('/download/'+ currentUrl);
		}
	});
}

function initBallMouseovers() {
	if( $('#balls').length == 0 ) return false;
	
	$('#balls li a').hover(
		function() {
			$('span', this).show();
		},
		function() {
			$('span', this).hide();
		}	
	);
}

function initIEBugfixes() {
	if($.browser.msie) {
	}
}

function truncate(text, length, postfix) {

	length = (typeof length != undefined && length > 0) ? length : 100;
	postfix = (typeof postfix != undefined && postfix.length > 0) ? postfix : '[..]';
	
	if( text.length < length ) return text;
	
	text = text.substring(0, len);
	text = text.replace(/\w+$/, '');
	
	return text + postfix;
}

var countOverlays = 0;

function showOverlay(id, img) {
	if( $.browser.msie && $.browser.version <= 6 ) {
		if( document.body.clientHeight > document.documentElement.clientHeight ) {
			document.getElementById('overlay').style.height = document.body.clientHeight;
		} else {
			document.getElementById('overlay').style.height = document.documentElement.clientHeight;
		}

		if( document.body.clientWidth > document.documentElement.clientWidth ) {
			document.getElementById('overlay').style.width = document.body.clientWidth;
		} else {
			document.getElementById('overlay').style.width = document.documentElement.clientWidth;
		}
	}
	//Show the background overlay and topbox...
	document.getElementById('overlay').style.visibility = 'visible';
	
	if( typeof img != 'undefined' ) {
		$('.overlay-content#ball img').attr('src', img.href);
	}
	
	document.getElementById(id).style.display = 'block';

	verticallyCenter(id);
	countOverlays++;
}

function hideOverlay(id) {
	if( countOverlays == 1 ) {
		document.getElementById('overlay').style.visibility = 'hidden';
	}

	document.getElementById(id).style.display = 'none';
	countOverlays--;
}

function viewportSize(side) {
	var viewportWidth;
	var viewportHeight;

	// for standards compliant browsers (mozilla/netscape/opera/IE7)
	if (typeof window.innerWidth != 'undefined') {
		viewportWidth = window.innerWidth,
		viewportHeight = window.innerHeight
	}
	// IE6 in standards compliant mode (i.e. with a valid doctype)
	else if( typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0 ) {
		viewportWidth = document.documentElement.clientWidth,
		viewportHeight = document.documentElement.clientHeight
	}
	// older versions of IE
	else {
		viewportWidth = document.getElementsByTagName('body')[0].clientWidth,
		viewportHeight = document.getElementsByTagName('body')[0].clientHeight
	}

	return (side == 'width' ? viewportWidth : viewportHeight);
}

// center all visible overlays when browser window is resized
window.onresize = function() {
	$('.overlay-content:visible').each(function(elm) {
		verticallyCenter( $(this).attr('id') )
	})
};

function verticallyCenter(id) {
	var elementHeight=document.getElementById(id).offsetHeight;
	var elementWidth=document.getElementById(id).offsetWidth;

	document.getElementById(id).style.top = (viewportSize('height')-elementHeight)/2+"px";
	document.getElementById(id).style.left = (viewportSize('width')-elementWidth)/2+"px";
}

// custom callback for handling clicks on custom up/down buttons for TV slider
function customButtons(carousel) {
    $('#slider-next').bind('click', function() {
        carousel.next();
        return false;
    });

    $('#slider-prev').bind('click', function() {
        carousel.prev();
        return false;
    });
};

function my_itemVisibleInCallback(carousel, item, index, state) {
	index = (index < 10) ? "0"+index : index;
	$('#slider-current').text( index );
}

function initCarousels() {
	$('.jcarousel-skin-wm').jcarousel({
		scroll: 1,
		initCallback: customButtons,
		buttonNextHTML: null,
		buttonPrevHTML: null,
		animation: 1000,
		itemVisibleInCallback: my_itemVisibleInCallback
	});
	
	$('#slider-max').text( $('.jcarousel-skin-wm li').length );
}

function initOrderForm1() {
	if( $('#order-step1').length == 0 ) return;

	updateShipping();

	$('#qty-edition').keyup(function() {
		if( $(this).val() > 0 ) {
			updateShipping()
		}
	});

	$('#qty-edition').blur(function() {
		if( $(this).val() == 0 ) $(this).val(1);
	});

	$('#shipping-country').change(function() {
		updateShipping();
	});
}

function initOrderForm2() {
	if( $('#order-step2').length == 0 ) return;

	$('#different_shipping_address').click(function() {
		if( $(this).attr('checked') ) {
			$('#shipping-personal').show();
		} else {
			$('#shipping-personal').hide();
		}
	});

	/*
	$('#payment_advance').click(function() {
		$('#discount-text').show();
	});

	$('#payment_paypal').click(function() {
		$('#discount-text').hide();
	});
	*/
}

function updateShipping() {
	var qtyEdition = $('#qty-edition').val();
	var country = $('#shipping-country').val();

	if( country == 'DE' ) {
		$('#price-shipping').text('0,00 Euro');
		return;
	}

	if( (typeof porto[country] != 'undefined') && (typeof porto[country][qtyEdition] != 'undefined') ) {
		$('#price-shipping').text(porto[country][qtyEdition] + ',00 Euro');
	} else {
		$('#price-shipping').html('bitte <a href="mailto:bestellung@dieweltmeisterschaftsbaelle.de?subject=Versandkostenanfrage">anfragen</a>');
	}
}


  /* BLANKWIN
  /* written by Alen Grakalic, provided by Css Globe (cssglobe.com)
  /* http://cssglobe.com/post/1281/open-external-links-in-new-window-automatically/
  ------------------------------------------------------------------------------*/

  this.blankwin = function(){
  	var hostname = window.location.hostname;
  	hostname = hostname.replace("www.","").toLowerCase();
  	var a = document.getElementsByTagName("a");
  	this.check = function(obj){
  		var href = obj.href.toLowerCase();
  		return (href.indexOf("http://")!=-1 && href.indexOf(hostname)==-1) ? true : false;
  	};
  	this.set = function(obj){
  		obj.target = "_blank";
  		//obj.className = "";
  	};
  	for (var i=0;i<a.length;i++){
  		if(check(a[i])) set(a[i]);
  	};
  };



  // script initiates on page load.

  this.addEvent = function(obj,type,fn){
  	if(obj.attachEvent){
  		obj['e'+type+fn] = fn;
  		obj[type+fn] = function(){obj['e'+type+fn](window.event );}
  		obj.attachEvent('on'+type, obj[type+fn]);
  	} else {
  		obj.addEventListener(type,fn,false);
  	};
  };
  addEvent(window,"load",blankwin);
