var Dom = YAHOO.util.Dom;
var Event = YAHOO.util.Event;
var Anim = YAHOO.util.Anim;

function init() {	
	Dom.setStyle(['site_menu_container', 'special-wrapper'], 'opacity', 0);
	Dom.setStyle(['site_menu_container', 'special-wrapper'], 'visibility', 'visible');

	//Event.addListener(Dom.getElementsByClassName('entry-link'), 'click', clickEntry);
	//Event.addListener(Dom.getElementsByClassName('entry-close'), 'click', clickClose);

Event.addListener('product', 'click', function(e) {
	Event.preventDefault(e);
	alert('We\'re working on it...');
});

	var s1 = new SWFObject("/resources/jw_flv_player/player.swf","ply","200","20","9","#000000");
	s1.addParam("allowfullscreen","false");
	s1.addParam("allowscriptaccess","always");
	s1.addParam("flashvars","file=playlist.xspf&&skin=/resources/jw_flv_player/nacht.swf&autostart=true");
	s1.write("music_container");

	activateLinks(Dom.get('entries-wrapper'));
	
	setTimeout(fadeInMenu, 400);
	
	function fadeInMenu()
	{
		var menuAnim = new Anim('site_menu_container', {
			opacity: { to: 1 }
		}, 4, YAHOO.util.Easing.easeOut);
		menuAnim.onComplete.subscribe(function() {
			setTimeout(fadeInSpecial, 200);
		});
		menuAnim.animate();
	}

	function fadeInSpecial()
	{
		var specialAnim = new Anim('special-wrapper', {
			opacity: { to: 1 }
		}, 4, YAHOO.util.Easing.easeOut);
		specialAnim.animate();
	}


	//Event.addListener('special-wrapper', 'mouseover', function() {
	//	Dom.setStyle('buy-wjs', 'visibility', 'visible');
	//});
	//Event.addListener('special-wrapper', 'mouseout', function() {
	//	Dom.setStyle('buy-wjs', 'visibility', 'hidden');
	//});
}

function activateLinks(parent)
{
	Event.addListener(Dom.getElementsByClassName('entry-link', 'a', parent), 'click', clickEntry);
	Event.addListener(Dom.getElementsByClassName('entry-close', 'a', parent), 'click', clickClose);
	blankWin(parent);
}

function clickEntry(e)
{
	Event.preventDefault(e);
	var entry_id = this.id.replace('entry', '');
	toggleEntry(entry_id, 0);
}

function clickClose(e)
{
	Event.preventDefault(e);
	var entry_id = this.id.replace('close', '');
	toggleEntry(entry_id, 0);
}

function toggleEntry(entry_id, h)
{
	var el = Dom.get('etxt'+entry_id);
	var h = 0;

	if (getH(el) === 0)
	{
		var wrapperEl = Dom.getElementsByClassName('entry-text-wrapper', 'div', el)[0];
		h = getH(wrapperEl);
	}

	var entryAnim = new YAHOO.util.Anim(el, {
		height: { to: h } 
	}, 1.5, YAHOO.util.Easing.easeOut);
	entryAnim.animate();
}

function getH(el)
{
	var region = Dom.getRegion(el);
	return +(region.bottom - region.top);
}

function blankWin(parent)
{
	if (!YAHOO.lang.isObject(parent))
	{
		parent = document;
	}
	var hostname = window.location.hostname;
	hostname = hostname.replace("www.","").toLowerCase();
	var a = parent.getElementsByTagName("a");
	this.check = function(obj)
	{
		var href = obj.href.toLowerCase();
		return (href.indexOf("http://")!=-1 && href.indexOf(hostname)==-1) ? true : false;				
	};
	this.set = function(obj)
	{
		obj.target = '_blank';
		//obj.className = "external";
		Dom.addClass(obj, 'external');
obj.title += ' -- (opens in a new window / tab)';
	};	
	for (var i=0;i<a.length;i++)
	{
		if(check(a[i])) set(a[i]);
	};		
};

/*
function showContent(e)
{
//console.info(this);
	Event.preventDefault(e);
	var cat = this.id;

	if (Dom.hasClass(this, 'cat_link'))
	{
		var hrefArr = this.href.replace(/\/$/, '').split('/').reverse();
		cat = hrefArr[0];
	}
	
	Dom.setStyle(Dom.getElementsByClassName('content_sub_container'), 'display', 'none');
	
	if (Dom.inDocument('content_'+cat))
	{
		Dom.setStyle('content_'+cat, 'display', 'block');
	}
	else
	{
		ajaxManager.processResult = function(o) {
			var newDiv = document.createElement('div');
			newDiv.id = 'content_'+cat;
			newDiv.className = 'content_sub_container';
			newDiv.innerHTML = o.responseText;
			Dom.get('content_container').appendChild(newDiv);

			activateLink(cat);
		};
		ajaxManager.startRequest(this.href);
	}
}
*/
var ajaxManager = {
	handleSuccess:function(o){
		// processResult member
		this.processResult(o);
	},
	handleFailure:function(o){
		// Failure handler
	},
	processResult:function(o){
		// This member is called by handleSuccess
		console.info(o.responseText);
	},
	startRequest:function(url, postVars, callback) {
		if (callback === undefined) {
			var callback = ajaxCallback;
		}
		YAHOO.util.Connect.asyncRequest('POST', url, callback, postVars);
	}
};

var ajaxCallback = {
	success: ajaxManager.handleSuccess,
	failure: ajaxManager.handleFailure,
	scope: ajaxManager,
	argument: []
};

Event.onDOMReady(init);
