var $ = jQuery.noConflict();
// Settings and Variables
var bgRunning = false;
var bgTimer;
var menuTimer;
var subMenuTimer;
var pageLoading=false;
var myAudio, btnSound;
var animateMenuPosition = false;
var twtTimer;
var showBgCaption = true;
var audioSupport = true;
var mobileDevice = false;
var minWidth = 1000;
// Detect mobile device
if( navigator.userAgent.match(/Android/i) ||
 navigator.userAgent.match(/webOS/i) ||
 navigator.userAgent.match(/iPhone/i) ||
 navigator.userAgent.match(/iPod/i)
 ){
	mobileDevice = true;
}

// Init after Loaded
$(window).load(function(){
	// Set deeplink
	$.history.init(openPage);
	
	// Dedect browser
	if($.browser.msie && $.browser.version<9)
	{
		audioSupport = false;
		$('#audioControls').hide();
	}
	
	if(mobileDevice)
	{
		normalFade = true;
		audioSupport = false;
		$('#audioControls').hide();
	}

	$(window).bind('resize', function() {
			doSize();
		});
	$(window).bind('scroll', function() {
			doSize();
		});
	$('#bgImages li:first-child').addClass('active');
	runBg();
	
	$(document).mousemove(function(e){
		if($('#menu-container').position().left<=($('#menu-container').outerWidth())*-1 && animateMenuPosition)
		{
			var menuY = e.pageY;
			var maxY = $('#body-wrapper').height()-$('#menu-container').height()-75;
			if(e.pageY<$('#menu-container').height()+50)
				menuY = $('#menu-container').height()+50;
			if(e.pageY>maxY)
				menuY = maxY;
			$('#menu-container').stop().animate({top:(menuY-35)}, 1000, 'easeOutQuad');
		}
		
		if(e.pageX > ($('#body-wrapper').width()-$('#bgImages').width()-20) && e.pageX<($('#body-wrapper').width()-20))
		{
			var posTop = parseInt((($('#body-wrapper').height()-$('#footer').height())/$('#body-wrapper').height())*e.pageY)*-1;
			if(posTop>0)
				posTop=0;
			if(posTop<=($('#body-wrapper').height()-$('#footer').height())-$('#bgImages').height())
				posTop=($('#body-wrapper').height()-$('#footer').height())-$('#bgImages').height();
			$('#bgImages').stop().animate({top:posTop}, 300);
			setCaptionPosition();
		}
	}); 
	
	$('#bgImages li').hover(function(){
			$(this).find('img.thumb').stop().animate({opacity:'1'}, 500);
	},function(){
		if(!$(this).hasClass('active'))
			$(this).find('img.thumb').stop().animate({opacity:'.4'}, 500);
	}).click(function(){
		if(!$(this).hasClass('active') && !bgRunning)
		{
			clearInterval(bgTimer);
			$('#bgImages li').removeClass('active');
			$(this).addClass('active');
			runBg();
		}
	});
	
	initMenu();
	doSize();
	
	// Social ToolTip
	$('#share .tip').hover(function(){
		if($(this).attr('tips-id')==undefined)
			$(this).attr('tips-id', 'tips-'+randomString(5));
		var tipsID = $(this).attr('tips-id');
		if($('#'+tipsID).length==0){
			var pos = $(this).position();
			$('#footer').append($('<div id="'+tipsID+'" class="tipbox">'+$(this).attr('tip-text')+'<span></span></div>'));
			$('#'+tipsID).css({top:(pos.top-$('#'+tipsID).height()-25)+'px', opacity:'0', left:(pos.left+($(this).width()-$('#'+tipsID).width()-20)/2)+'px'});
			$('#'+tipsID).find('span').css('left', parseInt(($('#'+tipsID).width()-9+20)/2)+'px');
		}
		$('#'+tipsID).stop().animate({opacity:'1'});
	}, function(){
		var tipsID = $(this).attr('tips-id');
		$('#'+tipsID).stop().animate({opacity:'0'}, function(){
			$(this).remove();
		});
	});
	
	if(audioSupport)
	{
		myAudio = new Audio(); 
		var audioTagSupport = !!(myAudio.canPlayType);
		{
			$('#audioControls .pause').click(function(){
				myAudio.pause();
				$('#audioControls .pause').css('display','none');
				$('#audioControls .play').css('display','block');
			});
			$('#audioControls .play').click(function(){
				myAudio.play();
				$('#audioControls .pause').css('display','block');
				$('#audioControls .play').css('display','none');
			});
			
			$('#audioControls .next').click(function(){			
				if(!$('#audioList li.active').is(':last-child'))
					$('#audioList li.active').removeClass('active').next().addClass('active');
				else
					$('#audioList li.active').removeClass('active').parent().find('li:first-child').addClass('active');
				path = $('#audioList li.active').html();
				playAudio(path);
			});
			
			$('#audioControls .prev').click(function(){		
				if(!$('#audioList li.active').is(':first-child'))
					$('#audioList li.active').removeClass('active').prev().addClass('active');
				else
					$('#audioList li.active').removeClass('active').parent().find('li:last-child').addClass('active');
				path = $('#audioList li.active').html();
				playAudio(path);
			});
			
			playAudio($('#audioList li:first-child').addClass('active').html());
			
			btnSound = new Audio();
			if(btnSoundURL!='')
			{
				var canPlayMp3 = !!btnSound.canPlayType && "" != btnSound.canPlayType('audio/mpeg');
				btnSound.src = btnSoundURL+((canPlayMp3)?'.mp3':'.ogg');
				
				$('a').mouseover(function(){
					btnSound.play();
				});
			}
		}
	}
	
	if(!mobileDevice)
	{
		$('.twButton').hover(function(){
			$(this).addClass('twActive');
			$('.twContent').stop(true).show().css('opacity','0').animate({opacity:'1'}, 0);
			clearTimeout(twtTimer);
			nextTweet();
			$('.twContent').hover(function(){
				$('.twContent').stop(true).animate({opacity:'1'}, 0);
			}, function(){
				$('.twContent').stop().delay(300).animate({opacity:'0'}, 0, function(){
					$('.twContent').unbind('mouseleave').unbind('mouseenter').hide();
					$('.twButton').removeClass('twActive');
				});
			});
		}, function(){
			$('.twContent').stop().delay(300).animate({opacity:'0'}, function(){
				$('.twContent').unbind('mouseleave').unbind('mouseenter').hide();
				$('.twButton').removeClass('twActive');
			});
		});
	}else{
		$('.twButton').click(function(){
			if($('.twContent').is(':hidden')){
				clearTimeout(twtTimer);
				nextTweet();
				$(this).addClass('twActive');
				$('.twContent').show().css('opacity','1');
			}else{
				$(this).removeClass('twActive');
				$('.twContent').hide();
			}
		});
	}
	
	$('#bodyLoading').animate({opacity:'0', top:-200}, 1000, 'easeOutBack', function(){
		$(this).remove();
	});
	$('#body-wrapper').delay(500).animate({opacity:'1'}, 1000);
	
	if(frontPage!='')
		openPage(frontPage);
});

