/*  - Louis-René Haché ak dummy lrhache@hotmail.com (lrhache@motionindesign.com)*/
var tempX = 0
var tempY = 0
var innerHeight = 0;
var innerWidth  = 0;
var scrollLeft = 0;
var scrollTop = 0;
var referrer = null;
var capsule = new Array("pink","blue","orange");
var querysent = false;
function resize_main_content(){}
function jsize()
{
	var f = $(".chs");
	for(var i=0;i<f.length;i++)
	{		
		font = (f[i].style.fontSize)? parseInt(f[i].style.fontSize) + 1 : 11 ;
		f[i].style.fontSize = font +"px";
		a = f[i].getElementsByTagName("a");
			for(var n=0;n<a.length;n++)
			{		
				font = (a[n].style.fontSize)? parseInt(a[n].style.fontSize) + 1 : 11 ;
				a[n].style.fontSize = font +"px";
			}
	}
	
	var table = $(".display")[0];
	if(table)
	{	
		fonttable = (table.style.fontSize)? parseInt(table.style.fontSize) + 1 : 10 ;
		table.style.fontSize = 	fonttable +"px";
		a = table.getElementsByTagName("a");
			for(var n=0;n<a.length;n++)
			{		
				font = (a[n].style.fontSize)? parseInt(a[n].style.fontSize) + 1 : 11 ;
				a[n].style.fontSize = font +"px";
			}

	}
	resize_main_content();
}
function jjsize()
{
	var f = $(".chs");
	for(var i=0;i<f.length;i++)
	{		
		font = (f[i].style.fontSize)? parseInt(f[i].style.fontSize) - 1 : 11 ;
		f[i].style.fontSize = font +"px";
		a = f[i].getElementsByTagName("a");
			for(var n=0;n<a.length;n++)
			{		
				font = (a[n].style.fontSize)? parseInt(a[n].style.fontSize) - 1 : 11 ;
				a[n].style.fontSize = font +"px";
			}
	}
	
	var table = $(".display")[0];
	if(table)
	{	
		fonttable = (table.style.fontSize)? parseInt(table.style.fontSize) - 1 : 10 ;
		table.style.fontSize = 	fonttable +"px";
		a = table.getElementsByTagName("a");
			for(var n=0;n<a.length;n++)
			{		
				font = (a[n].style.fontSize)? parseInt(a[n].style.fontSize) - 1 : 11 ;
				a[n].style.fontSize = font +"px";
			}

	}
	resize_main_content();
}


$(function()
{
	setTimeout(function()
	{
		var orwidth = $.browser.msie ? 39 : 6;
		
		$("#help-tab").show().css({marginTop:-($("#help-tab").height()/2)+'px',width:orwidth+'px'}).fadeTo(300,1);
		if($.macos.ipad||$.macos.ipod||$.macos.iphone)
		{
			$("#help-tab").css({marginRight:'-35px'});	
		}
		
		$("#help-tab-tab").bind("click"
		  ,function()
		  {
			var cont = $("#help-tab");
			if(!cont.hasClass('open'))
				cont.addClass('open').animate({width:370+orwidth+'px'},100);
			else
				cont.removeClass('open').animate({width:orwidth+'px'},100);
			return false;
		  });
	},1000);
});

function init_document()
{
		on_window_resize();
		on_window_scroll();
		document.onmousemove = getMouseXY;
		window.onscroll = on_window_scroll;
		window.onresize = on_window_resize;
		//window.onclick 	= on_window_onclick;
		referrer = (document.referrer)? document.referrer : "" ;
}
function on_window_scroll()
{
			if (self.pageYOffset) {
				scrollTop = self.pageYOffset;
				scrollLeft = self.pageXOffset;
			} else if (document.documentElement && document.documentElement.scrollTop) {	 
				scrollTop = document.documentElement.scrollTop;
				scrollLeft = document.documentElement.scrollLeft;
			} else if (document.body) {
				scrollTop = document.body.scrollTop;
				scrollLeft = document.body.scrollLeft;	
			}
}

function on_window_resize()
{
		innerHeight 	= parseInt(document.documentElement.clientHeight);
	 	innerWidth 		= parseInt(document.documentElement.clientWidth);
}


function resize_main_content()
{
	return;
	var contentHeight = $("#content").height();
	var bannerHeight = $("#banner_right_container").height();
	$("#main_content_container").css("height",((contentHeight > bannerHeight) ? contentHeight : bannerHeight  )+30+"px");
	return;
}

