$(document).ready(function(){
	
	if(typeof DD_belatedPNG != "undefined")
		DD_belatedPNG.fix('.pngFix, .carouselPrev, .carouselNext, .productPrev, .productNext, #popup div');
});
$(window).load(function(){
	
	var radius = 18;
	
	switch($('#holder').attr('class')){
		case 'startpage':
			$("#container").liquidCanvas("[shadow fill{color:#fff}] => roundedRect{radius:"+radius+"}");
		break;
		
		case 'about':
		case 'expert':
		case 'blog':
		case 'money':
		case 'about om':
			$("#container").liquidCanvas("[shadow fill{color:#fff}] => roundedRect{radius:"+radius+"}");
			$(".leftHolder").liquidCanvas("gradient => rect");
		break;
		
		
		default:
			$("#container").liquidCanvas("[shadow gradient] => roundedRect{radius:"+radius+"}");
		break;	
	
	}

	$("#container .roundedBox").liquidCanvas("[shadow gradient] => roundedRect{radius:"+radius+"}");
	$(".gradient").liquidCanvas("gradient => rect");
	$(".mirrorGradient").liquidCanvas("mirrorGradient => rect");
	
	
	 
	
	//twitter magic...
	$('a.twitter').click(function(){
		var service_url = '/29a/';
		var pageDescription = $('head meta[name="description"]').attr('content');
		var chars = 139;
		
		$.ajax({url: service_url, 
				type: 'POST',
				data: {url:window.location.href},
				dataType: 'json',
				jsonp: '',
				success: function(data){
						var urlLenght = data.url.length;
						var charsLeft = chars - urlLenght;
						var msg = pageDescription.length <= charsLeft ? pageDescription : pageDescription.substr(0, charsLeft - 3) + '...'; 
						msg += ' '+data.url;
						openTwitterStatus(msg);
					},
				error: function(data){
						var urlLenght = window.location.href;
						var charsLeft = chars - urlLenght;
						var msg = pageDescription.length <= charsLeft ? pageDescription : pageDescription.substr(0, charsLeft - 3) + '...'; 
						msg += ' '+window.location.href;
						openTwitterStatus(msg);
					}
				});
		return false;
	});
	
	$('a.facebook').click(function(){
		var url = 'http://www.facebook.com/share.php?u=' + escape(window.location.href);
		window.open (url, "facebook","status=no,toolbar=no,width=550,height=300"); 
		return false;
	});
	
	$('#searchForm').submit(function(){
		var input = $(this).find('input').val().replace(/\s/g, "_");
		if(input.length == 0)
			return false;
		
		window.location.href = "/fraga-experten/sok/"+input+"/1";
		
		return false;
	});
	
	$(window).scroll(function(){
		$('.floater').stop();
		$('.floater').animate({
			top:$(window).scrollTop()+'px'
		},1000);
	});
	
    $(".scroll").click(function(event){
		event.preventDefault();
		var full_url = this.href;
		var parts = full_url.split("#");
		var trgt = parts[1];
		var target_offset = $("#"+trgt).offset();
		var target_top = target_offset.top;
		$('html, body').animate({scrollTop:target_top - 20}, 500);
	});
    
    $(".popup").popup();

    $('.moneyBackHeart').click(function(event) {
        // IE7 fix. The link doesn't work normally
        location.href = $(this).attr('href');
        return false;
    })
    //Heart link animation
    $('#heartContainer').mouseover(function(){
    	$('#heartContainer').stop();
    	$('#heartContainer').animate({
    		width: 120,
    		'bottom':-9,
    		'left':-10
    	}, 200);
    });
    
    $('#heartContainer').mouseout(function(){
    	$('#heartContainer').stop();
    	$('#heartContainer').animate({
    		width: 90,
    		'bottom':0,
    		'left':0
    	}, 200);
    });
});





function openTwitterStatus(msg){
	var url = "http://twitter.com/home?status="+Url.encode(msg);
	window.open (url, "twitter","status=no,toolbar=no,width=800,height=500"); 
}


(function($) {
	var animating = false;
	var popup =  '<div id="popupOverlay"></div><div id="popup"><div><iframe frameborder="0" src=""></iframe></div></div>';
	$.fn.popup = function() {
		$(this).click(function(){
			$('html, body').animate({scrollTop:0}, 500);
			animating = true;
			$('body').append(popup);
			$('#popup iframe').attr('src', $(this).attr('href'));
			$('#popup').slideDown('slow', function(){animating = false;});
			$('#popupOverlay').css('width',$(document).width()+'px');
			$('#popupOverlay').css('height',$(document).height()+'px');
			$('#popupOverlay').click(function(){
				if(animating)
					return false;
				$('#popup').slideUp('slow', function(){
					animating = false;
					$('#popup').remove();
					$('#popupOverlay').remove();
				});
				return false;
			});
			return false;
		});		
	}
})(jQuery);




/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/
 
var Url = {
 
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
 
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}
