$(document).ready(function() {
	$.ajaxSetup({
		async: true,
		error: function(status,errorText) {
			toggleLoader(false, false);
			toggleErrorBox(3000);
		}
	});	
	/* glow effect Home Button */
	$(".glow .normal").hover(
		function(){
			$(this).stop();
			$(this).animate({opacity: 0}, 600);
		},
		function(){
			$(this).stop();
			$(this).animate({opacity: 1}, 600);
		}
	);
});

function toggleErrorBox(fadeOut) {
	if ($('#box_error').css('display') == 'block') {
		//Fade out + Overlay
		$('#box_error').fadeOut(
			400,
			function() {
				hideOverlay();
			}
		);
	} else {
		//Fade in + Overlay
		showOverlay();
		$('#box_error').fadeIn(
			400,
			function() {
				if (fadeOut > 0) {
					window.setTimeout(function() {
						$('#box_error').fadeOut(
							400,
							function() {
								hideOverlay();
							}
						);
					}, fadeOut);
				}
			}
		);
	}
}

function toggleSuccessBox(fadeOut) {
	if ($('#box_success').css('display') == 'block') {
		//Fade out + Overlay
		$('#box_success').fadeOut(
			400,
			function() {
				hideOverlay();
			}
		);
	} else {
		//Fade in + Overlay
		showOverlay();
		$('#box_success').fadeIn(
			400,
			function() {
				if (fadeOut > 0) {
					window.setTimeout(function() {
						$('#box_success').fadeOut(
							400,
							function() {
								hideOverlay();
							}
						);
					}, fadeOut);
				}
			}
		);
	}
}

function showContextMenu(sender) {
	sender 		= $(sender).parent();
	var cMenu 	= sender.find(".contextMenu").length == 0 ? sender.parent().find(".contextMenu") : sender.find(".contextMenu");
	if(cMenu.length == 0)
		return;	
	var width 	= 0;
	cMenu.find('li').each(function(){
		//width = width >= $(this).find('div.center').width() ? width : $(this).find('div.center').width();
		width = width >= $(this).find('div.center').text().length ? width : $(this).find('div.center').text().length;
	});	
	width = width * 7+20;	
	cMenu.find('div.center').width(width<180?180:width);
	var offs 	= $(window).width()-sender.position().left-cMenu.width()-15;
	cMenu.css({
		"margin-top"	: (sender.height()+5)+'px',
		'position'		: 'absolute',
		'top'			: (sender.position().top)+'px'
	});
	
	var marginLeft = 75;
	if(cMenu.hasClass('viewMode')) {
		if(isMSIE()) {
			if(isMSIE('7')) {
				marginLeft = cMenu.width() + 20;
			} else {
				marginLeft = cMenu.width()/2;
			}
		}
		
		marginLeft = (marginLeft - 10)*-1;
		cMenu.css('margin-left', marginLeft+'px');
	} else if(sender.hasClass('dropbox')) {
		cMenu.css({
			'margin-left'	: (-1*($.browser.msie && $.browser.version.slice(0,1) == '7'?sender.width():0)+8)+'px',
			'top'			: (sender.position().top-5)+'px'
		});
	} else {
		cMenu.css('left', (sender.position().left-(offs<0?Math.abs(offs):0))+'px');
	}
	
	cMenu.fadeIn(300, function() {
		$(document).one('click', function(){
			cMenu.hide();
			$(".media.active").removeClass("active");
			$(".album.active").removeClass("active");
		});
		$(window).one("resize.contextmenu", function(){
			$(document).click();
		});
	});
	
	checkSubmenu2containerHeight();
}

window.fixDivxPlayerBug = function () {
    var b = document.getElementsByTagName('body')[0];
    if ( window['ReplaceVideoElements'] && ! b.appendChildDivx ){
        b.appendChildDivx = b.appendChild;
        b.appendChild = function(element){
        	if(typeof(element.tagName)=="undefined"){
        		element.tagName="A";
        	}
		};
    }
};
