/*------------------------------------------------------[ popup menu ]---------------------------------------------------*/
function showSubmenu(elemId) {
	$('#'+elemId+' .subnavigation').show(); //.fadeIn("fast");
//	$('#'+elemId+' img.editprofile_popup').attr('src', document.location.protocol+'//'+siteTopDomain+'/img/opensubmenu_show.gif');
	
	$('#'+elemId).addClass('overDelay');
}
function hideSubmenu(elemId) {
	if (mouseOverMenu != elemId) {
		$('#'+elemId+' .subnavigation').hide(); //.fadeOut("fast");
//		$('#'+elemId+' img.editprofile_popup').attr('src', document.location.protocol+'//'+siteTopDomain+'/img/opensubmenu.gif');
		
		$('#'+elemId).removeClass('overDelay');
	}
}
function toggleSubmenuElem(elemId) {
	if ($('#'+elemId+' .subnavigation').is(':visible')) {
		$('#'+elemId+' .subnavigation').hide();
//		$('#'+elemId+' img.editprofile_popup').attr('src', document.location.protocol+'//'+siteTopDomain+'/img/opensubmenu.gif');
	} else {
		$('#'+elemId+' .subnavigation').show();
//		$('#'+elemId+' img.editprofile_popup').attr('src', document.location.protocol+'//'+siteTopDomain+'/img/opensubmenu_over.gif');
	}
}
/*
function changeSubmenuIcon(imageSelector, imageSrc) {
	var img = $(imageSelector);
	if (imageSrc == 'opensubmenu_over.gif') {
		if (img.attr('src') == document.location.protocol+'//'+siteTopDomain+'/img/opensubmenu_show.gif') {
			return false;
		}
		img.attr('src', document.location.protocol+'//'+siteTopDomain+'/img/opensubmenu_over.gif');
		return true;
	}
	img.attr('src', document.location.protocol+'//'+siteTopDomain+'/img/'+imageSrc);
	return true;
}
*/
function addSubmenuHover(elemId) {
	$('#'+elemId).bind('mouseenter', function(){
		mouseOverMenu = elemId;
		showSubmenu(elemId);
	}).bind('mouseleave',function(){
		mouseOverMenu = '';

		setTimeout("hideSubmenu('"+elemId+"')", 500);
	});
	/*
	.bind('click', function(){
		toggleSubmenuElem(elemId);
	});
	
	$('#'+elemId).bind('mouseenter', function(){
		mouseOverMenu = elemId;
		
		//dont't switch from white icon
		setTimeout("changeSubmenuIcon('#"+elemId+" img.editprofile_popup', 'opensubmenu_over.gif')", 30);
		//$('#'+elemId+' img.editprofile_popup').attr('src', document.location.protocol+'//'+siteTopDomain+'/img/opensubmenu_over.gif');
		
	})
	*/
}

var mouseOverMenu;
$('#navigation').ready(function(){
	addSubmenuHover('gallery');
	addSubmenuHover('forums');
	addSubmenuHover('users');
	addSubmenuHover('more');
	addSubmenuHover('jobs');
	addSubmenuHover('challenge');
});

/*--------------------------------------------------*/

$('#editprofile_li').ready(function(){
	
	$('#editprofile_li img.editprofile_popup').bind('mouseenter', function(){
		mouseOverMenu = 'editprofile_li';
		$('div.editsubmenu').show();
	}).bind('click', function(){
		$('div.editsubmenu').toggle();
	});
	
	$('#editprofile_li').bind('mouseenter', function(){
		mouseOverMenu = 'editprofile_li';
	}).bind('mouseleave',function(){
		mouseOverMenu = '';
		
		setTimeout("if(mouseOverMenu != 'editprofile_li'){$('div.editsubmenu').hide();}", 500);
	});
	
	$('div.editsubmenu').bind('mouseenter', function(){
		mouseOverMenu = 'editprofile_li';
	}).bind('mouseleave',function(){
		mouseOverMenu = '';
		
		setTimeout("if(mouseOverMenu != 'editprofile_li'){$('div.editsubmenu').hide();}", 500);
	});
	
});