function nextTweet(){
	if($('.twContent li.active').length>0)
	{
		if(!$('.twContent li.active').is(':last-child'))
			$('.twContent li.active').removeClass('active').next().addClass('active');
		else
			$('.twContent li.active').removeClass('active').parent().find('li:first-child').addClass('active');
	}else{
		$('.twContent li:first-child').addClass('active');
		$('.twContent li:not(.active)').hide();
	}
	$('.twContent ul').animate({height:$('.twContent li.active').height()+10}, 500);
	$('.twContent li:not(.active)').fadeOut('slow', function(){
		$('.twContent li.active').fadeIn('slow');
	});
	twtTimer = setTimeout(nextTweet, twtTime);
}

// init menu
function initMenu(){
	var logoData = {
		left: 20,
		top: ((75-$('#logo').height())/2),
		width: $('#logo img').width(),
		height:$('#logo img').height()
	};
	$('#logo').data('logoData', logoData);
	$('#logo').css({left:logoData.left+'px', top:logoData.top+'px', width:logoData.width+'px', height:logoData.height+'px'});
	$('#logo img').css({width:(logoData.width/4)+'px', height:(logoData.height/4)+'px', opacity:'0'});
	
	// Menu Closer
	if(mobileDevice){
		$('#menu-container').css('borderRadius','0');
		$('#menuCloser').click(function(){
			 closeMenu();
		});
	}
	// menu Openner setting
	if(!mobileDevice){
		$('#menuOpener').mouseover(function(){
			if($('#menu-container').position().left<0)
				showMenu();
		});
	}else{
		$('#menuOpener').click(function(){
			if($('#menu-container').position().left<0)
				showMenu();
		});
	}
	// Menu Buttons Click Action
	$('#mainmenu ul li a[target!="_blank"]').each(function(){
		$(this).click(function(){
			$.history.load($(this).attr('href'));
            return false;
		});
	});
	
	// Set Menu Container Hover
	if(!mobileDevice)
	{
		$('#menu-container').mouseenter(function(event){
			clearTimeout(menuTimer);
		}).mouseleave(function(event){
			clearTimeout(menuTimer);
			menuTimer = setTimeout(closeMenu, menuTime);
		});
	}
	// Set Sub Menu Positions
	$('#mainmenu ul li ul').each(function(){
		var ulW = 0;
		$(this).find('li').each(function(){ ulW += $(this).width(); });
		$(this).css({width:(ulW)+'px'});
		var ulW = 0;
		$(this).find('li').each(function(){ ulW += $(this).width();	});
		$(this).css({width:(ulW+2)+'px'});
		$(this).css('left', '-'+((((ulW+20)-$(this).parent().width())/2))+'px');
	});
	
	// Close Menu as First Position
	$('#menu-container').css({left:'-'+($('#menu-container').outerWidth())+'px'});
	
	if(!bgPaused)
		bgTimer = setInterval(nextBg, bgTime);
	else{
		$('#bgControl .play').show();
		$('#bgControl .pause').hide();
	}
}

