/* mobile */
//<![CDATA[
var mobi = ['opera', 'iemobile', 'webos', 'android', 'ipad', 'safari'];
var midp = ['blackberry', 'symbian'];
var basePath = 'fileadmin/templates/html/';
var ua = navigator.userAgent.toLowerCase();
var desktop = '<link rel="stylesheet" href="' + basePath + 'css/all.css" type="text/css" media="all"/><!--[if lt IE 8]><link rel="stylesheet" type="text/css" href="' + basePath + 'css/ie-lt8.css" /><![endif]--><!--[if IE]><link rel="stylesheet" type="text/css" href="' + basePath + 'css/ie-all.css" /><![endif]-->';
if ((ua.indexOf('midp') != -1) || (ua.indexOf('mobi') != -1) || ((ua.indexOf('ppc') != -1) && (ua.indexOf('mac') == -1)) || (ua.indexOf('webos') != -1)) {
	document.write('<link rel="stylesheet" href="' + basePath + 'css/allmobile.css" type="text/css" media="all"/>');
	if (ua.indexOf('midp') != -1) {
		for (var i = 0; i < midp.length; i++) {
			if (ua.indexOf(midp[i]) != -1) {
				document.write('<link rel="stylesheet" href="' + basePath + 'css/' + midp[i] + '.css" type="text/css"/>');
			}
		}
	}
	else {
		if ((ua.indexOf('mobi') != -1) || (ua.indexOf('ppc') != -1) || (ua.indexOf('webos') != -1)) {
			for (var i = 0; i < mobi.length; i++) {
				if (ua.indexOf(mobi[i]) != -1) {
					document.write('<link rel="stylesheet" href="' + basePath + 'css/' + mobi[i] + '.css" type="text/css"/>');
					break;
				}
			}
		}
	}
}
else {
	document.write(desktop);
}




