/*global $, jQuery, window, document */

jQuery.noConflict();

/* jQuery reverse() plugin/function */
jQuery.fn.reverse = function() {
	return this.pushStack(this.get().reverse(), arguments);
};

function change_regions () {
	window.location.href = jQuery(this).attr('value');
}

function set_current_nav () {
	var set = false;
	var checker = function () {
		if (!set && window.location.pathname.match(jQuery(this).attr('href'))) {
			jQuery(this).addClass('page_item_current');
			set = true;
		}
	};
	
	if(jQuery('body.support').length < 1) {
		jQuery('#prim-nav a').reverse().each(checker);	
		set = false;
		jQuery('#sub-nav a').reverse().each(checker);
	};
}


function toggle_item () {
    var ps = jQuery(this).find('p');
    if (ps.eq(0).css('display') == 'none') {
        jQuery(this).find('p').fadeIn(600);        
    } else {
        jQuery(this).find('p').fadeOut(400);                
    }
}

function setup_collapsible_list () {
    var items = jQuery(this).find('.collapse-item');
    items.click(toggle_item);
    items.find('p').hide();
}


// VIDEO FUNCS

function vidTags(url, width, height) {
    return '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" ' +
	'codebase="http://www.apple.com/qtactivex/qtplugin.cab" ' +
	'width="'+ width +'" height="'+ height +'" id="vid-obj">\n' +
	'<param name="src" value="'+ url +'" id="ie_src"/>\n' +
	'<param name="controller" value="true" />\n' +
	'<param name="autoplay" value="true" />\n' +
	'<!--[if !IE]>-->\n' +
	'<object type="video/quicktime" data="'+ url +'" ' +
	' width="'+ width +'" height="'+ height +'" id="vid-obj2">\n' +
	'<param name="autoplay" value="true" />\n' +
	'<param name="controller" value="true" />\n' +
	'<p style="padding: 1em; text-align: center; background-color: #fcc;">You need QuickTime in order to view this content.</p>\n' +
	'</object>\n' +
	'<!--<![endif]-->\n' +
	'</object>\n';
}

function getVid () {
	try {
		// try the outer object first, according to spec
		if (!document.getElementById('vid-obj').GetURL) {
			var isIE = false;
			return document.getElementById('vid-obj2');
		} else {
			return document.getElementById('vid-obj');
		}
	} catch (e) {
		// we're in IE; the exception seems to be an IE bug.
		return document.getElementById('vid-obj');
	}
}

function setTitle (text) {
	var vidTitle = document.getElementById('video-title');
	while (vidTitle.firstChild) {
		vidTitle.removeChild(vidTitle.firstChild);
	}
	vidTitle.text = "";
	vidTitle.appendChild(document.createTextNode(text));		
}

function showVid (caller) {
	var currentVid = getVid();
	
	if (caller.href == currentVid.GetURL()) {
		currentVid.Play();
		return false;
	}
	currentVid.Stop();
	currentVid.SetURL(caller.href);
	
	document.location.hash = "menu";
	setTitle(caller.title);
	return false;
}

function showVidURL (url) {
	var currentVid = getVid();
	
	if (url == currentVid.GetURL()) {
		currentVid.Play();
		return false;
	}
	currentVid.Stop();
	currentVid.SetURL(url);
	
    // document.location.hash = "menu";
    // setTitle(caller.title);
	return false;    
}

// END VIDEO FUNCS


// dom-ready
function dom_ready () {
	jQuery('#countryselector select').change(change_regions);
	var s = jQuery('#countryselector input');
	s.hide();
	
	set_current_nav();
	
	// jQuery('.collapsible-list').each(setup_collapsible_list);
	function positionOnMouse (elem) {
		return function (e) {
			elem.css({top: e.clientY + 20, left: e.clientX - elem.width() + 100});
		};
	}

	var helpDisappearTimer = null;
	jQuery('#info .icon').mouseover(function (e) {
        jQuery(this).css('cursor', 'pointer');
		window.clearTimeout(helpDisappearTimer);
		var dockHelp = jQuery('#info-description');
		jQuery(window).mousemove(positionOnMouse(dockHelp));
		dockHelp.fadeIn();
	}).mouseout(function (e) {
		jQuery(window).unbind('mousemove');
		helpDisappearTimer = window.setTimeout(function () {
			jQuery('#info-description').fadeOut(400, function () {});
		}, 500);
	});
	
	jQuery('#info a').hover(function() {
	   jQuery('#info').css('background-position', '0px -49px');
	}, function() {
	   jQuery('#info').css('background-position', '0px 0px');
	});
}

jQuery(dom_ready);
// sometimes dom.ready doesn't fire properly; we force it again here
// jQuery(window).load(dom_ready);