// Resize All Elements
function doSize(){ 
	var winW = $(window).width();
	if(mobileDevice)
		winW = minWidth;
	var winH = $(window).height();
	var winRatio = winW/winH;
	$('#body-wrapper').css({width:winW+'px', height:winH+'px'});
	if($('#bgImages li.active iframe').length>0){
		var imgW = $('#bgImages li.active iframe').width();
		var imgH = $('#bgImages li.active iframe').height();
	}else{
		var imgW = $('#bgImages li.active img.source').width();
		var imgH = $('#bgImages li.active img.source').height();
	}
	var imgRatio = imgW/imgH;
	var imgLeft=0;
	var imgTop=0;
	
	// Calculate Image Width, Height and Positions
	if(winRatio>imgRatio)
	{
		imgW = parseInt(winW);
		imgH = parseInt(imgW/imgRatio);
		
	}else{
		imgH = winH;
		imgW = parseInt(imgH*imgRatio);
	}
	imgLeft = parseInt((winW-imgW)/2);
	imgTop = parseInt((winH-imgH)/2);
	
	// Set Bg Image W, H
	$('#bgImage .new').css({width:imgW+'px', height:imgH+'px'});
	// Set Bg Image Position
	$('#bgImage .new').css({left:imgLeft+'px', top:imgTop+'px'});
	
	// Set Pattern W, H
	$('#bgPattern').css({width:winW+'px', height:winH+'px'});
	$('#content').css({left:(winW-826)+'px', height:(winH-$('#footer').height())+'px'});
	setContentMargin();
}

// Background Image Auto Next
function autoBg(){
	if(bgPaused) return false;
	nextBg();
}

// Background Image Next Button
function nextBg() {
	if(bgRunning) return false;
	clearInterval(bgTimer);
	if(!$('#bgImages li.active').is(':last-child'))
		$('#bgImages li.active').removeClass('active').next().addClass('active');
	else
		$('#bgImages li.active').removeClass('active').parent().find('li:first-child').addClass('active');
	runBg();
}

// Background Image Prev Button
function prevBg(){
	if(bgRunning) return false;
	clearInterval(bgTimer);
	if(!$('#bgImages li.active').is(':first-child'))
		$('#bgImages li.active').removeClass('active').prev().addClass('active');
	else
		$('#bgImages li.active').removeClass('active').parent().find('li:last-child').addClass('active');
	runBg();
}

// Background Image Pause Button
function pauseBg(){
	clearInterval(bgTimer);
	$('#bgControl .play').show();
	$('#bgControl .pause').hide();
	bgPaused = true;
	$('#bgImage img.new').stop();
}

// Background Image Play Button
function playBg(){
	clearInterval(bgTimer);
	$('#bgControl .play').hide();
	$('#bgControl .pause').show();
	bgPaused = false;
	nextBg();
}