function getMouseXY(e) {
	if(!e) e=window.event;
  if(ie6){
    tempX = e.clientX;
    tempY = e.clientY;
  }else if (ie) { 
    tempX = e.clientX + scrollLeft;
    tempY = e.clientY + scrollTop;
  } else { 
    tempX = e.pageX;
    tempY = e.pageY;
  }  

}



function init()
{
	//resize_main_content();	
	//$("body").css({background:'url(/media/images/bg_body.jpg) repeat fixed right top'});
	//newsheadline();
	init_document();
	init_announcement();
}

function init_announcement()
{
	var cont = $("#announcement");
	cont.find(".announcement-close").bind("click",function()
	{ 		
	
		$.ajax({url:'/app/actions/request.php?action=close_banner'
		,type:'POST',data:'id='+$(this).attr('rel')
		,success:function()
		{
			cont.css({whiteSpace:'nowrap'}).height(cont.height()).animate({width:'10px'},300,'linear'
			,function(){$(this).fadeTo(200,0.01,function(){$(this).remove();});});
		}});
		
	});
	
}




$ns =
{
	init: function()
	{
		setTimeout(function(){
		$ns.parent = $("#rss-container .rss");
		$ns.container_width = $ns.parent.width();
		$ns.width = 0;
		
		$ns.ul = $('<ul />').appendTo($ns.parent.empty());
		
		$.get("/app/actions/analektafeed.php?lg="+Lang,'',
		 function(data)
		 {
			var html = [];
			$(data).find('rss channel item')
			.each(function(i)
			{
				if(i>4) return;
				html.push('<li><a href="'+$(this).find('link').text()+'">'+$(this).find('title').text()+'</a></li>');	   
			});
			var append = [];
			for(var i=0;i<5;i++) append.push(html.join('<li class="span">&nbsp;</li>'));
			$ns.ul.append(append.join('<li class="span">&nbsp;</li>'));
					
			var content = $ns.parent.find("li");

			content.each(function(i)
			{
				$ns.width += parseInt($(this,i).outerWidth());
				
			})
			.hover($ns.stop,$ns.restart);
			
			$ns.duration = ($ns.width+$ns.container_width)/0.05;
			$ns.ul.hide().width($ns.width);
			setTimeout($ns.animate,1000);
		});
		},1000);
		return $ns;
	},
	animate: function()
	{
		$ns.ul.show().css({left:$ns.container_width+"px"});
		$ns.ul.animate({left: '-='+$ns.width+"px"},$ns.duration,'linear',$ns.animate);
	},
	stop:function()
	{
		$ns.ul.stop();
	},
	restart: function()
	{
		var left = $ns.ul.offset().left + $ns.width + $ns.container_width ;
		var time_left = left/0.05;
		
		$ns.ul.animate({left: '-='+left+"px"},time_left,'linear',$ns.animate);
	}
};

$($ns.init);





function subscribe()
{
	$.ajax({url:"/app/actions/request.php?action=subscribe",type:'POST',data:$(document.subscribe).serialize()
	,success:function(data)
	{
		data = eval('('+data+')');
		alert(data.msg);
		if(data.success)
		{
			document.subscribe.reset();
			track_event.subscribe_mailing_list();
		}
		
	}});
}



function clickclear(t,v)
{
	if(t.value == v)t.value = "";
}

function clickrecall(t,v)
{
	if(t.value == "")t.value = v;
}

function top()
{
	$.scrollTo( {top:"190px",left:0}, {duration:1000}  );
	return;
}

function got(div)
{
	$.scrollTo('#'+div,1000  );
	return;
}

function showbm(divid)
{
	if ($("#"+divid).is(":hidden")) $("#"+divid).slideDown("slow"); 
    else $("#"+divid).slideUp(); 
	
	track_event.share_btn();
	return;
}

function get_full_text()
{
	if(!$("#txt").is(":hidden"))
	{
		$("#txt").hide();
		$("#full_txt").css({top:0}).show();
	}
	else
	{
		$("#full_txt").hide();
		$("#txt").css({top:0}).show();
	}
	return;
}



function goto(url)
{
	location.href = url;	
}


