/**
 * @author fernando.drumond
 */

jQuery(function(){
	var shareBtn = jQuery('.dodotsharebutton').not('.no-bubble');
	
	if( shareBtn.length > 0 ) {
	 	configCookies();
	 	jQuery('.closeBtn').livequery('click', function(){
	 		jQuery(this).parent('.share_bubble').hide();
	 	});
 	}
 });

function onMaxViews() {
 	jQuery(".share_bubble").hide();
 }
 
function configCookies() {
 	var COOKIE_NAME = "dodot_interactive_share_bubble";
	var MAX_VIEWS = 10;
	var options = { path: '/', expires: 10 };
	var cookie;
	var viewCount;
	
	//get cookie
	cookie = jQuery.cookie(COOKIE_NAME);
	
	if(cookie == null) {
		viewCount = 1;
		cookie = jQuery.cookie(COOKIE_NAME, 'viewCount='+viewCount, { path: '/', expires: 10});
		posBubble();
	} else {
		viewCount = getCookieValue(cookie, "viewCount")

		if(viewCount >= MAX_VIEWS) {
			onMaxViews();
		} else {
			viewCount++;
			cookie = jQuery.cookie(COOKIE_NAME, 'viewCount='+viewCount, { path: '/', expires: 10});
			posBubble();
		}
	}
 }

function getCookieValue(cookie, name){
	var loc = cookie;
    var pos = loc.indexOf(name);
	
    if (pos < 0) {
		return null;
	}
    var len = pos+name.length+1;
    var param = loc.substring(len);
	
    if (param.indexOf('&') > 0) {
		param = param.substring(0, param.indexOf('&'));
	}
	return param;
	
 }

function posBubble(){
	var titleBuble = (dodot_lang=="es")? "&iquest;Quieres compartir este contenido?" : "Quer partilhar este conte&uacute;do?";
	var share_bubble_cnt = '<div class="share_bubble"><a class="closeBtn" href="#"></a><div class="bubble_box"><span>'+ titleBuble + '</span><ul class="share_icons"><li class="share_facebook"></li><li class="share_twitter"></li><li class="share_delicious"></li><li class="share_mail"></li></ul></div><div class="bubble_arrow"></div></div>';
	jQuery('.dodotsharebutton').not('.no-bubble').each(function(i){
		jQuery('body').append(share_bubble_cnt);
		var position = jQuery(this).offset();
		var hCnt = jQuery('.share_bubble').height();

		jQuery('.share_bubble').eq(i).css({'top':position.top-hCnt-14, 'left':position.left+0});
	});
}