// Background Image Animation
function runBg(){
	$('#bgImageWrapper .source').removeClass('new').addClass('old');
	if($('#bgImages li.active iframe').length>0){
		$('#bgImageWrapper').prepend($('#bgImages li.active iframe').clone().addClass('new').addClass('source').css('opacity','0').attr('src', $('#bgImages li.active iframe').attr('src')+'&autoplay=1&loop=1&controls=0'));
	}else{
		$('#bgImageWrapper').prepend($('#bgImages li.active .source').clone().addClass('new').css('opacity','0'));
	}
	
	$('#bgText h3, #bgText .subText').stop().animate({opacity:'0'}, 300 , function(){
		$('#bgText h3').html($('#bgImages li.active h3').text());
		$('#bgText .subText').html($('#bgImages li.active p').text());
		$(this).animate({opacity:'1'}, 300);
	});
	setCaptionPosition();
	
	bgRunning = true;
	clearInterval(bgTimer);
	doSize();
	$('#bgImages li img').stop().animate({opacity:'.4'},500);
	$('#bgImages li.active img').stop().animate({opacity:'1'},500);
	if($('#bgImageWrapper .old').length>0)
	{
		$('#bgImageWrapper .old').stop(true).animate({opacity:0}, 500, function(){
			$(this).remove();
			bgRunning = false;
		});
	}else{bgRunning = false;}
	
	bW = parseInt($('#bgImageWrapper .new').width()*.1);
	bH = parseInt($('#bgImageWrapper .new').height()*.1);
	$('#bgImageWrapper .new').stop(true).animate({opacity:1}, 500);
	if($('#bgImageWrapper iframe.new').length==0 && !normalFade)
		$('#bgImageWrapper .new').animate({width:$('#bgImageWrapper .new').width()+bW, height:$('#bgImageWrapper .new').height()+bH},bgTime+2000);

	if(bgTime>0 && bgPaused==false)
		bgTimer = setInterval(autoBg, bgTime);
}

// Open Inner Page
function openPage(getURL){
	// Page Loading on Click
	if(pageLoading || getURL=='' || getURL.charAt(0) == '!' || getURL=='#') return false;
	pageLoading = true;
	var pageLoadingURL = getURL;
	closeMenu();
	if($('#contentBox').html()=='')
	{
		if(pageLoadingURL!='/'){
			if(frontPage!='')
			{
				showLoading();
				$('#contentBox').load(frontPage, pageLoadReady);
			}else{
				showLoading();
				$('#contentBox').load(pageLoadingURL, pageLoadReady);
			}
		}
	}else{
		$('#contentBox').animate({opacity:'0', marginTop:-200}, 600, 'easeOutExpo', function(){
			if(pageLoadingURL!='/'){
				showLoading();
				$('#contentBox').load(pageLoadingURL, pageLoadReady);
			}else{
				if(frontPage!=''){
					showLoading();
					$('#contentBox').load(frontPage, pageLoadReady);
				}else{
					$('#contentBox').html('');
					pageLoading = false;
					showBgCaption = true;
					setCaptionPosition();
				}
			}
		});
	}
	return false;
}

function showLoading(){
	$('#contentLoading').css({top:'300px', opacity:'0'}).show();
	$('#contentLoading').animate({opacity:'1', top:200}, 'easeInExpo');
}
function hideLoading(){
	$('#contentLoading').animate({opacity:'0', top:100}, 'easeOutExpo', function(){
		$(this).hide();
	});
}

function pageLoadReady(){
	var imageTotal = $('#contentBox img').length;
    var imageCount = 0;
	if(imageTotal>0)
	{
		$('#contentBox img').load(function(){
			if(++imageCount == imageTotal) 
			pageLoaded();
		}).error(function(){
			if(++imageCount == imageTotal) 
			pageLoaded();
		});
	}else{
		pageLoaded();
	}
}

