
skype_users = new Array("dani.ganick", "scott.pattenden", "rhysajcarter");
var skype_state = false;
var i=0;

$(document).ready(function() {
	
	$(".thumb a").lightbox();
	
	$(".navigation li a").mouseover(function(event){
		
		array_name = "sub_" + $(this).parent().parent().attr('class');
		if(typeof(window[array_name]) == "undefined"){
			window[array_name] = new Array();
			window["x"] = $(".navigation li").find('.submenu');
		}

		if($(this).attr('class') == "has-submenu"){
			//$(this).children('.submenu').show();
			
			
			
			now = $(this).parent().children('.submenu').html();
		} else {
			now = "";
		}
		
		window["x"].each(function(i){
			window[array_name][i] = $(this);
			if(typeof(window[array_name + i]) != "undefined"){
				clearInterval(window[array_name + i]);
				window[array_name + i] = undefined;
			}
			if($(this).html() != now){
				if(typeof(window[array_name + i]) == "undefined"){
				window[array_name + i] = setInterval(function(){delay_hide_menu(i)}, 500);	
				}
			} else {
				$(this).parent().addClass("hover");
				$(this).css({"zIndex": 200, "margin-left": ((($(this).parent().children('a').width() + 60) / 100) * 89) - 23});
				//$(this).fadeIn();
				
				if(typeof(window[array_name + i + '_in']) == "undefined"){
				window[array_name + i + '_in'] = setInterval(function(){delay_show_menu(i)}, 250);	
			}
				
			}
		})
		
	});
	
	$(".navigation .submenu").mouseenter(function(event){
		
		now = $(this).html();
		window["x"].each(function(i){
			if(typeof(window["sub_navigation" + i]) != "undefined"){
			if($(this).html() == now){
				clearInterval(window["sub_navigation" + i]);
				window["sub_navigation" + i] = undefined;
			}
			}
		})

	});
	
	$(".navigation .submenu").mouseleave(function(event){
		
		now = $(this).html();
		window["x"].each(function(i){
			if($(this).html() == now){
				clearInterval(window["sub_navigation" + i]);
				window["sub_navigation" + i] = undefined;
			}
			if(typeof(window[array_name + i]) == "undefined"){
				window["sub_navigation" + i] = setInterval(function(){delay_hide_menu(i)}, 500);	
			}
		})
	});
	
	$(".navigation li a").mouseout(function(event){
		$(this).parent().children('.submenu').css({"zIndex": 100});
		array_name = "sub_" + $(this).parent().parent().attr('class');
		if($(this).attr('class') == "has-submenu"){
		x.each(function(i){
			window[array_name][i] = $(this);
			if(typeof(window[array_name + i + '_in']) != "undefined"){
				clearInterval(window[array_name + i + '_in']);
				window[array_name + i + '_in'] = undefined;
			}
			if(typeof(window[array_name + i]) != "undefined"){
				clearInterval(window[array_name + i]);
				window[array_name + i] = undefined;
			}
			if(typeof(window[array_name + i]) == "undefined"){
				window[array_name + i] = setInterval(function(){delay_hide_menu(i)}, 500);	
			}
		})
		}
		
		
	});
	
	$(".navigation .submenu a").unbind('mouseover');
	$(".navigation .submenu a").unbind('mouseout');
	
	function skype_status(id){
		if(skype_state == false){
			$.ajax({
				type: "GET",
				url: "/script/status.php?id=" + id,
				dataType: "xml",
				success: function(xml) {
					$(xml).find('presence').each(function(){
						if($(this).attr('xml:lang')== "en")
							//alert(id.replace(".", "-") + ": " + $(this).text().toLowerCase());
							if($(this).text().toLowerCase() == "online" || $(this).text().toLowerCase() == "away"){
								$('#' + id.replace(".", "-")).attr("src", "/images/icons/skype/" + $(this).text().toLowerCase() + ".png");
							} else {
								$('#' + id.replace(".", "-")).attr("src", "/images/icons/skype/offline.png");
							}
							if(i<(skype_users.length-1)){
								i++
								skype_status(skype_users[i]);
							} else {
								skype_state = true;
							}
					});
				}
			});
		}
	}

	$(".skype").mouseover(function(event){
		skype_status(skype_users[i]);
		$('.skype').css({"background-position": "10px -17px", "color" : "#333333"});
		$('.skype-sub').css({"left": $(this).offset().left - 13});
		$('.skype-sub').fadeIn(200);
		
	});
	
	$("#countries a").mouseover(function(event){
		$('#countries a span').css({"background-position": "100% -11px", "color" : "#333333"});
		$('.countries-sub').css({"left": $(this).offset().left - 22});
		$('.countries-sub').fadeIn(200);
		
	});
	
	$("#countries a span").mouseover(function(event){
		if(typeof(sub_delayhide) != "undefined"){
			clearInterval(sub_delayhide);
		}
	});
	
	$(".skype").mouseout(function(event){
		if(typeof(sub_delayhide) != "undefined"){
			clearInterval(sub_delayhide);
		}
		sub_delayhide = setInterval(function(){delay_hide_menu(".skype-sub")}, 1000);
	});
	
	$("#countries a").mouseout(function(event){
		if(typeof(sub_delayhide) != "undefined"){
			clearInterval(sub_delayhide);
		}
		sub_delayhide = setInterval(function(){delay_hide_menu(".countries-sub")}, 1000);
	});
	
	$(".skype-sub").mouseenter(function(event){
		$(this).fadeIn(200);
		if(typeof(sub_delayhide) != "undefined"){
			clearInterval(sub_delayhide);
		}
	});
	
	$(".countries-sub").mouseenter(function(event){
		$(this).fadeIn(200);
		if(typeof(sub_delayhide) != "undefined"){
			clearInterval(sub_delayhide);
		}
	});
	
	$(".skype-sub").mouseleave(function(event){
		if(typeof(sub_delayhide) != "undefined"){
			clearInterval(sub_delayhide);
		}
		sub_delayhide = setInterval(function(){delay_hide_menu(".skype-sub")}, 1000);
	});
	
	$(".countries-sub").mouseleave(function(event){
		if(typeof(sub_delayhide) != "undefined"){
			clearInterval(sub_delayhide);
		}
		sub_delayhide = setInterval(function(){delay_hide_menu(".countries-sub")}, 1000);
	});
	
	$("#countries a").click(function(event){
		event.preventDefault();
	});
	
});

