$(document).ready(function() 
{
	/*
	 * DEFAULT ERROR BEHAVIOUR ON BROKEN IMAGES
	 */
	$("img").error(function () {
		$(this).unbind("error").attr("src", "/media/images/products/not_found.png");
	});

	
	// Avoid non-conform documents for W3C standards.
	// This functions applies target="_blank" on all hyperlinks with the class "blank".
	$('a.blank').click(function(){
		this.target = "_blank";
	});

	// This functions applies back-button with the class "back".
	$('a.back').click(function(event){
		event.preventDefault();
		history.go(-1);
	});


	//insert the product in marked list
	var markedListStr			=	$('.toggle-wish-list').text();
	var prefListStr				=	$('.button-preference a').text();
	//messages
	var markedMessageSave		= 	'Speichern Sie das Produkt auf Ihrer Merkliste';
	var markedMessageRemove 	= 	'Löschen Sie das Produkt aus Ihrer Merkliste';
	var prefMessageSave 		=	'Fügen Sie den Shop Ihren Favoriten hinzu';
	var prefMessageRemove 		= 	'Löschen Sie den Shop aus Ihren Favoriten';
	if(markedListStr == markedMessageSave) {
		$('.toggle-wish-list').toggle(function() {
			$(this).text(markedMessageRemove).attr('title', markedMessageRemove).toggleClass('active');
		}, function() {
			$(this).text(markedMessageSave).attr('title', markedMessageSave).toggleClass('active');
		});
	} else {
		$('.toggle-wish-list').toggle(function() {
			$(this).text(markedMessageSave).attr('title', markedMessageSave).toggleClass('active');
		}, function() {
			$(this).text(markedMessageRemove).attr('title', markedMessageRemove).toggleClass('active');
		});
	}
	if(prefListStr == prefMessageSave) {
		$('.shop-search-list a').toggle(function() {
			$(this).attr('title', prefMessageRemove).toggleClass('active');
		}, function() {
			$(this).attr('title', prefMessageSave).toggleClass('active');
		});
	} else {
		$('.shop-search-list a').toggle(function() {
			$(this).attr('title', prefMessageSave).toggleClass('active');
		}, function() {
			$(this).attr('title', prefMessageRemove).toggleClass('active');
		});
	}
	if(prefListStr == prefMessageSave) {
		$('.shop-details-page a').toggle(function() {
			$(this).text(prefMessageRemove).attr('title', prefMessageRemove).toggleClass('active');
		}, function() {
			$(this).text(prefMessageSave).attr('title', prefMessageSave).toggleClass('active');
		});
	} else {
		$('.shop-details-page a').toggle(function() {
			$(this).text(prefMessageSave).attr('title', prefMessageSave).toggleClass('active');
		}, function() {
			$(this).text(prefMessageRemove).attr('title', prefMessageRemove).toggleClass('active');
		});
	}	


	/*
	 * SOCIAL COMMUNITY PLUGIN => FACEBOOK; GOOGLE+; TWITTER
	 */
	
	if (!IS_MOBILE) { 
	var permalink	 = document.location.href;
	var httpProtocol = document.location.protocol;
	
	var ic3Twitter	= '<script type="text/javascript" src="//platform.twitter.com/widgets.js"></script> <a href="' + httpProtocol + '//twitter.com/share" class="twitter-share-button" data-url="http://www.ic3.de" data-count="horizontal" data-lang="de">Twittern</a>';
	
	var ic3Google	= '<script type="text/javascript" src="https://apis.google.com/js/plusone.js">{lang: "de"}</script><g:plusone size="medium" href="http://www.ic3.de/"></g:plusone>';
	
	var ic3Facebook = '<iframe src="//www.facebook.com/plugins/like.php?locale=de_DE&href=http%3A%2F%2Fwww.facebook.com%2FIC3.de&amp;send=false&amp;layout=button_count&amp;width=150&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:150px; height:21px;" allowTransparency="true"></iframe>';

	$('#footer #copy-info').after('<div id="ilike-box" style="width: 900px; padding: 10px;">'+
		'<div id="add-facebook">'+ ic3Facebook +'</div>' +
		'<div id="add-twitter">'+ ic3Twitter +'</div>' +
		'<div id="add-google">'+ ic3Google +'</div>' +
		'<div class="clear"></div></div>'
	);
	
	$('#add-twitter, #add-facebook, #add-google').css({
		'float'		: 'left',
		marginRight : '5px'
	});
	}
	
	/*
	 * OPEN IN EXTERNAL LINK 
	 */
	$('a').click(function() {
		var urlType		= $(this).attr('rel');
		var url			= $(this).attr('href');
		
		if (typeof(urlType) != 'undefined') {
			if (urlType.match(/external/)) {
				window.open(url);
				return false;
			}
		}

	}); 
	 
	
	/*
	 * WIDGETS FOLDABLE
	 */
	$('.widget-area .widget-hidden .widget-content').hide();
	$('.widget-area .widget .widget-title span.btn').css({
		background: 'url("/media/themes/default/images/icons/toggle_plus_minus.png") no-repeat',
		width: 16,
		height: 16,
		marginRight: 5,
		right: 3,
		position: 'absolute',
		cursor: 'pointer'
	}).parent().css('cursor', 'pointer');
	$('.widget-area .widget-show .widget-title span.btn').css('background-position', '0 -16px');
	$('.widget-area .widget-show .widget-title').toggle(function(){
		$('span.btn', this).css('background-position', '0 0');
		$('span.btn', this).parents(".widget").children(".widget-content").slideToggle();
	},function(){
		$('span.btn', this).css('background-position', '0 -16px');
		$('span.btn', this).parents(".widget").children(".widget-content").slideToggle();
	});
	$('.widget-area .widget-hidden .widget-title').toggle(function(){
		$('span.btn', this).css('background-position', '0 -16px');
		$('span.btn', this).parents(".widget").children(".widget-content").slideToggle();
	},function(){
		$('span.btn', this).css('background-position', '0 0');
		$('span.btn', this).parents(".widget").children(".widget-content").slideToggle();
	});
	
});