/* -----------------------------[ XPath ]------------------------------------------------------- */

function getXPath(node, path) {
	path = path || [];
	if(node.parentNode) {
	    path = getXPath(node.parentNode, path);
	}

	if(node.previousSibling) {
        var count = 1;
        var sibling = node.previousSibling
        do {
            if(sibling.nodeType == 1 && sibling.nodeName == node.nodeName) {count++;}
            sibling = sibling.previousSibling;
        } while(sibling);
        if(count == 1) {count = null;}
	} else if(node.nextSibling) {
        var sibling = node.nextSibling;
        do {
            if (sibling.nodeType == 1 && sibling.nodeName == node.nodeName) {
                var count = 1;
                sibling = null;
            } else {
                var count = null;
                sibling = sibling.previousSibling;
            }
        } while(sibling);
	}

	if(node.nodeType == 1) {
	    path.push(node.nodeName.toLowerCase() + (node.id ? "[@id='"+node.id+"']" : count > 0 ? "["+count+"]" : ''));
	}
	return path;
}

$.fn.XPathSelector = function() {
	var self = this[0];
	var XPath = getXPath(self);

	return $.selectorByXPath(XPath);
};

$.selectorByXPath = function(XPath) {
	if (typeof XPath == 'string') {
		XPath = XPath.split('/').clean('');
	}
	XPath = XPath.join(' > ').replace(new RegExp('\\[@', 'g'), '[');
	var reg = new RegExp('\\[([0-9]+)\\]');
	while (reg.test(XPath)) {
		var num = parseInt( XPath.match(reg)[1] );
		XPath = XPath.replace(new RegExp('\\['+num+'\\]'), ':eq('+(num - 1)+')');
	}
	reg = new RegExp('id\\("([^"]+)"\\)');
	while (reg.test(XPath)) {
		var match = XPath.match(reg);
		XPath = XPath.replace(match[0], '#'+match[1]);
	}

	return XPath;
};

$.findByXPath = function(XPath) {
	var XPathSelector = $.selectorByXPath(XPath);
	return $.find(XPathSelector);
};

/* -----------[ myPopup ]-----------------------------*/
$.fn.myPopup = function(s) {

	s = $.extend({
		'bindClickToggle' : true,
		'onFirstShow' : null,
		'onShow' : null,
		'mouseOverElem': false,
		'openTimeout':0
	}, s || {});

	return this.each(function() {

		var self = this;

		var selfXPathSelector = $(self).XPathSelector();

		s.selfXPathSelector = selfXPathSelector;

		var showedOnce = false;

		function showPopup() {
			if (!s.mouseOverElem) {
				return false;
			}

			for (var XPathSelector in site.myPopup) { //close other popups
				if (XPathSelector != selfXPathSelector) {
					site.myPopup[XPathSelector](); //call close function
				} else {
//					console.log(XPathSelector, XPathSelector != selfXPathSelector);
				}
			}

			var timeout = 0;

			if (typeof s.onFirstShow == 'function' && !showedOnce) {
				showedOnce = true;
				s.onFirstShow(s);
				timeout += 50;
			}

			if (typeof s.onShow == 'function') {
				s.onShow(s);
				timeout += 10;
			}

			//for IE7 fix
			s.popupElem.parents('.over-el:first').css({'z-index':1000, 'border':'none'});

			setTimeout(function(){
				s.popupElem.show(); //.fadeIn("fast");
			}, timeout);
		}
		function hidePopup() {
			if (!s.mouseOverElem) {
				forceHidePopup();
			}
		}
		function forceHidePopup(elem) {
			if (typeof elem == 'object' || typeof elem == 'function') {
				//for IE7 fix
				elem.parents('.over-el:first').css({'z-index':999, 'border':'none'});

				elem.hide(); //.fadeOut("fast");
			} else {
				//for IE7 fix
				s.popupElem.parents('.over-el:first').css({'z-index':999, 'border':'none'});

				s.popupElem.hide(); //.fadeOut("fast");
			}
		}
		function togglePopup() {
			if (s.popupElem.is(':visible')) {
				//for IE7 fix
				s.popupElem.parents('.over-el:first').css({'z-index':999, 'border':'none'});

				s.popupElem.hide();
			} else {
				//for IE7 fix
				s.popupElem.parents('.over-el:first').css({'z-index':1000, 'border':'none'});

				showedOnce = true;

				s.popupElem.show();
			}
		}
		function addHover() {
			if (s.bindClickToggle) {
				$(self).bind('click', function(){
					togglePopup();
				});
			}

			var popupElemXPathSelector = s.popupElem.XPathSelector();

			if ($(self).has(popupElemXPathSelector).length > 0) {
				//bind only self
				var bind = self;
			} else {
				//bind self and popup element
				var bind = s.selfXPathSelector+', '+popupElemXPathSelector;
			}

			$(bind).bind('mouseenter', function(){
				s.mouseOverElem = true;
				setTimeout(function(){showPopup();}, s.openTimeout);
			}).bind('mouseleave',function(){
				s.mouseOverElem = false;
				setTimeout(function(){hidePopup();}, s.timeout);
			});
		}

		//add close function to container -- call when showing other popup
		if (typeof site.myPopup != 'object') {
			site.myPopup = {};
		}
		site.myPopup[selfXPathSelector] = function(){forceHidePopup(s.popupElem);};

		setTimeout(function(){
			addHover();
		}, 200)
	});
};

