var page_lang;

Event.onDOMReady(function() {
	/* Add Archive Layer */
		page_lang = GetMetaContents('DC.Language');
		var elm = "<div id='lightbox-layer-archive' class='lightbox-layer' style='top:519px;'>"
		elm += "<div class='lightbox-header'><img src='../../framework/pub/img/icon-folder-expanded-bw.gif' /> "+(page_lang == 'de' ? 'Archiv' : 'Archive')+"</div><div class='close'><a href='javascript:void(0);'>";
		elm += "<span class='access'>Close Layer</span></a></div>";
		elm += "<div class='layer-content'><div id='layer-content-archive'></div></div></div>";
	
		new Insertion.Before($('footer-position-placeholder'), elm);
	
		var node = $("lightbox-layer-archive");
		var key="lightbox-layer-archive";
		GLOBAL_LAYER_CONTROLLER[key]=new PubLayer(node);
	
	/* Add Country Layer */
		var elm = "<div id='lightbox-layer-country' class='lightbox-layer' style='top:522px; left:300px;'>";
		elm += "<div class='lightbox-header'><img src='../../framework/pub/img/icon-folder-expanded-bw.gif' /> "+(page_lang == 'de' ? 'Landesausgaben' : 'Country versions')+"</div><div class='close'><a href='javascript:void(0);'>";
		elm += "<span class='access'>Close Layer</span></a></div>";
		elm += "<div class='layer-content'><div class='clearfix' style='height:5px;'>&#160;</div><div id='layer-content-country'></div></div></div>";
	
		new Insertion.Before($('footer-position-placeholder'), elm);
	
		var node = $("lightbox-layer-country");
		var key="lightbox-layer-country";
		GLOBAL_LAYER_CONTROLLER[key]=new PubLayer(node);
	
	/* Copy Non-script Archive content into new Layer */
		$('layer-content-archive').update( '<br clear="both" />'+ $$('div.fourth').first().innerHTML );
		$$('div#layer-content-archive p').first().remove();	//remove LinkList-Header
	
	/*Initialize Flash*/
		var coverflow = null;
		coverflow  = new SWFObject("../../pool/coverflow.swf", "Coverflow", "960", "250", "8,0,0,0", "#ffffff");
		coverflow.addParam("quality", "high");
		coverflow.addParam("wmode", "transparent");
		coverflow.addParam("menu", "false");
		coverflow.addParam("swliveconnect", "true");
		coverflow.addParam("allowScriptAccess", "always");
		coverflow.addParam("FlashVars", "jsEnabled=true&imgPath=../../pool/pof/&xmlPath=../../apps/PofList/poflist_"+page_lang+".xml");
		coverflow.write("pof-zone");
	
	/* Adding OrderForm Layer */
		var id = "orderform_pub";
		
		var elm = "<div id='lightbox-layer-"+id+"' class='lightbox-layer layer-orderform'><div class='close'><a href='javascript:void(0);'>";
		elm += "<span class='access'>Close Layer</span></a></div>";
		elm += "<div class='layer-content'><div id='layer-content-"+id+"'></div></div></div>";
	
		new Insertion.Before($('footer-position-placeholder'), elm);
		
		//Adding Hidden Fake-Trigger Link to Page
		var hidden_link = '<a href="../orderform.php?type=pof" id="faketrigger" style="display:none;">FakeTrigger</a>';
		new Insertion.Before($('footer-position-placeholder'), hidden_link);
		
		var node = $("lightbox-layer-" + id);
		var key="lightbox-layer-"+id;
		var trigger = $('faketrigger');
		GLOBAL_LAYER_CONTROLLER[key]=new ExternalLightboxLayer(node,trigger,id);
});

/* START: PubLayer */
var PubLayer = Class.create();