function addemailtolist()
{
$.ajax({
	   type: "POST",
	  	url: "/app/actions/globalquery.php?lg="+lg+"&config=addtomailinglist&simple",
	  	async: false,
		data: $(document["addtolist"]).serialize(),
		success:function (d) 
		{
				var data = eval('('+d+')');
				alert(data.errormessage);
			if(data.error ==1)
			{
				document["addtolist"].reset();
				track_event.subscribe_mailing_list();
			}
		}
		});
	
}


var track_event =
{
	 subscribe_mailing_list:function()
	{
		if(!document.dev)
		_gaq.push(['_trackPageview','mailing_list_button_'+lg]);
	}
	,resellers:function(s,a,t)
	{
		if(!document.dev)
		_gaq.push(['_trackPageview','tag_magasin/'+s+'/'+a]);
		
		if(!document.dev)
		_gaq.push(['_trackEvent', 'Album-Resellers', (t?"Download":"Buy")+"-"+s]);
	}
	,anchor_download:function()
	{
		_gaq.push(['_trackEvent', 'Album-Utility', 'Download-link']);
	}
	,booklet:function()
	{
		if(!document.dev)
		_gaq.push(['_trackEvent', 'Album-Utility', 'PDF-booklet']);
	}
	,share_btn:function()
	{
		if(!document.dev)
		_gaq.push(['_trackEvent', 'Album-Utility', 'Share']);
	}
	,print_album:function()
	{
		if(!document.dev)
		_gaq.push(['_trackEvent', 'Album-Utility', 'Print']);
	}
	,facebook:function()
	{
		if(!document.dev)
		_gaq.push(['_trackEvent', 'Album-Utility', "Facebook"]);
		return true;
	}
	,twitter:function()
	{
		if(!document.dev)
		_gaq.push(['_trackEvent', 'Album-Utility', "Twitter"]);
	}
	,switch_format_album:function(f)
	{
		if(!document.dev)
		_gaq.push(['_trackEvent', 'Album-Quality', 'Select-'+f.toUpperCase()]);
	}
	,addtocart:function()
	{
		if(!document.dev){
		_gaq.push(['_trackPageview','ajout_panier_achat']);
		}
	}
	,addtocart_event:function(t)
	{
		
		if(document.dev == null)
		{
			switch(t)
			{
				case 'album':
					if(!document.dev)
					_gaq.push(['_trackEvent', 'Album-List', 'Download-All']);
				break;
				case 'song':
					if(!document.dev)
					_gaq.push(['_trackEvent', 'Album-List', 'Download-section']);
				break;
				case 'oeuvre':
					if(!document.dev)
					_gaq.push(['_trackEvent', 'Album-List', 'Download-track']);
				break;
			}
		}
	}
	,listen_streaming:function()
	{
		if(!document.dev)
		_gaq.push(['_trackEvent', 'Album-List', 'Listen-track']);
	}
	,landing_download:function(t)
	{
		if(!document.dev)
		_gaq.push(['_trackEvent', 'Landing-Pages', 'Download-'+t.toUpperCase()]);
	}
	,landing_download_album:function(t)
	{
		if(!document.dev)
		_gaq.push(['_trackEvent', 'Landing-Pages', 'Download-Album-'+t.toUpperCase()]);
	}
};

function printpage(c)
{
	if(/alb/.test(c)) track_event.print_album();
	
	
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open('/app/actions/globalquery.php?lg="+lg+"&simple&config=print&"+c+"', '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=447,height=93,left = 0,top = 0');");
}


function globalquery(upd,config)
{
	top();
	if(querysent) return;
	querysent = true;
	$.ajax({ type: "GET",url: "/app/actions/globalquery.php",async: true,data: "lg="+lg+"&config="+config,
	success:function (htm) 
	{
		querysent = false;
		 $("#"+upd).html(htm);
	}});
	return;
}

function globalqueryhome(u,c,p)
{
	$.ajax({type: "GET",url: "/app/actions/globalquery.php",async: true,data: "lg="+lg+"&config="+c,
	success:function (htm) 
	{
		$("#"+u).html(htm)
	}});
	return;
}

function addthistofav(t)
{
	$("#addthis").clone(true).appendTo("body")
		.attr("id","addthisbox").css({position:"absolute",zIndex:10000,left:parseInt(tempX+10)+"px",top:parseInt(tempY-10)+"px"}).show();
	$(t).mousemove(function(){$("#addthisbox").css({left:parseInt(tempX+10)+"px",top:parseInt(tempY-10)+"px"});})
		.mouseout(function(){$("#addthisbox").remove();});
}