/*-------------------------------------------------------*/

$(document).ready(function(){
	if (!onReadyUserpicTooltipInUse) {
		onReadyUserpicTooltipInUse = true;
		setTimeout("onReadyUserpicTooltip();onReadyUserpicTooltipInUse = false;", 500);
	}

	onReadyTooltipSpan();

	$('.feedview .switcher, .feedview .switchmenu').bind('mouseenter', function(){
		mouseOverMenu = 'feed_menu';
		$('.feedview .switchmenu').show();
	}).bind('mouseleave',function(){
		mouseOverMenu = '';
		
		setTimeout("if(mouseOverMenu != 'feed_menu'){$('.feedview .switchmenu').hide();}", 500);
	});


	/*$("input, textarea").focus(function() {
		if ($(this).css("background-color") == 'rgb(242, 249, 251)') { //#F2F9FB
			$(this).css("background-color" ,"#F8FAF0");
		}
	}).blur(function() {
		if ($(this).css("background-color") == 'rgb(248, 250, 240)') { //#F8FAF0
			$(this).css("background-color" ,"#F2F9FB");
		}
	});*/

});

var onReadyUserpicTooltipInUse = false;
function onReadyUserpicTooltip() {
	$('a.userpic').tooltip({
		bodyHandler: function() {
			var img = $(this).find('img');
			var src = img.attr('src');
			src = src.replace('thumb', 'large');
			var username = img.attr('alt');
			var ret = '<img src="'+src+'"><br />'+username;
			
			try {
				if ($.browser.chrome) {
					var longdesc = img.attr('longdesc').replace(/\|/g, '<br />', 'g').replace(/avatar_[0-9]+_/, '');
				} else {
					var longdesc = img.attr('longdesc').replace('|', '<br />', 'g').replace(/avatar_[0-9]+_/, '')
				}
				if (longdesc) {
					ret += '<br />Status: '+longdesc;
				}
			} catch (e) {
				//pass
			}
			
			
			
			return ret;
		},
		track: true,
		delay: 0,
		showURL: false,
		showBody: " - ",
		opacity: 1
	});
}
function onReadyTooltipSpan() {
	$('span.tooltip_span').tooltip({
		bodyHandler: function() {
			return $(this).find('span.tooltip_content').html();
		},
		track: true,
		delay: 0,
		showBody: " - ",
		opacity: 0.90
	});
}