// Inner Page Loaded Actions
function pageLoaded(){
	showBgCaption = false;
	setCaptionPosition();
	pageLoading = false;
	hideLoading();
	$('#contentBox').scrollTop(0);
	setContentMargin();
	$('#contentBox').show().css({opacity:'0', marginTop:'200px'});
	$('#contentBox').stop().delay(500).animate({opacity:'1', marginTop:20}, 600, 'easeOutExpo');
	$('#contentBox a').mouseover(function(){
		if(audioSupport)
			btnSound.play();
	});
	
	setLink();
	
	// Ajax Comment Post
	acp_initialise();
	// Set click sound to all links
	$('#contentBox .blogTop a').click(function(){
		$('#content').stop().animate({scrollTop:0}, 1000, 'easeOutExpo');
	});
	
	setImageModal();
	
	// Set Meta Tips
	$('#contentBox .meta-links a').hover(function(){
		if($(this).attr('tips-id')==undefined)
			$(this).attr('tips-id', 'tips-'+randomString(5));
		var tipsID = $(this).attr('tips-id');
		if($('#'+tipsID).length==0){
			var pos = $(this).position();
			$('#contentBox').append($('<div id="'+tipsID+'" class="meta-tips">'+$(this).attr('rel')+'<span></span></div>'));
			$('#'+tipsID).css({top:(pos.top+ (($(this).height()-$('#'+tipsID).height())/2))+'px', opacity:'0', left:(pos.left-$('#'+tipsID).width()-30)+'px'});
		}
		$('#'+tipsID).stop().delay(300).animate({opacity:'1'});
	}, function(){
		var tipsID = $(this).attr('tips-id');
		$('#'+tipsID).stop().delay(300).animate({opacity:'0'}, function(){
			$(this).remove();
		});
	});
	
	// ToolTip
	$('#contentBox .tip').hover(function(){
		if($(this).attr('tips-id')==undefined)
			$(this).attr('tips-id', 'tips-'+randomString(5));
		var tipsID = $(this).attr('tips-id');
		if($('#'+tipsID).length==0){
			var pos = $(this).position();
			$('#contentBox').append($('<div id="'+tipsID+'" class="tipbox">'+$(this).attr('tip-text')+'<span></span></div>'));
			$('#'+tipsID).css({top:(pos.top-$('#'+tipsID).height()-30)+'px', opacity:'0', left:(pos.left+($(this).width()-$('#'+tipsID).width()-20)/2)+'px'});
			$('#'+tipsID).find('span').css('left', parseInt(($('#'+tipsID).width()-9+20)/2)+'px');
		}
		$('#'+tipsID).stop().animate({opacity:'1'});
	}, function(){
		var tipsID = $(this).attr('tips-id');
		$('#'+tipsID).stop().animate({opacity:'0'}, function(){
			$(this).remove();
		});
	});
	
	setImageAnimations();
	
	// Form Focus
	$('#contentBox .dform input[type=text], #contentBox .dform select, #contentBox .dform textarea').focus(function(){
		$(this).parent().addClass('dFormInputFocus');
	}).blur(function(){
		$(this).parent().removeClass('dFormInputFocus');
	});
	
	// Button Animation
	$('#contentBox .buttonSmall, #contentBox .buttonMedium').hover(function(){
		$(this).stop().animate({opacity:'.50'}, 400);
	}, function(){
		$(this).stop().animate({opacity:'1'}, 400);
	});
	
	//Filter
	var $applications = $('#contentBox .portfolioitems');
	var $data = $applications.clone();
	
	// Toggle Button
	$('div.sh_toggle_text').click( function(){
		$(this).parent().find(".sh_toggle_content").slideToggle("slow");
			$(this).toggleClass("sh_toggle_text_opened");
		}
	); 
	$('#contentBox .portfolioFilter li a').click(function(e) {
		var dataValue = $(this).parent().attr('data-value');
		if (dataValue=='all'){
			var $filteredData = $data.find('li');
		} else {
			var $filteredData = $data.find('li[data-type~="' + dataValue + '"]');
		}

		// finally, call quicksand
		$('.hoverWrapper, .hoverWrapper a').hide();
		$applications.quicksand($filteredData, {
		  duration: 800,
		  easing: 'easeInOutQuad',
		  enhancement: function(){ 
			setLink();
			setImageModal();
			setImageAnimations(); }
		}, function(){
			//
		});
	});
}

function setLink(){
	$('#contentBox a[target!="_blank"]:not(.nolink|)').each(function(){
		$(this).click(function(){
			$.history.load($(this).attr('href'));
            return false;
		});
	});
}

function setImageModal(){
	var modalid = randomString(5);
	$('#contentBox .image_frame a img').not('.nomodal').parent().attr('rel','gallery[photo'+modalid+']');
	$('#contentBox a[rel="gallery[photo'+modalid+']"]').not('.nomodal').prettyPhoto({theme:prettyTheme, autoplay: true, opacity:0.4, show_title: false});
}