function initFilterSelect()
{	
	$('.grid-filter select').replaceWith( function () {
		
		var repHtml = '<div class="select-hidden" style="display: none;">' 
					+ $(this).parent().html() 
					+ '</div>';
		
		return repHtml;
	});
	
	$('.grid-filter select').each( function () {
		
		$(this).parent().parent().append( genAltFilterWrap() );
		
		var optSelector 	= 'ul.options';
		var optContainer 	= $(optSelector, $(this).parent().parent());
		var selected 		= {};
		
		// replace options
		$( 'option', this ).each( function () {		
			
			var repOption = $(genAltFilterOption(this));
			
			// bind custom action to option click
			$('a',repOption).bind( 'click', optionClickHandler);
			repOption.bind('click', optionClickHandler);
			
			// append option to alternative container
			optContainer.append( repOption );
			
			if ( $(this).attr('selected') )
				selected = this;
		});
		
		// put selected option into title box
		var titleSelector = '.title span';
		var titleContainer = $( titleSelector, $(this).parent().parent() );
		
		titleContainer.append( $(selected).text() );
		
		// bind title events
		$('a',titleContainer)
			.bind('click', titleClickHandler)
			.hover(titleHoverHandler);
		titleContainer
			.bind('click', titleClickHandler)
			.hover(titleHoverHandler);
	});
	
	$('body').bind('click', function () {		
		if ( $.activeDrop ) hideDrop( $.activeDrop )
	});
}
function genAltFilterOption( selectOption ) 
{
	var optHtml = ( $(selectOption).attr('selected') ) 
				? '<li class="selected">' 
				: '<li>';
	
	optHtml += '<a href="'
			+ $(selectOption).parents('form:first').attr('action') 
			+ '?' 
			+ $(selectOption).parent().attr( 'name' ) 
			+'='
			+ $(selectOption).attr('value')
			+ '"'
			+ ( $(selectOption).attr('disabled') ? ' class="disabled"' : '' )
			+ '>';
		
	optHtml += '<span>' 
			+ $(selectOption).text() 
			+ '</span></a></li>';
	
	return optHtml;
}
function genAltFilterWrap()
{
	return '<a href="#" class="title"><span></span></a>'
			+ '<div class="drop">'
			+ '<div class="drop-inner">'
			+ '<div class="table">'
			+ '<div class="top-left"></div>'
			+ '<div class="top"></div>'
			+ '<div class="top-right"></div>'
			+ '<div class="body">'
			+ '<div class="left"></div>'
			+ '<div class="content">'
			+ '<div class="col">'
			+ '<ul class="options"></ul>'
			+ '</div></div>'
			+ '<div class="right"></div>'
			+ '</div>'
			+ '<div class="bottom-left"></div>'
			+ '<div class="bottom"></div>'
			+ '<div class="bottom-right"></div>'
			+ '</div></div></div>';
}
function optionClickHandler ( ev )
{
	var title 	= $( '.title span', $(ev.target).parents('li') );
	var index 	= $(ev.target).parents('li:first').index();
	var selOpt 	= $( $( '.select-hidden select option', $(ev.target).parents('.filter-type') )[index] );
	
	selOpt.parent().val( selOpt.attr( 'value' ) ); 	// change value in selectbox
	title.text( selOpt.text() ); 					// change title field
	hideDrop( $.activeDrop ); 						// hide drop
	$(selOpt).parents('form:first').submit(); 		// submit form
	
	return false;
}
function titleClickHandler ( ev )
{
	var drop = $( '.drop', $(ev.target).parents('li') );
	
	drop.hidden = ( parseInt(/[0-9\-]+/.exec(drop.css('top'))[0]) <  0 );
	
	if ( drop.hidden ) 	showDrop( drop );
	else 				hideDrop( drop );
	
	return false;
}
function titleHoverHandler( ev )
{
	var drop = $( '.drop', $(ev.target).parents('li') );
	
	if ( ev.type == 'mouseenter' && $.activeDrop[0] != drop[0] ) 
	{		
		hideDrop( $.activeDrop );	
		showDrop( drop );
	}	
}
function hideDrop ( dropEl )
{
	//var wrapperHeight = $(dropEl).offset().top + $('.grid').parent().height();
   // $('#wrapper').css( 'height', wrapperHeight+$('#footer').height() );
	
	$(dropEl).css( 'top', '-9999px' );
	$('a.title', $.activeDrop.parents('li.filter-type')).removeClass('active-drop');
	
	$(dropEl).addClass( 'hidden' );
	$.activeDrop 		= null;
}
function showDrop ( dropEl )
{
	var dropHeight 		= $(dropEl).css( 'height' );
	$(dropEl).css		( 'height', 0 );
	$(dropEl).animate	( {height: dropHeight}, 250 );
	$(dropEl).css		( 'top', '24px' );
	$(dropEl).removeClass('hidden');
	$.activeDrop 		= $(dropEl);

	/*
	var wrapperHeight 	= parseInt( $(dropEl).offset().top )
    					+ parseInt( dropHeight.substr( 0, dropHeight.length-2) )
    					+ $('#footer').height()
    					+ 25;

	if ( $('#wrapper').height() < wrapperHeight )
		$('#wrapper').css( 'height', wrapperHeight );
	*/
	
	$('a.title',dropEl.parents('li.filter-type')).addClass('active-drop');
}

function initDropDown()
{
	$('li.level1', '#nav').each(function() {
		
		if ( $('.drop',this).length > 0 ) {
			$(this).addClass('has-drop-down');
			$(this).hover(
				function() {					
					var position = $(this).position();
					$('.drop', this).stop().hide().show().attr('style','top: 50px; left:' + position.left + 'px;').animate({top:'25px'}, 150);
				},
				function() {
					$('.drop', this).stop().hide();
				}
			); 
		}
	});
}

jQuery.fn.eachDelay = function(callback, speed){
	return jQuery.eachDelay( this, callback, speed)
}
jQuery.extend({
	eachDelay: function(object,callback, speed){ 
		var name, i = -1, length = object.length, $div = $('<div>'), id;
		if (length === undefined) { //not an array process as object
			var arr = [], x = -1;
			for (name in object) arr[++x] = name; 
			id = window.setInterval(function(){
			 if( ++i === arr.length || callback.call(object[ arr[i] ], arr[i], object[ arr[i] ]) === false) 
			 	 clearInterval(id);
			}, speed);	
		}
		else { //array-compatible element ie. [], jQuery Object
			id = window.setInterval(function(){ 
				if (++i === object.length || callback.call(object[i], i, object[i]) === false) 
					clearInterval(id);
			}, speed);
		}
		return object;
	}
});

