/**
 * PRELOAD / RANDOM POS / MORPH
 * @author uffizi(dot)ch for YoH atelier de couture zurich
 */
/* Conf */
var faktor1 = 2;
var faktor2 = 1;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
/* Start */
// yohstartimgpos();
// yohstartload();
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
/* Preload img
var busy = false, timer, loadedImages = [];
var images = new Array('/grafik/files/gimgs/11_yi2t0130_v2.jpg?' + Math.random(),
                       '/grafik/files/gimgs/11_yi2t0145_v2.jpg?' + Math.random());
var bilder = new Array();
bilder[0] = new Object();
bilder[0]['breite'] = '399';
bilder[0]['hoehe']  = '600';
bilder[0]['alt']  = 'Legendentext';
bilder[1] = new Object();
bilder[1]['breite'] = '600';
bilder[1]['hoehe']  = '598';
bilder[1]['alt']  = '';
var i_count = images.length -1;
*/
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
function unBlur() {
	this.blur();
}
function addUnBlur() {
	var links = document.getElementsByTagName('a');
	for (var i=0; i < links.length; i++) {
		links[i].onfocus = unBlur;
	}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
// MORPH
Fx.Morph = Fx.Styles.extend({
	start: function(className){
		var to = {};
		$each(document.styleSheets, function(style){
			var rules = style.rules || style.cssRules;
			$each(rules, function(rule){
				if (!rule.selectorText.test('\.' + className + '$')) return;
				Fx.CSS.Styles.each(function(style){
					if (!rule.style || !rule.style[style]) return;
					var ruleStyle = rule.style[style];
					to[style]	= (style.test(/color/i) && ruleStyle.test(/^rgb/)) 
								? ruleStyle.rgbToHex() 
								: ruleStyle;
				});
			});
		});
		return this.parent(to);
	}
});
Fx.CSS.Styles = ["backgroundColor", "backgroundPosition", "color", "width", "height", "left", "top", "bottom", "right", "fontSize", "letterSpacing", "lineHeight", "textIndent", "opacity"];
Fx.CSS.Styles.extend(Element.Styles.padding);
Fx.CSS.Styles.extend(Element.Styles.margin);
Element.Styles.border.each(function(border){
	['Width', 'Color'].each(function(property){
		Fx.CSS.Styles.push(border + property);
	});
});
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
function uffizistartload() {
	addStyle("body { overflow: hidden; }", -1);
	$('uffizistart').empty();
	var myMorph = new Array();
	new Asset.images(images, {
		onProgress: function(i) {
			myMorph[i] = new Fx.Morph('morpher'+i, {wait: false});
		///	if (i < 2 && bilder[i]['href'] != '') {
			if (bilder[i]['href'] != '') {
				var bild = new Element('img', {
					'src': images[i], 
					'id': 'morpher' + i, 
					'alt': bilder[i]['alt'], 
					'title': bilder[i]['alt'], 
					'class': 'morph' + i,
				//	'onclick': 'GehWeiter("' + window.location.pathname + bilder[i]['href'] + '")',
					'onclick': 'GehWeiter("' + uffiziUri + bilder[i]['href'] + '")',
					'style': 'cursor: pointer' });
				/*	'class': 'morph' + i,
					'style': 'z-index:' + (Math.round(Math.random()* i_count)) */
			} else {
				var bild = new Element('img', {
					'src': images[i], 
					'id': 'morpher' + i, 
					'alt': bilder[i]['alt'], 
					'title': bilder[i]['alt'], 
					'class': 'morph' + i });
				/*	'class': 'morph' + i,
					'style': 'z-index:' + (Math.round(Math.random()* i_count)) */
			}
			bild.injectInside($('uffizistart'));
			morph(i);
			// tips
		///	if (i < 2 && bilder[i]['href'] != '') {
			if (bilder[i]['alt'] != '') {
				new Tips($('morpher' + i), { 
					showDelay: 800, 
					fixed: false, 
					'style': 'z-index:' + i_count + 1
				});
				loadedImages[i] = this;
			}
		},
		onComplete: function() {
			$('uffizistart').setStyle('display', 'block');
		/*	if ($('uffizifuss')) {
				$('uffizifuss').setStyle('display', 'block');
			} */
		}
	});
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
function morph(i) {
	var myMorph = new Fx.Morph('morpher'+i, {wait: false});
	$('morpher'+i).addEvent('mouseenter', function(e){
		new Event(e).stop();
		myMorph.start('morph'+ i + '_b');
		bringInFront(this);
	
	});
	$('morpher'+i).addEvent('mouseleave', function(e){
		new Event(e).stop();
		myMorph.start('morph' + i);
		// bringInBack(this);
		bringInFront($('morpher0'));
	});
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
// (c) radelfinger dot com
// var currentMaxZIndex = i_count + 1;	// anzahl elemente+1
function bringInFront(element) {
	if (element.style.zIndex != currentMaxZIndex) {
		currentMaxZIndex = parseInt(currentMaxZIndex) + 1;
		element.style.zIndex = currentMaxZIndex;
	}
	// top weiterhin zuoberst
	// bringInFront($('morpher0')).delay(1000);
//	var yoh = bringInFront($('morpher0')).delay(1000);
}
function bringInBack(element) {
	element.style.zIndex = '0';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
// IMG POSITIONIERUNG
function uffizistartimgpos() {

	var css_img		= ' border: 10px solid #fff;';
	var supported	= (document.getElementById || document.all);
	if (supported) {
		var pageSize	= uffiziPageSize();
		var winwidth 	= (pageSize[0] >= 990)
						? pageSize[0]
						: 990;
		var winheight	= (pageSize[1] >= 560)
						? pageSize[1]
						: 560;
		for (var i=0; i <= i_count; i++) {
			
			if (i == 0) {
				var z	= i_count + 1;
			} else if (i == 1) {
				var z	= i_count;
			} else {
				var z = 0;
			}
			var w1	= Math.round(bilder[i]['breite']/faktor1);
			var h1	= Math.round(bilder[i]['hoehe']/faktor1);
			var w2	= Math.round(bilder[i]['breite']/faktor2);
			var h2	= Math.round(bilder[i]['hoehe']/faktor2);
			var m_w	= Math.round((w1-w2)/2);
			var m_h	= Math.round((h1-h2)/2);
			var hs	= ((h2-h1)/2)+h1+10;
			
		//	var x	= (Math.round(Math.random()* (winwidth - (w1 + 240)))) + 220;
			var x	= (Math.round(Math.random()* (winwidth - (w1 + 20))));
			var y	= (Math.round(Math.random()* (winheight-hs)));
			// links
			if ((x + m_w) < 0) {
				m_w = - x;
			}
			// rechts	
			if ((x + w2 + m_w + 40) > winwidth) {
				m_w	= winwidth - (x + w2 + 20);
			}
			// oben
			if ((y - Math.round((h2 - h1) / 2)) < 0) {
				m_h	= 0 - y;
			}
			// unten
			if ((y + Math.round((h2 - h1) / 2)) > winheight) {
				m_h	= (y + Math.round((h2 - h1) / 2)) - winheight;
			}
			addStyle(".morph" + i + "{ position: absolute; top: " + y + "px; left: " + x + "px; width: " + w1 + "px; height: " + h1 + "px;" + css_img + " margin: 0; z-index: " + z + "; }", -1);
			addStyle(".morph" + i + "_b { width: " + w2 + "px; height: " + h2 + "px;" + css_img + " margin: " + m_h + "px 0 0 " + m_w + "px; z-index: " + z + "; }", -1);
		}
	///	addStyle("#uffizifuss { position: absolute; top: " + (winheight-20) + "px; right: 8px; height: 18px; white-space: nowrap; display: none; z-index: 0; }", -1);
		addStyle("#uffizifuss { position: absolute; top: " + (winheight-20) + "px; right: 8px; height: 18px; white-space: nowrap; display: none; z-index: 0; }", -1);
	}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
function uffiziPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight
	arrayPageSize = new Array(w,h) 
	return arrayPageSize;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
function GehWeiter(ziel) {
	window.location = ziel;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
// IMG POS VIA CSS UEBERGEBEN
// 'Stylesheet/Regeln hinzufuegen' (c) cybaer at binon dot net - http://Coding.binon.net/AddStyle
// Lizenz CC <http://creativecommons.org/licenses/by-nc-sa/2.5/>
function addStyle(rules,target)	{
	var styleObj=null, styleSheetObj=null, i, j, p, selector, singleSelector, text;
	if(document.createElement && document.getElementsByTagName) {
		if(typeof(target)=="number") {
			if(target<=-1) { target=document.getElementsByTagName("style").length+Math.ceil(target); }
			target=Math.max(0,Math.min(document.getElementsByTagName("style").length-1,Math.floor(target)));
		}
		if(typeof(target)=="undefined" || typeof(target)=="string" || !document.getElementsByTagName("style")[target]) {
			if(document.createStyleSheet) {
				styleSheetObj=document.createStyleSheet();
				styleObj=styleSheetObj.owningElement || styleSheetObj.ownerNode;
			} else {
				styleObj=document.createElement("style");
				document.getElementsByTagName("head")[0].appendChild(styleObj);
			}
			styleObj.setAttribute("type","text/css");
			if(target) { styleObj.setAttribute("media",target); }
		} else if(typeof(target)=="number") {
			styleObj=document.getElementsByTagName("style")[target];
			styleSheetObj=styleObj.sheet || styleObj.styleSheet;
		}
		if(styleObj && rules) {
			/*@cc_on
			@if(@_jscript)
				rule=rules.replace(/\s+/g," ").replace(/\/\*.+?\*\//g,"").split("}");
				for(i=0;i<rule.length;i++) {
					p=rule[i].indexOf("{");
					selector=rule[i].substring(0,p).replace(/^\s+|\s+$/g,"");
					text=rule[i].substring(p+1);
					if(selector) {
						if(selector.indexOf(",")) {
							singleSelector=selector.split(",");
						} else {
							singleSelector=new Array(selector);
						}
						for(j=0;j<singleSelector.length;j++) { styleSheetObj.addRule(singleSelector[j].replace(/^\s+|\s+$/g,""),text.replace(/^\s+|\s+$/g,"")); }
					}
				}
			@else @*/
				if(styleObj.firstChild) { styleObj.firstChild.nodeValue=styleObj.firstChild.nodeValue.replace("<!--",""); }
				if(styleObj.lastChild) { styleObj.lastChild.nodeValue=styleObj.lastChild.nodeValue.replace("-->",""); }
				styleObj.appendChild(document.createTextNode(rules+"\n"));
			/*@end @*/
		}
	}
	return styleObj;
}