function setImageAnimations(){
	// Image Animation
	$('#contentBox .image_frame a img').each(function(){
		// Set First Position and Size for Image Hover
		$(this).parent().parent().find('.hoverWrapperBg').css({width:$(this).width()+'px', height:$(this).height()+'px', opacity:'0'});
		$(this).parent().parent().find('.hoverWrapper').css({width:$(this).width()+'px', height:$(this).height()+'px'});
		$(this).parent().parent().find('.hoverWrapper h3, .hoverWrapper .enter-text').css({opacity:'0'});
		$(this).parent().parent().find('.hoverWrapper a').css({opacity:'0'});
		
		if($(this).parent().parent().find('.hoverWrapper h3, .hoverWrapper .enter-text').length==0)
			$(this).parent().parent().find('.modal, .link, .modalVideo').css({bottom:parseInt((($(this).height()-26)/2))+'px'});
		
		var centerX = parseInt($(this).parent().parent().find('.hoverWrapper').width()/2);
		$(this).parent().parent().find('.modal, .modalVideo').css({left:(centerX-41)+'px'}).click(function(event){
			$(this).find('a').trigger('click');
		});
		$(this).parent().parent().find('.link').css({left:(centerX+15)+'px'}).click(function(event){
			event.stopPropagation();
		});
		$(this).parent().click(function(event){
			event.stopPropagation();
		});
		// Set Image Hover Animation
		$(this).parent().parent().hover(function(event){
			event.stopPropagation();
			$(this).find('.modal, .modalVideo').stop().animate({opacity:'1', left:(centerX-31)}, 100 );
			$(this).find('.link').stop().animate({opacity:'1', left:(centerX+5)}, 100);
			$(this).find('.hoverWrapperBg').stop().animate({opacity:'.85'}, 400);
			$(this).find('.hoverWrapper h3').stop().delay(100).animate({opacity:'1'}, 300);
			$(this).find('.hoverWrapper .enter-text').stop().delay(200).animate({opacity:'1'}, 300);
		}, function(event){
			event.stopPropagation();
			$(this).find('.modal, .modalVideo').stop().animate({opacity:'0', left:(centerX-41)}, 100);
			$(this).find('.link').stop().animate({opacity:'0', left:(centerX+15)}, 100);
			$(this).find('.hoverWrapperBg').stop().animate({opacity:'0'}, 400);
			$(this).find('.hoverWrapper h3').stop().animate({opacity:'0'}, 300);
			$(this).find('.hoverWrapper .enter-text').stop().animate({opacity:'0'}, 300);
		}).click(function(event){
			event.stopPropagation();
			$(this).find('> a').trigger('click');
		});
	});
}

// Close Main Menu
function closeMenu(){
	if(mobileDevice)
		$('#menuCloser').hide();
		
	if(!$('#contentBox').html()==''){
		showBgCaption = false;
		setCaptionPosition();
	}
	closeSubMenu();
	$('#mainmenu a, #mainmenu li').unbind('mouseenter').unbind('mouseleave').unbind('mouseover').unbind('mouseout');
	$('#mainmenu ul li.active').removeClass('active');
	$('#content').stop().show().animate({opacity:1}, 800);
	$('#menu-container').stop().animate({left:'-'+($('#menu-container').outerWidth())+'px'}, 400, 'easeOutQuad', function(){
		$('#menuOpener').show().animate({opacity:'1'});
		animateMenuPosition = false;
	});
}

// Close Sub Menu
function closeSubMenu(menuID){
	if(menuID==undefined){
		$('#mainmenu ul li ul').stop(true, true).animate({opacity:'0'}, 400, 'easeOutExpo', function(){
			$(this).hide();
		});
	}else{
		$('#mainmenu ul li:not([id='+menuID+']) ul').stop(true, true).animate({opacity:'0'}, 400, 'easeOutExpo', function(){
			$(this).hide();
		});
	}
}

// Main Menu Item over Animation
function menuItemOver(obj){
	if (obj==undefined)
		obj = this;
	if(!mobileDevice)
		clearTimeout(menuTimer);
	$(obj).find('span').stop(true,true).animate({top:75}, 0).animate({top:0},600, 'easeOutBack');				
	if(audioSupport)
		btnSound.play();
}

// Main Menu Item out Animation
function menuItemOut(obj){
	if (obj==undefined)
		obj = this;
	$(obj).find('span').stop(true, true).animate({top:0}, 300);
}