function delay_show_menu(element){
	
	clearInterval(window["sub_navigation" + element + '_in']);
	window["sub_navigation"][element].fadeIn();
	window["sub_navigation" + element + '_in'] = undefined;
	
	/*
	.children('.submenu').show();
	
	if(isNaN(element) == true){
		clearInterval(sub_delayhide);
		$(element).fadeOut(200);
	} else {
		
		//while (typeof(window["sub_navigation" + element]) != "undefined"){
		clearInterval(window["sub_navigation" + element]);
		window["sub_navigation" + element] = undefined;
		//}
		window["sub_navigation"][element].fadeOut();
		$(window["sub_navigation"][element]).parent().removeClass("hover");
	}
	
	$('.skype').css({"background-position": "10px 2px", "color" : "#a0a0a0"});
	$('#countries a span').css({"background-position": "100% 6px", "color" : "#a0a0a0"});
	*/
}

function delay_hide_menu(element){
	
	if(isNaN(element) == true){
		clearInterval(sub_delayhide);
		$(element).fadeOut(200);
	} else {
		
		//while (typeof(window["sub_navigation" + element]) != "undefined"){
		clearInterval(window["sub_navigation" + element]);
		window["sub_navigation" + element] = undefined;
		//}
		window["sub_navigation"][element].fadeOut();
		$(window["sub_navigation"][element]).parent().removeClass("hover");
	}
	
	$('.skype').css({"background-position": "10px 2px", "color" : "#a0a0a0"});
	$('#countries a span').css({"background-position": "100% 6px", "color" : "#a0a0a0"});
	
}
