﻿//---------------------------------------------------------------------------------------------------------
(function(){
  if(jQuery){
    jQuery.extend({
      link:function(text){
		try {
        return text.replace(/(href="|src="|<a.*?>)?[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g, function($0, $1) {
          return $1 ? $0 : $0.link($0);
		});
		} catch(e) {}
      }
    });

    jQuery.fn.extend({
      'link':function(){
        this.html(jQuery.link(this.html()));
      }
    });
  
  }
})();

//---------------------------------------------------------------------------------------------------------
var openLink = function(link, target, popup, e)
{
	if(e)
	{
		if (window.event) {
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		} else {
			e.stopPropagation();
		}
	}
	
	if(typeof popup != "undefined" && popup.window_id && popup.features)
	{
		window.open(link, popup.window_id, popup.features); 
		self.focus();
	}
	else
	{	
		if(target == "_blank") 
		{
			window.open(link); 
		}
		else
		{
			if(typeof Mobile != "undefined")
			{
				showLoader();
			}
			
			if(link.indexOf("#") == 0) {
				if(goToAnchor(e, link))
					return;
			}

			if(target == "_top")
				window.top.location.href = link; 
			else 
				window.location.href = link;
		}
	}
};	

var initOverlayLinks = function($selector) {
	$selector = $($selector);
	
	try {
		if(!IsMobileDevice()) {   
			$selector.find(".loup").fancybox({
				'padding' : 0,
				hideOnContentClick : true,
				'overlayShow' : true,
				'zoomSpeedIn' : 0,
				'zoomSpeedOut' : 0,
				overlayColor : $(document.body).hasClass("content-style-dark") ? "#000" : "#FFF"
			});
		
			$selector.find(".overlay-link").fancybox({
				'padding' : 0,
				hideOnContentClick : false,
				'overlayShow' : false,
				'zoomSpeedIn' : 0,
				'zoomSpeedOut' : 0
			});
			
			$(".video-overlay-link").each(function() {
				$(this).fancybox({
					'padding' : 0,
					hideOnContentClick : false,
					'overlayShow' : false,
					'zoomSpeedIn' : 0,
					'zoomSpeedOut' : 0
				});
			});
		}
	} catch(e) {}
};

var initTeaser = function()
{
	$(".ce-link").hover(function() { 
		 $(this).parents(".ce-link-wrapper").find(".ce-link").each(function() {
			var $link = $(this),
				xclass = $link.attr("class").split(" ");
		
			if(xclass[0].length)
				xclass[0] += '-hovered';
			$link.addClass(xclass[0]).addClass("hovered"); 
		});
	},
	function() {
		 $(this).parents(".ce-link-wrapper").find(".ce-link").each(function() {
			var $link = $(this),
				xclass = $link.attr("class").split(" ");
			
			if(xclass[0].length)
				xclass[0] += '-hovered';
			$link.removeClass(xclass[0]).removeClass("hovered"); 
		});
	});
};

function initTweetButton(context) { 
	if(typeof twttr != "undefined")
		twttr.widgets.load();
	//$.getScript("http://platform.twitter.com/widgets.js", function() {  twttr.widgets.load(); $(".twitter-share-button").show(); });
}

function initGooglePlusOne(context) { 
	if(typeof gapi != "undefined")
		gapi.plusone.go(document);	
}

function initLinkedInShare(context) { 
	if(typeof IN != "undefined")
		try { IN.parse(document); } catch(e) {}
}

function goToAnchor(e, link) {
	var id = link.substr(Math.max(0, link.indexOf("#")), link.length);
	
	if(id.length <= 1)
		return false;
		
	if(link.indexOf("://") == -1 || link.indexOf("#") == 0) 
	{
		var baseLink = window.location.href;
		if(baseLink.indexOf("#") > -1)
			baseLink = baseLink.substr(0, baseLink.indexOf("#"));
		link = baseLink + id;
	}
		
	try {
		var $anchor = $(id);
		if($anchor.length == 0)
			return false;
			
		$($.browser.safari ? 'body' : 'html').animate({scrollTop: $anchor.offset().top}, function() { window.location.href = link; });
	} catch(e) {  
		return false;
	};
	
	return true;
}

function initAnchors() {
	$("a[href*='#']").not(".rotator-navigation a").bind("click", function(e) {
		var href = $(this).attr("href");

		if(href.indexOf("://") == -1 || href.indexOf(window.location.host) > -1) {
			if(goToAnchor(e, $(this).attr("href")))
				e.preventDefault();
		}
	});
}

$(function() {
	initTeaser();	
	initAnchors();	   
});

$(window).bind("linkedInLoad", function(e) { 
	$fancyAjax = $("#fancy_ajax"); 
	$("#fancy_content").find(".linkedin-button").each(function() {
		$(this).html($fancyAjax.next(".IN-widget, script")[0]);
	});
	
	initLinkedInShare();
});

$(window).bind("load ajaxLoad", function(e, context) {
	if(typeof context == "undefined")
		context = document;
	else
		context = $(context)[0];
		
	initOverlayLinks(context); 			
	initGooglePlusOne(context);	
	initTweetButton(context);
	initLinkedInShare(context);
	
	$(window).trigger("linkedInLoad");
});