// Main Menu Show Animation
function showMenu(){
	showBgCaption = true;
	setCaptionPosition();
	$('#content').stop().animate({opacity:0}, 800, function(){
		$(this).hide();
	});
	
	if(mobileDevice)
		$('#menuCloser').show();
	
	// animate menu
	animateMenuPosition = false;
	$('#mainmenu ul li a').unbind('mouseenter').unbind('mouseleave').unbind('mouseover').unbind('mouseout');
	$('#menuOpener').stop(true).hide().css({opacity:'0'});
	var logoData = $('#logo').data('logoData');
	clearTimeout(subMenuTimer);
	$('#mainmenu ul.menu > li > a span').css('top','75px');
	$('#mainmenu ul.menu > li ul li a span').css('top','0px');
	$('#menu-container').stop(true).animate({left:0}, 600, 'easeOutExpo');

	$('#logo img').stop(true).animate({width:(logoData.width), height:(logoData.height), opacity:'1'}, 600, 'easeOutBack', function(){
		$('ul.menu > li').each(function(i,el){
			$(el).find('> a > span').stop(true).delay(100*i).animate({top:0}, 600,'easeOutBack');
			
			if(!mobileDevice)
			{
				$(el).find('> a').hover(function(event){
					$(this).parent().find('ul').stop().show().animate({opacity:'1'}, 600, 'easeOutExpo');				
					clearTimeout(subMenuTimer);
					closeSubMenu($(this).parent().attr('id')); 
					menuItemOver(this);
				}, function(){
					if(!mobileDevice)
						subMenuTimer = setTimeout(closeSubMenu, menuTime);
					menuItemOut(this);
				});
			}else{
				$(el).find('> a').click(function(event){
					$(this).parent().find('ul').stop().show().animate({opacity:'1'}, 600, 'easeOutExpo');
					closeSubMenu($(this).parent().attr('id')); 
					menuItemOver(this);
				});
			}
		});
	});
	
	$('#mainmenu ul li ul li a').each(function(i,el){
		
		if(!mobileDevice)
		{
			$(this).hover(function(event){
				clearTimeout(subMenuTimer);
				$(this).closest('.sub-menu').parent().addClass('active');
				menuItemOver(this);
				closeSubMenu($(this).closest('.sub-menu').parent().attr('id'));
			}, function(){
				$(this).closest('.sub-menu').parent().removeClass('active');
				$(this).parent().parent().parent().removeClass('active');
				menuItemOut(this);
				subMenuTimer = setTimeout(closeSubMenu, menuTime);
			});
		}else{
			$(this).click(function(event){
				$(this).closest('.sub-menu').parent().addClass('active');
				menuItemOver(this);
				closeSubMenu($(this).closest('.sub-menu').parent().attr('id'));
			});
		}
	});
	
}

function setCaptionPosition(){
	if(showBgCaption)
	{
		var posTop = $('#bgImages .active').position().top+$('#bgImages').position().top-10;
		var maxTop = $('#content').height()-$('#bgText').height()-20;
		posTop = (posTop>maxTop)?maxTop:posTop;
		posTop = (posTop<-20)?-20:posTop;
		$('#bgText').stop().animate({top:posTop, opacity:'1'}, 500, 'easeOutQuad');
	}else{
		$('#bgText').stop().animate({opacity:'0'}, 500, 'easeOutQuad');
	}
}

// Set Content Margin according to Scroll
function setContentMargin(){
	//
}

// Play Background Audio
function playAudio(path){
	if(audioSupport){
		var isPlaying = !myAudio.paused;
		var canPlayMp3 = !!myAudio.canPlayType && "" != myAudio.canPlayType('audio/mpeg');
		var canPlayOgg = !!myAudio.canPlayType && "" != myAudio.canPlayType('audio/ogg; codecs="vorbis"');
		if(canPlayMp3)
			myAudio.src = path+'.mp3';
		else if(canPlayOgg)
			myAudio.src = path+'.ogg';
			

		myAudio.addEventListener('ended', function() {
			if(loop){
			this.currentTime = 0;
			this.play();
			}else{
				$('#audioControls .next').trigger('click');
			}
		}, false);
		
		if(autoPlay || isPlaying)
		{
			myAudio.play();
			$('#audioControls .pause').css('display','block');
			$('#audioControls .play').css('display','none');
		}else{
			$('#audioControls .play').css('display','block');
			$('#audioControls .pause').css('display','none');
		}
	}
}

// Randoma string generator
function randomString(size) {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var randomstring = '';
	for (var i=0; i<size; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}