function animateGrid()
{
	var colorArray = new Array(4);
	colorArray[0] = "#F3F3F3";
	colorArray[1] = "#D3D3D3";
	colorArray[2] = "#DFDFDF";
	colorArray[3] = "#C2C2C2";
	
	var delayTime = 100;
	
	$('.visual-list div.visual').each(function() {
		var random = Math.floor ( Math.random() * colorArray.length );
		$(this).attr('style','background-color: '+ colorArray[random] + ';');
		$('a', this).attr('style','width: 0%; height: 100%; display: inline-block; overflow: hidden;');
		$(this).delay(delayTime).animate({'opacity': 1},500, function() {
			var aTag = $('a', this);
			$(aTag).animate({ width:'100%'},300, function() {
				var imageWrapper = $('.image_wrapper', aTag);
				var imageCount = $('img', this).length;
				var counter = imageCount;
				$('img', imageWrapper).each(function() {
					$(this).attr('style','position: absolute; top:0px; z-index:'+counter);
					counter--;
				});
				
				if ((ua.indexOf('midp') != -1) || (ua.indexOf('mobi') != -1) || ((ua.indexOf('ppc') != -1) && (ua.indexOf('mac') == -1)) || (ua.indexOf('webos') != -1)) {
					var textBox = $('.text', this);
					var textBoxHeight = $(textBox).height();
					var holderArea = $('.holder', textBox);
					var frameArea = $('.frame', textBox);
					$(holderArea).attr('style', 'opacity: 0');
					$(frameArea).attr('style', 'opacity: 0');
					$(textBox).attr('style','bottom:-'+textBoxHeight+'px; display: block; z-index: 100;').animate({'bottom':0},500);
					$(holderArea).delay(400).animate({'opacity': 1},300);
					$(frameArea).delay(400).animate({'opacity': 1},300)

					// We do not show the images on iPad (hover function not available)
//					$('img',imageWrapper).eachDelay(function() {
//						$(this).attr('style','position: absolute; top:0px; z-index:0');
//					}, 800);
				} else {
					$(this).hover(
						function() {
							// show the text box
							var textBox = $('.text', this);
							var textBoxHeight = $(textBox).height();
							var holderArea = $('.holder', textBox);
							var frameArea = $('.frame', textBox);
//							$(holderArea).attr('style', 'opacity: 0');
//							$(frameArea).attr('style', 'opacity: 0');
							$(textBox).stop().attr('style','bottom:-'+textBoxHeight+'px; display: block;').animate({'bottom':0},500);
							$(holderArea).delay(400).animate({'opacity': 1},300);
							$(frameArea).delay(400).animate({'opacity': 1},300);
							
							
							

							$(this).addClass( 'hover' );
							
							// show the different images
							var counter = 0;
							imgDelayPlay( $('img',imageWrapper) );
						},
						function() {
							// hide the text box
							var textBox = $('.text', this);
							var textBoxHeight = $(textBox).height();
							var holderArea = $('.holder', textBox);
							var frameArea = $('.frame', textBox);

							$(holderArea).stop().animate({'opacity': 0},100);
							$(frameArea).stop().animate({'opacity': 0},100, function() {
								$(textBox).stop().animate({'bottom':-textBoxHeight},500);
							});
							
							$(this).removeClass( 'hover' );
							
							// reset the z-index for images
							var counter = imageCount;
							$('img', imageWrapper).each(function() {
								$(this).attr('style','position: absolute; top:0px; z-index:'+counter);
								counter--;
							});
						}
					);
				}
				
				
			});	
		});
		delayTime += 100;
	});
}

/**
 * Play images frame by frame (and loop them).
 * 
 * @param imgs		jQuery object of images to animate
 * @param img		[optional] Used internally
 * @param imgCount	[optional] Used internally
 */
function imgDelayPlay ( imgs, img, imgCount )
{
	if ( !img )
	{
		// first time play
		var counter=0;
		imgs.eachDelay( function() { 
			imgDelayPlay( imgs, this, counter ); 
			counter++; 
		}, 800 );
	}
	else
	{
		// check if we are still hovering
		if ( $(img).parents('.hover').length > 0 )
		{
			$(img).attr('style','position: absolute; top:0px; z-index:0');
		
			if ( imgCount == imgs.length-1 ) 
			{
				// reset
				var counter = imgs.length;
				imgs.each(function()
				{
					$(this).attr('style','position: absolute; top:0px; z-index:'+counter);
					counter--; 
				});
				
				// play it again
				var counter=0;
				imgs.delay(800).eachDelay( function() { 
					imgDelayPlay( imgs, this, counter ); 
					counter++; 
				}, 800 );
			}
		}
	}
}