function displaycompo(t,id)
{
	var div = "#compo_"+id;
	var div2 = "#compo_hover_"+id;
	if(!document.getElementById("compo_hover_"+id))
	{
		$(div).clone(true).appendTo("body").attr("id","compo_hover_"+id)
			.css({position:"absolute",zIndex:10000,left:parseInt(tempX+10)+"px",bottom:parseInt(innerHeight - tempY)+"px"}).show();
		$(t).mousemove(function(){$(div2).css({left:parseInt(tempX+10)+"px",bottom:parseInt(innerHeight - tempY)+"px"});})
			.mouseout(function(){$(div2).remove();});
	}
}

function displaywhy(t,id)
{
	var div = "#compo_"+id;
	var div2 = "#compo_hover_"+id;
	if(!document.getElementById("compo_hover_"+id))
	{
		$(div).clone(true).appendTo("body").attr("id","compo_hover_"+id)
			.css({height:"120px",width:"400px",position:"absolute",zIndex:10000,left:parseInt(334)+"px",top:parseInt(252)+"px"}).show();
		$(t).mouseout(function(){$(div2).remove();});
	}
}

function displaywhy2(t,id)
{
	var div = "#compo_"+id;
	var div2 = "#compo_hover_"+id;
	if(!document.getElementById("compo_hover_"+id))
	{
		$(div).clone(true).appendTo("body").attr("id","compo_hover_"+id)
			.css({width:"150px",position:"absolute",zIndex:10000,left:parseInt(800)+"px",top:parseInt(389)+"px"}).show();
		$(t).mouseout(function(){$(div2).remove();});
	}
}


function init_box(id)
{
	$stf.init(id);
}


var $stf =
{
	init: function(id)
	{
		this.overlay = $('<div id="black_box_overlay" />')
						.appendTo("body")
						.height($(window).height())
						.bind("click",this.close);
						
		this.content = $("#"+id).clone(true);
		this.container = $('<div id="black_box_overlay_inner" />')
						.appendTo("body");
		
		this.content.attr("id","black_box_overlay_inner_content")
				.show()
				.appendTo(this.container);
		
		this.container
						.css({
						    marginLeft:-this.container.outerWidth()/2+'px'
						   ,top: $(window).scrollTop()+this.container.outerHeight()/2+'px'
						   });
				
		window.resize = function()
		{
			$stf.overlay.height($(window).height());
		};
	
		return;
	}
	,close:function()
	{
		$stf.container.hide("fast",function(){$(this).remove();});
		$stf.overlay.fadeTo(500,0.01,function(){$(this).remove();});
	}
	
};



function go_back()
{
	location.href = (!referrer) ? "/" : referrer;	
}

function popupradio() 
{
	if($.macos.iphone || $.macos.ipod || $.macos.ipad)
	{
		location.href = 'http://itunes.apple.com/app/analekta-radio/id370897248';
		return;
	}
	
	var left = Math.floor(parseInt(innerWidth - 400)/2);
	var top  = Math.floor(parseInt(innerHeight - 550)/2);
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open('/app/actions/playeraudio.php?lg="+lg+"', '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=400,height=550,left = "+left+",top = "+top+"');");
}

function popupdownload()
{
	if($.macos.iphone || $.macos.ipod || $.macos.ipad)
	{
		location.href = 'http://itunes.apple.com/app/analekta-radio/id370897248';
		return;
	}
	var left = Math.floor(parseInt(innerWidth - 400)/2);
	var top  = Math.floor(parseInt(innerHeight - 500)/2);
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open('/media/swf/widget/', '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=400,height=500,left = "+left+",top = "+top+"');");
}



function create_cookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
  
}


function get_cookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function urldecode(url) //function decode URL
{
var HEXCHARS = "0123456789ABCDEFabcdef";
var encoded = url;
var plaintext = "";
var i = 0;
while (i < encoded.length) {
var ch = encoded.charAt(i);
if (ch == "+") {
plaintext += " ";
i++;
} else if (ch == "%") {
if (i < (encoded.length-2)
&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1
&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
plaintext += unescape( encoded.substr(i,3) );
i += 3;
} else {
alert( 'Bad escape combination near ...' + encoded.substr(i) );
plaintext += "%[ERROR]";
i++;
}
} else {
plaintext += ch;
i++;
}
} // while

return plaintext;
}; 
