if (typeof Cufon != "undefined"){
	Cufon.replace(".officinasansctt, #header-callcenter span, #title h1, #block-login a.register," +
		".block .block-title, #block-user .menu-title, .index-block .title, #login-popup .title," +
		"#login-popup label", { fontFamily: "OfficinaSansCTT" });
	Cufon.replace("#header-menu li.item a.link", { fontFamily: "OfficinaSansCTT", textShadow: "0px 1px rgba(255, 255, 255, 1)", separate: "none" });
	Cufon.replace("#block-login label", { fontFamily: "OfficinaSansCTT", textShadow: "0px 1px rgba(255, 255, 255, 1)" });
	Cufon.replace("#index-page #title h1", { fontFamily: "OfficinaSansCTT", textShadow: "0px -1px rgba(56, 137, 177, 1)", separate: "none" });
	Cufon.replace("#index-page #content A.more, #block-newuser A.login", { fontFamily: "OfficinaSansCTT", hover: true });
	Cufon.replace("#block-signup p", { fontFamily: "OfficinaSansCTT", textShadow: "0px 1px rgba(47, 145, 208, 1)" });
	Cufon.replace("#block-signup p a", { fontFamily: "OfficinaSansCTT", textShadow: "0px 1px rgba(27, 98, 176, 1)", hover: true}); 
}

$(function(){
	
	if (typeof jQuery.fn.dropdown != "undefined"){
		$('.field-select').dropdown();
	}
	
	var langDropdown = $("#language-dropdown > .dropdown");
	langDropdown.click(function(e){
		var el = $(this);
		el.blur();
		if (el.hasClass("dropdown-opened")){
			el.removeClass("dropdown-opened");
			el.next(".dropdown-menu").hide();
		} else {
			el.addClass("dropdown-opened");
			el.next(".dropdown-menu").show();
		}
		return false;
	});

	$("#header-menu .with-submenu").hover(function(){
		$(this).addClass("hover");	
	}, function(){
		$(this).removeClass("hover");
	});
	
	/* qTip */
	
	var qtipCfg = {
		show: "click",
		hide: "click",
		position: {
	      	corner: {
	         	target: "bottomMiddle",
	         	tooltip: "topRight"
	      	}
	   	},
		style: {
		   	name: "cream",
		   	tip: "topRight",
		   	width: {
		   		min: 0,
		   		max: 300
		   	}
	   	},
	   	api: {}
	};
	
	$("a.tooltip[title]").each(function(){
		var el = $(this), title = el.attr("title");
		el.attr("title", "");

		
		if (!el.attr("qtip:hide")) {
		    var div = $("<div>" + title + "</div>"), close = $('<a href="javascript:void(0)" class="tooltip-close">Close</a>');
		    div.prepend(close);
		
		    qtipCfg.content = div;
		    qtipCfg.api.onRender = function(){ el.qtip("api").elements.tip.find('[nodeName="shape"], [nodeName="image"], canvas').remove(); };
		    close.click(function(){ el.qtip("hide") });				
		} else {
		    var div = $("<div>" + title + "</div>");
		    div.prepend(close);
		
		    qtipCfg.content = div;
		}
		
		var maxWidth = el.attr("qtip:width");
		if (maxWidth && maxWidth > 0) {
			qtipCfg.style.width.max = maxWidth;
		}
		var showEvent = el.attr("qtip:show");
		if (showEvent) {
			qtipCfg.show = showEvent;
		}
		
		var hideEvent = el.attr("qtip:hide");
		if (hideEvent) {
			qtipCfg.hide = hideEvent;
		}
		
		
		el.qtip(qtipCfg);
	});
	
	/* Table row expanding */
	
	var expRow = null;
	$(".alpha-filter .table a").click(function(){
		var row = $(this).closest("tr"), show = true;
		if (expRow) {
			expRow.removeClass("expanded");
			if (row.get(0) == expRow.get(0)){ show = false; }
			expRow = null;
		}
		if (show){
			row.addClass("expanded");
			expRow = row;
		}
		return false;
	});
	
	$("#block-newuser a.login").click(function(){
		$("#login-popup").toggle();
		return false;
	});
	
	/* Field labels */
	
	function enchanceFieldLabel(inputEl, labelEl){
		$(inputEl).bind("focus", function(e){
			$(labelEl).hide();
		}).bind("blur", function(){
			if ($.trim(this.value) == ""){
				$(labelEl).show();
			}
		}).blur();
	}
	enchanceFieldLabel("#login-popup input.login", "#login-popup label.login");
	enchanceFieldLabel("#login-popup input.password", "#login-popup label.password");
	
	/* Prevent clicking on links with .disabled class */
	
	$("a").live("click", function(){
		if ($(this).hasClass("disabled")){
			return false;	
		}
	});
	
	/* Other actions */
	
	$(document).click(function(){
		/* Hide laguages dropdown when clicked outside the list */
		if (langDropdown.hasClass("dropdown-opened")){
			langDropdown.click();
		}
	});
});


function showID(id) {
    $("#"+id).show();
}

function hideID(id) {
    $("#"+id).hide();
}

function makeSelected(id) {
    $("#"+id).addClass('selected');
}
function makeDeselected(id) {
    $("#"+id).removeClass('selected');
}

function resizeSelect(width) {
    $('.dropdown-options').css('width', width);
}

function update_charges() {
    $(".charges-type").css('display', 'none');
    $("#"+$("#charge_type").val()).css('display', '');
}
