/*------------------------------------------------------------------------------*/
function isBrowserIE()
{
	return $.browser.msie;
}

/*------------------------------------------------------------------------------*/
function isBrowserSup(version)
{
	if(parseFloat($.browser.version) > version)
		return true;
	else
		return false;
}

/*------------------------------------------------------------------------------*/
function improveUI(contextReloaded)
{

	doCorners(contextReloaded);
	doQtip(contextReloaded);
	improveLinks(contextReloaded);
	initMyNotificationsHiding();
	improveFades();
	improveTextareas(contextReloaded);
}

/*------------------------------------------------------------------------------*/
function initMyNotificationsHiding()
{
	$('#myNotification_title').each(function(){
		if(!$(this).has('.doesnthide'))
		{
		 	$(this).oneTime(5000, function() {
					 toggleSubtitle($(this));
			});  
		}
	});
	
}

/*------------------------------------------------------------------------------*/
function doQtip(contextReloaded)
{
	$(contextReloaded+' [title]').qtip({
		position: { 
			target: 'mouse',
			adjust: { x: -3, y: -3 },
			corner: {
			        target: 'topRight',
			        tooltip: 'rightBottom'
			     }
			},
	   style: {
	      name: 'cream',
	      padding: '7px 13px',
	      width: {},
	      tip: true
	   }
	});
}

/*------------------------------------------------------------------------------*/
function doCorners(contextReloaded)
{
	if($.support.opacity)
	{
		$(contextReloaded+" .mycontents_subtitle").corner("5px");
		$(contextReloaded+" .mycontents_title").corner("5px");
		$(contextReloaded+" .mycontents_myoperation").corner("4px");
		$(contextReloaded+" input").corner("3px");
		$(contextReloaded+" textarea").corner("3px");
		$(contextReloaded+" select").corner("3px");
		$(contextReloaded+" .multiple").corner("3px");
		$(contextReloaded+" .submit").corner("4px");
		$(contextReloaded+" table").corner("4px");
		$(contextReloaded+" .mycontents_menu a").corner("4px");
		$(contextReloaded+" .mycontents_menu_selected").corner("4px");
		$(contextReloaded+" .mycontents_mynotification_warning").corner("4px");
		$(contextReloaded+" .mycontents_mynotification_success").corner("4px");
		$(contextReloaded+" .mycontents_mynotification_info").corner("4px");
		$(contextReloaded+" .mycontents_fav_link").corner("4px");
	}
}

/*------------------------------------------------------------------------------*/
function initVerticalFollowing(identifier,target)
{
	if($(identifier).exists())
	{
		$(window).scroll(function()
		{		
			if($(window).scrollTop() > $(identifier).offset().top)
			{
				$(target).css("position", "absolute");
				$(target).css("top", $(window).scrollTop());
			}
			else
			{
				$(target).css("position", "absolute");
				$(target).css("top", $(identifier).offset().top);
			}
		});
	}
}

/*------------------------------------------------------------------------------*/
function intiFavLinkContainerFollowing()
{
	initVerticalFollowing(".mycontents_fav_links_identifier",".mycontents_fav_links");
}

/*------------------------------------------------------------------------------*/
function initDragDropLinks(contextReloaded)
{
	$(contextReloaded+" a").draggable({revert: true, helper: "clone", distance: 20});
	$(contextReloaded+" .mycontents_apps_installed").draggable({revert: true, helper: "clone", distance:20});
	$(contextReloaded+" .mycontents_fav_link").draggable({distance:40});

	$(contextReloaded+" .mycontents_fav_links_drop" ).droppable({
		drop: function( event, ui ) {
			url = ui.helper.attr("href");
			title = ui.helper.attr("title");
			if(url == undefined) url = $(ui.helper).parent('a').attr("href");
			if(title == "") title = url;	
			ajaxGet(makeUrlAjax("myUser","addFavoriteLink",new Array(globalIdUser,$.base64.encode(url),$.base64.encode(title))),".trap",function(){ajaxGet(makeUrlAjax("myUser","partialCustomMenu",new Array(),"myContents"),".mycontents_fav_links_links");});
		}
	});
	
	$( contextReloaded+" .mycontents_fav_links_bin" ).droppable({
		drop: function( event, ui ) {
			idLink = $(ui.helper).parent('a').attr("id");
			ajaxGet(makeUrlAjax("myUser","removeFavoriteLink",new Array(globalIdUser,idLink)),".trap",function(){ajaxGet(makeUrlAjax("myUser","partialCustomMenu",new Array(),"myContents"),".mycontents_fav_links_links");});
		}
	});
}