function initSubNavigation()
{

	$('.navbar>ul>li>a')
	.css( {backgroundPosition: "-205px 0px"} )
	.mouseover(function(){
		$(this).stop().animate(
			{backgroundPosition:"(0px 0px)"}, 
			{duration:300});

	})
	.mouseout(function(){
		$(this).stop().animate(
			{backgroundPosition:"(-205px 0px)"}, 
			{duration:300});
	});
	
/*	$('.navbar li').each(function() {
		$(this).hover(
			function() {
				$(this).stop().animate({backgroundPosition:"(0px 0px)"},{duration:300});
			},
			function() {
				$(this).stop().animate({backgroundPosition:"(-205px 0px)"},{duration:300});
			}
		);
	});
*/
}

function initMoreLinkAnimations()
{	
	$('.more-link a, .mailform .input-holder input.submit, .list li a').each(function() {
		animateMoreLinkBg( this, '(95% 55%)', 100, '(100% 55%)', 300 );
	});
	
	$('a.more').each( function () { 
		animateMoreLinkBg( this, '(95% 3px)', 100, '(100% 3px)', 300 ); 
	});

	$('.news-single-backlink a').each( function () { 
		animateMoreLinkBg( this, '(3px 0px)', 100, '(0px 0px)', 300 ); 
	});

	
	$('.next').each( function () {
		if (!$(this).hasClass('disabled'))
		{
			if ( $(this).parents( '.browse-links-wrap' ) )
				animateMoreLinkBg( this, '(95% 55%)', 100, '(100% 55%)', 300 );
			else
				animateMoreLinkBg( this, '(93% 55%)', 100, '(100% 55%)', 250 );
		}
	});
	
	$('.prev').each( function () {
		if (!$(this).hasClass('disabled'))
		{
			if ( $(this).parents( '.browse-links-wrap' ) )
				animateMoreLinkBg( this, '(5% 55%)', 100, '(0% 55%)', 300 );
			else
				animateMoreLinkBg( this, '(7% 55%)', 100, '(0% 55%)', 250 );
		}
	});
}

function animateMoreLinkBg ( 
		el, 
		indentBgPos, 
		indentDuration, 
		outdentBgPos, 
		outdentDuration  )
{
	$(el).hover(
		function() {
			$(this).stop().animate(
					{backgroundPosition: indentBgPos}, 
					{duration: indentDuration})
					.animate(
					{backgroundPosition:outdentBgPos}, 
					{duration: outdentDuration})
		},
		function() {
			$(this).stop().animate(
					{backgroundPosition:outdentBgPos}, 
					{duration:100})
		}
	);
}

