var topSlidingWindowVisible=false;
var mouseOverMenu=false;
var windowHeight=0;
var activeButton=null;

function loadOption(button){
	var iframe;
	if(button=='login'){
		iframe="login.html";
	}else if(button=='new'){
		iframe="newaccount.html";
	}else if(button=='iconEmail'){
		iframe="contact.html";
	}else if(button=='iconFacebook'){
		iframe="facebook.html";
	}else if(button=='iconTwitter'){
	
		iframe="twitter.html";
	}else if(button=='iconRSS'){
		iframe="rss.html";
	}
	
	$('#topSlidingWindow').html('<iframe id="topOptionsWindow" frameborder="0" height="100%" width="100%" scrolling="no" src="'+iframe+'">Your browser is not suitable to view this webpage.</iframe>');
	
	$('iframe#topOptionsWindow').load(function() 
    {
       fadeMenu('out',button);
    })
}

function setUpWindow(button){
	if(button=='login'){
		if(!userRegistered()){
			windowHeight=275;
		}else{
			logoutUser();
			return;
		}
		
	}else if(button=='new'){
		windowHeight=400;
	}else if(button=='iconEmail'){
		windowHeight=320;
	}else if(button=='iconFacebook'){
		windowHeight=170;
	}else if(button=='iconTwitter'){
		windowHeight=170;
	}else if(button=='iconRSS'){
		windowHeight=200;
	}
	
	$('#topSlidingWindow').css('height',windowHeight+'px');
	$('#topSlidingWindow').css('top',-windowHeight+'px');
		
	loadOption(button);
}

//hide left menu
function fadeMenu(type,button){
	if(type=='in'){
		$('#leftTopMenu').fadeIn(1500)
	}else{
		$('#leftTopMenu').fadeOut('fast',function(){
			open(button);
		});
	}
}

function open(button){
	//event.stopPropagation();			
	$('#topSlidingWindow').animate({
			"top": '+='+windowHeight
		},"slow");
	$('#mainContent').animate({
			"top": '+='+windowHeight
		},"slow",openAnimationEnded(button));
}

function openAnimationEnded(button){
	topSlidingWindowVisible=true;
	mouseOverMenu=true;
	activeButton=button;
}

function close(button){
	if(!button){
		$('#topSlidingWindow').animate({
				"top": '-='+windowHeight
			},"slow");
		$('#mainContent').animate({
				"top": '-='+windowHeight
			},"slow",closeAnimationEnded());
	}else{
		$('#topSlidingWindow').animate({
				"top": '-='+windowHeight
			},"slow");
		$('#mainContent').animate({
				"top": '-='+windowHeight
			},"slow",closeAnimationEnded(button));
	}
}

function closeAnimationEnded(button){	
	topSlidingWindowVisible=false;
	mouseOverMenu=false;
	windowHeight=0;
	activeButton=null;
	if(button){
		setUpWindow(button);
	}
	fadeMenu('in',button);
	
	if(refresh){
		refresh=false;
		window.location.reload();
	}
}

function initNewWindow(button){
	if(!topSlidingWindowVisible){
		setUpWindow(button);
	}else{
		if(button!=activeButton && activeButton!='iconEmail'){
			close(null);
		}
	}
}

$(document).ready(function(){
	$('.socialNetworkIcon').hover(
		function(){	
			$(this).css("cursor","pointer");
			$('#'+this.id).addClass('iconMouseOver')
		},
		function(){
			$('#'+this.id).removeClass('iconMouseOver')
		}
	);
	
	$('.socialNetworkIcon').click(function(){
		initNewWindow(this.id);
	});
	
	
	$('.topMenuButton').hover(
		function(){	
			mouseOverMenu=true;
			$(this).css("cursor","pointer");
			$('#'+this.id).addClass('buttonMouseOver')
		},
		function(){
			mouseOverMenu=false;
			$('#'+this.id).removeClass('buttonMouseOver')
		}
	);
	
	$('#rightTopMenu>.topMenuButton').click(function(){
		if(this.id!="socialNetworking"){		
			initNewWindow(this.id);
		}	
	});
	
	
	//close menu on click outside
	$('#topSlidingWindow').hover(
		function(){	
			mouseOverMenu=true;
		},
		function(){
			mouseOverMenu=false;
		}
	);
	
	$('body').click(function(){
	//alert(mouseOverMenu)
		if(!mouseOverMenu && topSlidingWindowVisible){
			close(null);
			activeButton=null;
		}		
	})
	


})

