// (c) Albert Jindra Trebla

var topmenuoffest=0;
var thumbpage=0;
var thumbpages=0;
var thumheight=0;
var thlist;

var accordion;


var sirfwidthmax=424;

/*	
	Original:
	---------
	sIFR 2.0.2 (http://www.mikeindustries.com/sifr/)
	Copyright 2004 - 2006 Mike Davidson, Shaun Inman, Tomas Jogin and Mark Wubben
	sIFR 2.0.2 is licensed under the CC-GNU LGPL <http://creativecommons.org/licenses/LGPL/2.1/>
 
	Adapted:
	--------
	MoosIFR v0.4 rough mootools sIFR conversion
	Copyright 2007 Chris Martin (http://www.redantdesign.com/)
	MoosIFR v0.4 is licensed under the CC-GNU LGPL <http://creativecommons.org/licenses/LGPL/2.1/>
 
	Description:
	------------
	Due to sIFR 2.0.2 having memory leaks I decided to roughly put together a mootools conversion.
	This release is incomplete and only performs the basic functionality of sIFR 2.0.2
	Please feel free to contribute to future releases and PLEASE keep memory leaks in mind.
 
*/
 
var MoosIFR = new Class({
	initialize: function(elements, options) {
		this.setOptions({
			flashsrc: "",
			textcolor: "#000000",
			linkcolor: null,
			hovercolor: null,
			bgcolor: "#FFFFFF",
			paddingtop: 0,
			paddingright: 0,
			paddingbottom: 0,
			paddingleft: 0,
			flashvars: "",
			wmode: "transparent"
		}, options);
 
		if(this.hasFlash() && $$(elements).length > 0) {
			if(this.options.wmode == "transparent"){
				this.options.bgcolor = "transparent";
			}
 
			//$$("html").addClass("sIFR-hasFlash");
 
			$$(elements).each(function(el){
				if(el.hasClass("sIFR-replaced") || el.hasClass("sIFR-alternate")) {
					return;
				}
				var offsets = el.getSize();
				var sWidth = offsets.size.x - this.options.paddingleft - this.options.paddingright;
				var sHeight = offsets.size.y - this.options.paddingtop - this.options.paddingbottom;
 
				/*
				cloning a node using the .clone() method to replace itself causes leaks,
				I chose to make the alternate content span node, then change links directly before flash replacement
				*/
				var alternate = new Element("span",{"class":"sIFR-alternate"}).setHTML(el.innerHTML);
				var links = "";
				$each(el.getElements("a"), function(link, index){ 
					links += "&sifr_url_" + index + "=" + link.getProperty("href");
					link.setProperty("href","asfunction:_root.launchURL," + index);
				});
				//end of link replacement
 
				//if you haven't sucked air through your teeth yet, the next lot of code will make you do so.
				this.flashvars = "txt=" + el.innerHTML.replace(/\+/g, "%2B").replace(/&/g, "%26").replace(/\"/g, "%22");
				if (sWidth>sirfwidthmax) sWidth=sirfwidthmax;
				this.flashvars += "&w=" + sWidth + "&h=" + (sHeight-5);
				this.flashvars += links + "&" + this.options.flashvars;
				$each(this.options, function(v, k){
					if(v && k != "flashvars"){
						this.flashvars += "&" + k + "=" + v;
					}
				}.bind(this));
 
				//change the content to flash stuff
				el.addClass("sIFR-replaced").setHTML(
					'<embed class="sIFR-flash" type="application/x-shockwave-flash" src="' + this.options.flashsrc + '" quality="best" wmode="' + this.options.wmode + '" bgcolor="' +  this.options.bgcolor + '" flashvars="' + this.flashvars + '" width="' + sWidth + '" height="' + sHeight + '" sifr="true"></embed>'
				);
 
				//add the alternate clone
				alternate.injectInside(el);
			}, this);
		}
 
	},
 
	//following function is a copy and paste
	hasFlash: function(){
		/*
		pulled from swfObject (http://blog.deconcept.com/swfobject/), 
		I didn't like the sIFR way of injecting vbscript to detect flash.
		*/
		var PlayerVersion = new flashVersion([0,0,0]);
 
		if(navigator.plugins && navigator.mimeTypes.length){
			var x = navigator.plugins["Shockwave Flash"];
			if(x && x.description) {
				PlayerVersion = new flashVersion(x.description.replace(/([a-zA-Z]|\s)+/, "").replace(/(\s+r|\s+b[0-9]+)/, ".").split("."));
			}
		} else {
			try{
				var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
			}catch(e){
				try {
					var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
					PlayerVersion = new flashVersion([6,0,21]);
					axo.AllowScriptAccess = "always";
				} catch(e) {
					if (PlayerVersion.major == 6) {
						return PlayerVersion;
					}
				}
				try {
					axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
				} catch(e) {}
			}
			if (axo != null) {
				PlayerVersion = new flashVersion(axo.GetVariable("$version").split(" ")[1].split(","));
			}
		}
		return PlayerVersion.major >= 6;
	}
});
 
MoosIFR.implement(new Options);
 
/* also adapted from swfObject (http://blog.deconcept.com/swfobject/) */
var flashVersion = new Class({
	initialize: function(arrVersion){
		this.major = arrVersion[0] != null ? parseInt(arrVersion[0]) : 0;
		this.minor = arrVersion[1] != null ? parseInt(arrVersion[1]) : 0;
		this.rev = arrVersion[2] != null ? parseInt(arrVersion[2]) : 0;
	}
});

var imagesanim=$$('#slideshow img').length;

function fadeimg(inx,iny) {
oldimg=$$('#slideshow img')[inx];
newimg=$$('#slideshow img')[iny];
oldx=oldimg.src;
var marginChange = new Fx.Style(oldimg, 'opacity', {duration:300});
marginChange.start(1, 0).chain(function(){
$$('#slideshow img')[inx].src=newimg.src;
$$('#slideshow img')[iny].src=oldx;
var marginChange2 = new Fx.Style($$('#slideshow img')[inx], 'opacity', {duration:500});
marginChange2.start(0, 1);
});

}




function animmove(z) {
min=766-maxwidth;
s=sliderwrap.getStyle('margin-left').toInt()+z;
if (s>0) s=0;
if (s<min) s= min;

boxfx.start({'margin-left': s });


}


function animbox() {

var widthmask=766;
s=widthmask-destination*160;
//console.log(s+","+destination);

if(s>0) s=0;
boxfx.set({'margin-left': sliderwrap.getStyle('margin-left').toInt() });
boxfx.start({'margin-left': s });

}

var maxwidth,destination,sliderwrap,boxfx,mtimer;

// --- action --------------------------------------
window.addEvent('domready', function(){


sliderwrap=$('slidewrap');

boxfx = sliderwrap.effects({duration: 1000, transition: Fx.Transitions.Quad.easeInOut});


toplinks=$$("#top_menu a");
toplinkscount=toplinks.length;
maxwidth=toplinkscount*160;

var start=0;
indx=1;
toplinks.each(function(el){


box = new Element("div",{"class":"sbox","style":'background-image:url(/css/img/icon0'+indx+'.png);'});
box.innerHTML='<p>'+el.innerHTML+'</p>';

var thbfx = box;
//new Fx.Styles(box, {duration:0, wait:false});
 var dest=indx;
 
box.addEvent('mouseenter', function(){
		/*thbfx.start({
			'background-position': "0 -162px"
		});*/
		
		this.style.backgroundPosition="0 -162px";
		
		el.addClass('hover');
		thbfx.addClass('hover');
	
	});
 
box.addEvent('mouseleave', function(){
		/*thbfx.start({
			'background-position': "0 1px"
		});*/
				this.style.backgroundPosition="0 1px";

		thbfx.removeClass('hover');
		el.removeClass('hover');
	});
	
	if (el.hasClass('active')) { 
	box.style.backgroundPosition="0 -162px";
	start=indx;
	box.addClass('active');
	}
	
	
box.addEvent('click', function(){
window.location.href=el.href;
	});
	
	
el.addEvent('mouseenter', function(){
			destination=dest;
			mtimer = animbox.periodical(100);
			/*thbfx.start({
			'background-position': "0 -162px"
		});*/
		thbfx.style.backgroundPosition="0 -162px";
		el.addClass('hover');
		thbfx.addClass('hover');
		
		
	});
 
el.addEvent('mouseleave', function(){
		/*thbfx.start({
			'background-position': "0 1px"
		});*/
		thbfx.style.backgroundPosition="0 1px";
		thbfx.removeClass('hover');
		el.removeClass('hover');
		
		$clear(mtimer);
	});
	
	


/* thbfx.start({
			'opacity': 1
		});
*/
box.style.backgroundImage = "url(/css/img/icon0" + indx + ".png)";
indx++;
sliderwrap.adopt(box);
//box.inject(sliderwrap);
});

// --- start

if (start>0) { 

destination=start;
//mtimer = 
animbox();//.periodical(100); 
//$clear(mtimer);			
			}


// add arrows

arrleft = new Element("div",{"id":"arrleft"});
arrleft.addEvent('click', function(){
animmove(160);
	});

sliderwrap.adopt(arrleft);

arrright = new Element("div",{"id":"arrright"});
arrright.addEvent('click', function(){
animmove(-160);;
	});

sliderwrap.adopt(arrright);





new MoosIFR("h1", {
		flashsrc: "/sifr/Myriad_Pro_Bold_Cond_It.swf", //font
		textcolor: "#333333"
	});
 
new MoosIFR("h2", {
		flashsrc: "/sifr/Myriad_Pro_Bold_Cond_It.swf", //font
		textcolor: "#333333"
	});
 



/*
new MoosIFR(("#content h2"), {
		flashsrc: "/cmsms/sifr/verlag-book.swf",
		 flashvars: "offsetTop=1",
		textcolor: "#236FB0"
	});
 
*/

// var list = $$('#mootoolmenu1 h3');
// list.each(function(element) );
	
	

new SmoothScroll({duration: 800});



var accordion = new Accordion('h3.atStart', 'div.atStart', {
	opacity: false,
	onActive: function(toggler, element){
		//toggler.toggleClass('accofirst');
		//toggler.setStyle('background-position', '0 -36px');
		if (toggler.hasClass('accofirst')) toggler.addClass('accofirston'); else toggler.addClass('toggleron');
		
		
		
	},
 
	onBackground: function(toggler, element){
	toggler.removeClass('accofirston');
	toggler.removeClass('toggleron');
	
	}
}, $('accordion'));
 
 
 
 
accordion.display($$('h3.atStart').length-1);


//forms:

$$('input,textarea').each(function(el){

el.addEvent('focus', function(){
el.addClass('focus');
$E('label[for='+el.id+']').addClass('focus');
	});
el.addEvent('blur', function(){
el.removeClass('focus');
$E('label[for='+el.id+']').removeClass('focus');

	});


});

//fill in page title

if ($('pagetitle')) $('pagetitle').value=document.title;



// Safari menu bug
/*
if (window.webkit) {

window.addEvent('domready', function(){

function correctmenu(){

topmenuoffest=$('floatmenu').getTop()+$('contentbox').getTop()-$('menu_vert').getTop();

$('floatmenu').style.top=topmenuoffest+"px";
//alert("saf fix "+topmenuoffest);
}

correctmenu.delay(500);

});

}
*/


});