/*parametrs [selector, hover_class]*/
function ieHover() {
	hoverForIE6(".navigation li, .mail-form .submit, .gallery div.prev-holder , .gallery div.next-holder , .profile .submit, .visual-list .visual", "hover");
}
function initPage()
{
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: true,
		addClassFocus: "focus",
		filterClass: "default"
	});
}
function clearFormFields(o)
{
	if (o.clearInputs == null) o.clearInputs = true;
	if (o.clearTextareas == null) o.clearTextareas = true;
	if (o.passwordFieldText == null) o.passwordFieldText = false;
	if (o.addClassFocus == null) o.addClassFocus = false;
	if (!o.filter) o.filter = "default";
	if(o.clearInputs) {
		var inputs = document.getElementsByTagName("input");
		for (var i = 0; i < inputs.length; i++ ) {
			if((inputs[i].type == "text" || inputs[i].type == "password") && inputs[i].className.indexOf(o.filterClass)) {
				inputs[i].valueHtml = inputs[i].value;
				inputs[i].onfocus = function ()	{
					if(this.valueHtml == this.value) this.value = "";
					if(this.fake) {
						inputsSwap(this, this.previousSibling);
						this.previousSibling.focus();
					}
					if(o.addClassFocus && !this.fake) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				inputs[i].onblur = function () {
					if(this.value == "") {
						this.value = this.valueHtml;
						if(o.passwordFieldText && this.type == "password") inputsSwap(this, this.nextSibling);
					}
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
				if(o.passwordFieldText && inputs[i].type == "password") {
					var fakeInput = document.createElement("input");
					fakeInput.type = "text";
					fakeInput.value = inputs[i].value;
					fakeInput.className = inputs[i].className;
					fakeInput.fake = true;
					inputs[i].parentNode.insertBefore(fakeInput, inputs[i].nextSibling);
					inputsSwap(inputs[i], null);
				}
			}
		}
	}
	if(o.clearTextareas) {
		var textareas = document.getElementsByTagName("textarea");
		for(var i=0; i<textareas.length; i++) {
			if(textareas[i].className.indexOf(o.filterClass)) {
				textareas[i].valueHtml = textareas[i].value;
				textareas[i].onfocus = function() {
					if(this.value == this.valueHtml) this.value = "";
					if(o.addClassFocus) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				textareas[i].onblur = function() {
					if(this.value == "") this.value = this.valueHtml;
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
			}
		}
	}
	function inputsSwap(el, el2) {
		if(el) el.style.display = "none";
		if(el2) el2.style.display = "inline";
	}
}

(function($){
	$(document).ready( function(){
		initFilterSelect();
	});
	
	$(window).load(function() {
		
		if ( $('.image_wrapper').length > 0 )
		{
			$(".image_wrapper img:not(:first-child)").lazyload({event: "dummyload"});
			var timeout = setTimeout(
				function() {
					$(".image_wrapper img:not(:first-child)").trigger("dummyload");
				}, 
				2000
			);
		}
		initDropDown();
		initSubNavigation();
		initMoreLinkAnimations();
		animateGrid();
	});
})(jQuery);

eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('9 u=k(){9 g=/^([^#.>`]*)(#|\\.|\\>|\\`)(.+)$/;k u(a,b){9 c=a.J(/\\s*\\,\\s*/);9 d=[];n(9 i=0;i<c.l;i++){d=d.v(o(c[i],b))};6 d};k o(a,b,c){a=a.z(" ","`");9 d=a.r(g);9 e,5,m,7,i,h;9 f=[];4(d==8){d=[a,a]};4(d[1]==""){d[1]="*"};4(c==8){c="`"};4(b==8){b=E};K(d[2]){w"#":7=d[3].r(g);4(7==8){7=[8,d[3]]};e=E.L(7[1]);4(e==8||(d[1]!="*"&&!x(e,d[1]))){6 f};4(7.l==2){f.A(e);6 f};6 o(7[3],e,7[2]);w".":4(c!=">"){5=p(b,d[1])}y{5=b.B};n(i=0,h=5.l;i<h;i++){e=5[i];4(e.C!=1){q};7=d[3].r(g);4(7!=8){4(e.j==8||e.j.r("(\\\\s|^)"+7[1]+"(\\\\s|$)")==8){q};m=o(7[3],e,7[2]);f=f.v(m)}y 4(e.j!=8&&e.j.r("(\\\\s|^)"+d[3]+"(\\\\s|$)")!=8){f.A(e)}};6 f;w">":4(c!=">"){5=p(b,d[1])}y{5=b.B};n(i=0,h=5.l;i<h;i++){e=5[i];4(e.C!=1){q};4(!x(e,d[1])){q};m=o(d[3],e,">");f=f.v(m)};6 f;w"`":5=p(b,d[1]);n(i=0,h=5.l;i<h;i++){e=5[i];m=o(d[3],e,"`");f=f.v(m)};6 f;M:4(c!=">"){5=p(b,d[1])}y{5=b.B};n(i=0,h=5.l;i<h;i++){e=5[i];4(e.C!=1){q};4(!x(e,d[1])){q};f.A(e)};6 f}};k p(a,b){4(b=="*"&&a.F!=8){6 a.F};6 a.p(b)};k x(a,b){4(b=="*"){6 N};6 a.O.G().z("P:","")==b.G()};6 u}();k Q(a,b){9 c=u(a);n(9 i=0;i<c.l;i++){c[i].R=k(){4(t.j.H(b)==-1){t.j+=" "+b}};c[i].S=k(){4(t.j.H(b)!=-1){t.j=t.j.z(b,"")}}}}4(D.I&&!D.T){D.I("U",V)}',58,58,'||||if|listNodes|return|subselector|null|var||||||||limit||className|function|length|listSubNodes|for|doParse|getElementsByTagName|continue|match||this|parseSelector|concat|case|matchNodeNames|else|replace|push|childNodes|nodeType|window|document|all|toLowerCase|indexOf|attachEvent|split|switch|getElementById|default|true|nodeName|html|hoverForIE6|onmouseover|onmouseout|opera|onload|ieHover'.split('|'),0,{}))