PubLayer.prototype = Object.extend(new Layer, {
	initialize: function(node) {
		this.node = node;
		this.initSuper(node, null);
		
		var closeButton = Helper.getCloseButton(this.node);
		closeButton.observe("click", function(){this.close();}.bindAsEventListener(this));

		if (Info.browser.isIEpre7) {
			this.iframeLining = new IframeLining(this.node);
		}

		//store the listener so it can be accessed by functions add and remove
		this.listener = {'augmentDone' : this.handleOpen.bind(this) };
	},
	open: function() {
		if(HeaderAnimation.diminishable && !HeaderAnimation.augmented) {
			HeaderAnimation.listenerQueue.add(this.listener);
			HeaderAnimation.augment();
		} else {
			Layer.prototype.open.call(this);
		}
	},
	show: function() {
		this.node.setStyle({'display': 'block'});
	},
	hide: function() {
		this.node.setStyle({'display': 'none'});
	},
	beforeOpen: function() {
		//calculate top value of layer
		if(!this.headerHeight) this.headerHeight = $('header-zone').getDimensions().height + $('toolbar-zone').getDimensions().height;

		if(this.iframeLining) this.iframeLining.show();
		return true;
	},
	beforeClose: function() {
		if(this.iframeLining)
			this.iframeLining.hide();
		return true;
	},
	handleOpen: function() {
		HeaderAnimation.listenerQueue.remove(this.listener);
		Layer.prototype.open.call(this);
	}
});
/* End PubLayer */

function OpenPubLayer(where, what) {
	//Get Flash Position
	var flashpos = $('pof-zone').cumulativeOffset();
	$('lightbox-layer-country').setStyle({"top" : (flashpos.top + 210)+'px'});
	$('lightbox-layer-archive').setStyle({"top" : (flashpos.top + 174)+'px'});
	
	//alert("Opening Layer \nX-Pos: " + where + "\nContent:\n"+ what);
	var additinonal_content;
	if(where == 0) {
		GLOBAL_LAYER_CONTROLLER['lightbox-layer-country'].hide();
		GLOBAL_LAYER_CONTROLLER['lightbox-layer-archive'].show();
	} else {
		if(page_lang == "de") {
			additional_content = '<ul class="link"><li><a href="pof_tuerkisch.htm" class="link">Türkische Ausgaben (Türk baski)</a></li></ul><p><br /></p>';
		} else {
			additional_content = '<ul class="link"><li><a href="../publikationen/pof_turkisch.htm" class="link">Turkish Issues (Türk baski)</a></li></ul><p><br /></p>';
		}
		$('layer-content-country').update("<br clear='both' />"+ what + additional_content);
		$('lightbox-layer-country').setStyle({"left" : (where+32)+'px'});
		GLOBAL_LAYER_CONTROLLER['lightbox-layer-archive'].hide();
		GLOBAL_LAYER_CONTROLLER['lightbox-layer-country'].show();
		//change all links so they open in new window
		$$('#layer-content-country div ul li a').each( function(item) {
			item.writeAttribute('target','_blank');
			});
	}
	$$('body').first().observe('mousedown', pubHandleClick);
}

function closeLayer() {
	GLOBAL_LAYER_CONTROLLER['lightbox-layer-country'].hide();
	GLOBAL_LAYER_CONTROLLER['lightbox-layer-archive'].hide();
}

function pubHandleClick(event) {
	var element = event.element();
	while ('BODY' != element.tagName) {
		if(element.identify() == 'lightbox-layer-country' || element.identify() == 'lightbox-layer-archive') return;
		element = element.up();
	}
	$$('body').first().stopObserving('mousedown', pubHandleClick);
	GLOBAL_LAYER_CONTROLLER['lightbox-layer-country'].hide();
	GLOBAL_LAYER_CONTROLLER['lightbox-layer-archive'].hide();
}

function OpenOrderForm() {
	fireEvent( $('faketrigger') , "click");
}

function fireEvent(element,event){
    if (document.createEventObject){
        var evt = document.createEventObject();
        return element.fireEvent('on'+event,evt)
    }
    else{
        var evt = document.createEvent("HTMLEvents");
        evt.initEvent(event, true, true ); // event type,bubbling,cancelable
        return !element.dispatchEvent(evt);
    }
}

function showAnchor(anchorId) {
	window.location = String(window.location).replace(/\#.*$/, "") + "#" + anchorId;
}

function showUrl(url) {
   window.location = url;
}
