/*Adding top tabs to specify the user, so this is the function + a little setCookie/getCookie functions*/

function setCookie(f, h, b, l, g, j) {
	document.cookie = f + "=" + escape(h) + ((b) ? "; expires=" + b : "") + ((l) ? "; path=" + l : "") + ((g) ? "; domain=" + g : "") + ((j) ? "; secure" : "")
}

function getCookie(f) {
	var h = " " + document.cookie;
	var g = " " + f + "=";
	var j = null;
	var l = 0;
	var b = 0;
	if (h.length > 0) {
		l = h.indexOf(g);
		if (l != -1) {
			l += g.length;
			b = h.indexOf(";", l);
			if (b == -1) {
				b = h.length
			}
			j = unescape(h.substring(l, b))
		}
	}
	return (j)
}

function changeMenu(name){
$(".drop-menu_l").css("background-image", "url('/images/update/menu_bg_l_"+name+".gif')");
		$(".drop-menu_r").css("background-image", "url('/images/update/menu_bg_r_"+name+".gif')");
		$(".drop-menu").css("background-image", "url('/images/update/menu_bg_"+name+".gif')");
var color="#007ed5";
switch (name){
case "private_user":
color="#4F9A00";
break;
case "corp_user":
color="#007ed5";
break;
case "partner":
color="#851597";
break;
}
		$(".drop-menu .family .drop .in").css("color",color);
		$(".drop-menu .family .drop b").css("background",color);

return false
}

function InitUserCategoryMenu() {
/*hook to display top bar*/
console.log(getCookie("userCat"));
$(".user_cat_tabs DIV").removeClass("uc_active");

	var userCat = getCookie("userCat") || "";
	switch (userCat) {
	case "private_user":
		$(".family").hide();
		$(".private_user").show();
		$("BUTTON[name=private_user]").parent().addClass("uc_active");
		changeMenu("private_user");
		break;
	case "corp_user":
		$(".family").hide();
		$(".corp_user").show();
		$("BUTTON[name=corp_user]").parent().addClass("uc_active");
		changeMenu("corp_user");
		break;
	case "partner":
		$(".family").hide();
		$(".partner").show();
		$("BUTTON[name=partner]").parent().addClass("uc_active");

		changeMenu("partner");		
		break;
                default: 
                    $(".family").hide();
                    $(".private_user").show();
			$("BUTTON[name=corp_user]").parent().addClass("uc_active");
		changeMenu("corp_user");
		break;
                    
	}
        
	$(".user_cat_tabs BUTTON").unbind("click").bind("click", function(a, b, c) {
		$(".user_cat_tabs DIV").removeClass("uc_active");
		$(this).parent().addClass("uc_active");
		$(".family").hide();
		$("." + this.name).show();
		setCookie("userCat", this.name,0,'/','.gdemoi.ru');
		changeMenu(this.name);
$(".drop").hide();
	});
	return false
}



/**
 * Nucleo (X)HTML/CSS Framework
 *
 * @copyright       Copyright (c) Sergey Gogolev
 * @link            http://css.softprojects.ru/
 * @license    		CC-A-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0/)
 * @version         0.2.3
 */

jQuery(document).ready(function() {
	CMenu.init();
	CSmallPopup.init();
	InitUserCategoryMenu();
});

/**
 *  SmallPopup namespace 
 */

var CSmallPopup = {
	version: '0.0.2'

	,
	init: function() {

		var links = jQuery('a.small-popup-link');

		var popups = jQuery('div.small-popup');

		links.unbind('click').bind('click', function() {
			var rel = 'div.small-popup-' + jQuery(this).attr('rel');
			if (jQuery(rel + ':visible').size() > 0) {
				jQuery(rel).hide();
			}
			else {
				jQuery(rel).fadeIn('fast');
			}
			return false;
		});

		popups.find('input.close').unbind('click').bind('click', function() {
			jQuery(this).parents('div.small-popup').fadeOut('slow');
			return false;
		});

		popups.find('input.text').each(function() {
			var def = this.value;
			jQuery(this).unbind('focus').bind('focus', function() {
				if (this.value === def) this.value = '';
				return false;
			});
			jQuery(this).unbind('blur').bind('blur', function() {
				if (this.value === '') this.value = def;
				return false;
			});
		});

		return true;
	}

}

/**
 *  Menu namespace 
 */

var CMenu = {
	version: '0.0.2'

	,
	timeoutHandler: 0,
	timeout: 1000

	,
	init: function() {

		var menu = jQuery('#menu');
		var items = menu.find('span.link');
		var subs = menu.find('div.drop');

		items.bind('mouseover', function() {

			CMenu.clear();

			items.removeClass('active');
			jQuery(this).addClass('active');

			var sub = jQuery('#' + this.id + '-submenu');

			if (this.id === 'family') sub.css('left', '-' + Math.floor(sub.width() / 2 - 115) + 'px');

			subs.filter('[id!=' + this.id + '-submenu]').hide();

			if (sub.css('display') == 'none') sub.fadeIn('fast');
		});

		items.bind('mouseout', function() {
			var item = this;
			CMenu.timeoutHandler = setTimeout(function() {
				jQuery('#' + item.id + '-submenu').fadeOut('fast');
				jQuery('#' + item.id).removeClass('active');
			}, CMenu.timeout);
		});

		subs.bind('mouseout', function() {
			var item = this;
			CMenu.timeoutHandler = setTimeout(function() {
				jQuery('#' + item.id).fadeOut('fast');
				jQuery('#' + item.id.replace('-submenu', '')).removeClass('active');
			}, CMenu.timeout);
		});

		subs.bind('mouseover', CMenu.clear);
		subs.find('div').bind('mouseover', CMenu.clear);
		subs.find('a').bind('mouseover', CMenu.clear);

		return true;
	}

	,
	clear: function() {
		clearTimeout(CMenu.timeoutHandler);
	}
}