/*------------------------------------------------------------------------------*/
function improveFades()
{
	doFade(".fadeOutInItem1","0.1","1","fast");
	doFade(".fadeOutInItem3","0.3","1","fast");
	doFade(".fadeOutInItem5","0.5","1","fast");
	doFade(".fadeOutInItem8","0.8","1","fast");
	doFade(".fadeOutInItem9","0.9","1","fast");
}

/*------------------------------------------------------------------------------*/
function improveTextareas(contextReloaded)
{
	$(contextReloaded+' textarea').each(function()
	{
		if(!$(this).hasClass('bypass'))
			$(this).elastic();
	});
}

/*------------------------------------------------------------------------------*/
function doFade(selector,from,to,speed)
{
	$(selector).stop().fadeTo(speed,from);
	$(selector).hover(function () {
			$(this).stop().fadeTo(speed,to);
		},
		function () {
			$(this).stop().fadeTo(speed,from);
		}
	);
}

/*------------------------------------------------------------------------------*/
function improveLinks(contextReloaded)
{
	$(contextReloaded+" a").click(function() 
	{ 
		if($(this).hasClass('popup'))
		{
			launchAInPopup($(this));
			return false;
		}
		if($(this).hasClass('bypass'))
			return true;	
		if($(this).attr('rel') !== undefined && $(this).attr('rel')!= "")
			return true;
		if($(this).attr('target') !== undefined && $(this).attr('target')!= "")
			return true;
		if($(this).attr('href') != '#')
			displayWaitingScreen();
	}); 	    
	
	$(contextReloaded+" :submit").each(function(i)
	{
		$(this).click(function()
		{
			if($(this).hasClass('bypass'))
				return true;
			displayWaitingScreen();	
		});
	});
}

/*------------------------------------------------------------------------------*/
function launchDivInPopup(divIdentifier,width,height,onClosedCallback)
{
	if(height == undefined)
		height = '90%';
	if(width == undefined)
		width = '850px';
	if(onClosedCallback == undefined)
		onClosedCallback = function(){};
		
	params = new Array();
	params['inline'] = true;
	params['href'] = divIdentifier;
	params['width'] = width;
	params['height'] = height;
	params['onClosed'] = onClosedCallback;
			
	parent.$.colorbox(params);
	$("#cboxClose").corner("top 5px");
	$("#cboxLoadedContent").corner("bottom left 5px");
}

/*------------------------------------------------------------------------------*/
function launchUrlInPopup(url,width,height)
{
	if(height == undefined)
			height = '90%';
	if(width == undefined)
		width = '850px';	
	params = new Array();
	
	params['href'] = url;
	params['iframe'] = true;	
	params['width'] = width;
	params['height'] = height;
	
	parent.$.colorbox(params);
	$("#cboxClose").corner("top 5px");
	$("#cboxLoadedContent").corner("bottom left 5px");
}

/*------------------------------------------------------------------------------*/
// a mettre sur onclick d'un a <a href='mon lien' onClick="launchAInPopup($(this))">
function launchAInPopup(aObject,width,height)
{
	$(aObject).click(function(){return false;});
	url = $(aObject).attr("href");
	launchUrlInPopup(url,width,height);
}

/*------------------------------------------------------------------------------*/
function displayWaitingScreen()
{
	$.colorbox({width:"310px", transition:'none', inline:true, href:"#mycontents_waitingScreen",overlayClose:false,escKey:false});
	$("#cboxLoadedContent").css("background-color","#444444");
	$("#cboxLoadedContent").corner("5px");
	$("#cboxClose").hide();
	//$("#cboxOverlay").hide();
	//$("#cboxOverlay").fadeIn('slow');
	$(this).oneTime(20000, function() {closeWaitingScreen()});
}

/*------------------------------------------------------------------------------*/
function closeWaitingScreen()
{
	$("#cboxOverlay").stop();
	$.colorbox.close();
}

/*------------------------------------------------------------------------------*/
function displayErrorScreen()
{

}

/*------------------------------------------------------------------------------*/
function toggleSubtitle(object){
	$(object).next().toggle('blind','fast');
}

/*------------------------------------------------------------------------------*/
function popup(_url, _name, _width, _height, _scrolls)
{
	var newwindow;
	var top=(screen.height-_height)/2;
	var left=(screen.width-_width)/2;
	newwindow=window.open(_url,_name,'top='+top+',left='+left+',height='+_height+',width='+_width+',scrollbars='+_scrolls);
	if (window.focus) {newwindow.focus()}
}

/*------------------------------------------------------------------------------*/
function makeDropdownMenu()
{
    $("ul.dropdown li").hover(function(){   
        $(this).addClass("hover");
        $('ul:first',this).css('visibility', 'visible');
    }, function(){
        $(this).removeClass("hover");
        $('ul:first',this).css('visibility', 'hidden');
    });
    $("ul.dropdown li ul li:has(ul)").find("a:first").append(" &raquo; ");
}
