//callout jquery plugin
// JavaScript Document

	$.fn.calloutFN = function(){
		return this.each(function(){
			var $callout = $(this);
			var $title = $('> a > span.title', $callout);
			var $tag = $('> a > span.subtitle', $callout);
			var tag_height = ($tag.outerHeight() == null ? 0 : $tag.outerHeight());
			var title_height = $title.outerHeight();
			var callout_height = $callout.outerHeight();
			var available_space = callout_height - title_height;
			
			$tag.toggle();
			
			/*console.log('Callout Height: ' + callout_height);
			console.log('Title Height: ' + title_height);
			console.log('Tag Height: ' + tag_height);
			console.log('Available Space: ' + available_space);*/
			
			if($tag.length > 0){
				$callout.hover(function(){
					if(tag_height > available_space){
						$title.slideToggle();
					}
					$tag.slideToggle();
				},
				function(){
					if(tag_height > available_space){
						$title.stop(true, true).slideToggle();
					}
					$tag.stop(true, true).slideToggle();
				});
			}
		});
	}

$(function(){
	$('.callout').calloutFN();
});

var menu_top = "88px";
var open_menu = "";
var timer = null;

function hideall() {
	$("#popupabout").hide();
	$("#popupproducts").hide();
	$("#popupfabric").hide();
	$("#popupnews").hide();
	$("#popupcommunity").hide();
	$("#popuptrade").hide();
	$("#popups").hide();
}

function unpop_menu(menu) {
	//Clear the tracked open menu to test if we're still on a part of it
	open_menu = "";
	
	//Wait a little bit to see if we're still in a part of the menu
	timer = setTimeout(function() {
		//If we're still in a part of the open menu, open_menu will have been changed to non-empty string value
		//So leave the menu open
		if (menu == open_menu)
		{
			return false;
		}
		
		//Otherwise, let's close that menu
		close_menu(menu);
		open_menu = "";
		
	}, 10);
}

function close_menu(menu) {
	//clearTimeout(timer);
	
	//Nothing fancy, let's just close the menu
	var menupopup = "#popup" + menu;
	var menubutton = "#a_" + menu;
	
	$(menupopup).hide();
	//$("#popups").hide();
	$(menubutton).removeClass("menu_btn_active");
}

function pop_menu(menu) {
	//If we're still in a part of the open menu, leave it open
	if (menu == open_menu) {
		return false;
	}
	
	//If there's a previously unclosed menu open for some reason, just close it
	if (open_menu != "") {
		close_menu(open_menu);
	}
	
	//Track the currently open menu
	open_menu = menu;
	
	var menupopup = "#popup" + menu;
	var menubutton = "#a_" + menu;
	
	//Close other menus if they are still open from a bug
	// Not this -> hideall();
	
	//Bring out the group of popups
	$("#popups").show();
	
	//Show the specific menu popup
	//JQuery effects can be used here for menu effects
	//Or simply use .show()
	//$(menupopup).slideDown("fast");
	$(menupopup).show();
	
	//Make the menu button (link) show an active style while in the menu
	$(menubutton).addClass("menu_btn_active");
}

//function locale_drop() {
//	$("#lang_select").slideToggle(1000);

	//Prevent href from being followed in links, if Javascript enabled
//	return false;
//}


//function rand_quote() {
//	var randomquotes=["", "", "", "", "", "", "", "", "", "", ""];
//	
//	var quote = randomquotes[Math.floor(Math.random()*randomquotes.length)];
//	
//	document.write(quote);
//}

function rand_quote() {
	var randomquotes=["&ldquo;My solo journey to the magnetic North Pole was like life itself. We all have our metaphorical North Poles. We can reach them just as I did, one step at a time, never giving up on ourselves.&rdquo;", "Take a visionary look beyond the norm, set your goals with a positive commitment to win.", "Plan in detail before taking the first step. A goal without a plan is only a dream.", "Perseverance overcomes problems, obstacles and the fear of failure. Sometimes it's one day or one step at a time.", "Quitting is not an option.", "Work in partnership with team members to shape success and lead with confidence.", "Age is never a barrier to your ultimate success.", "No matter how high your mountain - no matter how difficult the climb, never give up.", "Set achievable goals, plan for success, be persistent.", "Set your horizons to infinity.", "Never look back at failure, always look ahead to success."];
	
	var quote = randomquotes[Math.floor(Math.random()*randomquotes.length)];
	
	document.write(quote);
}

function rand_quote_img() {
	var image = '<img src="/images/quotes/' + (Math.floor(Math.random()*13) + 1) + '.gif" />';
	
	document.write(image);
}

$(document).ready(function () {
    if ($.browser.msie && $.browser.version < 10) { // am I a hopeless romantic for assuming that IE10 will support it?
        $('#popupproducts > ul').easyListSplitter({ colNumber: 2, direction: 'horizontal' });
        $('#popupfabric > ul').easyListSplitter({ colNumber: 2 });
        $('#popupnews > ul').easyListSplitter();
    }
    //Hide the popup menus on load
    hideall();

    //Hide the language selection on load
    //$("#lang_select").hide();

    /* --- Register events --- */
    //$("#lang_switch").click(locale_drop);


    //Products popup
    $(".aa_products").hover(function () {
        pop_menu("products");

    }, function () {
        unpop_menu("products");

    });
    //Fabric popup
    $(".aa_fabric").hover(function () {
        pop_menu("fabric");

    }, function () {
        unpop_menu("fabric");

    });

    //News popup
    $(".aa_news").hover(function () {
        pop_menu("news");

    }, function () {
        unpop_menu("news");

    });

});
