var $j = jQuery.noConflict();

var menu_left_last=0;

function left_menu_fix() {		

	if (menu_left_last==2){
		
		if (document.getElementById("menu_lastLevel2").className=="selected") {

			document.getElementById("menu_lastLevel2").className = "lastLevel2Selected";
		} else {
			document.getElementById("menu_lastLevel2").className = "lastLevel2";

		}
    }
}

function f_length(elId){
	return document.getElementById(elId).childNodes.length;
}

function menu(){
	$j("#menu ul li").hover(function() {
		$j(this).css({cursor: "pointer"});
		$j(this).find('ul:first').css({visibility: "visible", display: "none"}).slideDown(400);
	}, function() {
		$j(this).find('ul:first').css({visibility: "hidden"});
	});	
}

function closePopup() {
	$j('#overlay').css('display','none');
	$j('#identification_form').remove();
}

function refreshSharepointSrc() {
	$j('#sharepoint_news').attr('src', $j('#sharepoint_news').attr('src'));
}

function embedPlayer(file, image) {
	var so = new SWFObject('/_includes/players/flvplayer.swf','player2','295','197','9');
	
    so.addParam('allowfullscreen','true');
    so.addParam('allowscriptaccess','always');
    so.addVariable('file', file);
    so.addVariable('image', image);
    so.write('video');
	$j('#video embed, #video object').css('visibility', 'visible');
}

function goToSharepoint() {
	if (parent.location.href.indexOf("ref=sharepoint") > 0) {
		$j('#sharepoint_iframe').load(function() {
			parent.location.href = "http://sharepoint.eumwa.org";
		});
		$j('#sharepoint_iframe').attr('src', $j('#sharepoint_iframe').attr('dest'));
	} else if (parent.location.href.indexOf("ref=amember") > 0) {
		$j('#sharepoint_iframe').load(function() {
			parent.location.href = "http://www.eumwa.org/en/my-euma/personal-page.html";
		});
		$j('#sharepoint_iframe').attr('src', $j('#sharepoint_iframe').attr('dest'));
	} else {
		$j('#sharepoint_iframe').attr('src', $j('#sharepoint_iframe').attr('dest'));
	}
}

$j(function(){					
	menu();

	switch (pageId) {
		case 47: // index
			$j('#list1a').accordion({fillSpace: true, autoHeight: false});

			imagePreview();

			$j("#moreOptions").click(function(){
				$j('#advanced').toggle();
				$j('#login2').parent().css('overflow', 'visible');
			});
			
			$j("#slider").easySlider();

			$j('.preview').click(function(event) {
				event.preventDefault();
				embedPlayer($j(this).attr('file'), $j(this).attr('image'));
			});
			
			refreshSharepointSrc();
			
			break;
		
		case 171:
		case 178: // knowledge-center
			$j('#list1a').accordion({fillSpace: true, autoHeight: false, active: 1});
		
			$j("#moreOptions").click(function() {
				$j('#advanced2').toggle();
				$j('#login2').parent().css('overflow', 'visible');
			});
			break;				default:			$j('#list1a').accordion({fillSpace: true, autoHeight: false});					$j("#moreOptions").click(function(){				$j('#advanced2').toggle();				$j('#login2').parent().css('overflow', 'visible');			});		
	}
		
	$j("select").uniform();
	
	$j('a').click(function(event) {
		var href = $j(this).attr('href');
		if (/^\/knowledge-center\.*/.test(href)) {
			event.preventDefault();
			$j.post('/knowledge-center/check.php', {filename : href}, function(data) {
				if (data == 'valid') {
					document.location.href = href;
				} else {
					// Display login form
					$j('#overlay').css('display','block');
					
					$j('html').scrollTop(0);
					$j('body').prepend('<div id="identification_form"><iframe src="' + data + '" width="710" height="365" frameborder="0" scrolling="no"></iframe></div>');
				}
			});
		} 
	});
	
	$j('#closeBtn,#overlay').click(function() {
		parent.closePopup();
	});

	goToSharepoint();
});

