﻿document.write('<script type="text/javascript" src="cms/jquery-plugins/jquery.flash.js"></scr' + 'ipt>'); 

function slideSwitch() {
    var $active = $('#slideshow div.active');
    if ( $active.length == 0 ) $active = $('#slideshow div:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow div:first');

    // uncomment the 3 lines below to pull the images in random order
    //var $sibs  = $active.siblings();
    //var rndNum = Math.floor(Math.random() * $sibs.length );
    //var $next  = $( $sibs[ rndNum ] );

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
	.addClass('active')
	.animate({opacity: 1.0}, 1000, function() { $active.removeClass('active last-active');});
	//$('h4').animate({left:0}, 1000);
	
}

function startFlash(appType){
	$('body').append('<div id="mask"></div>');
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();
	$('#mask').css({'width':maskWidth,'height':maskHeight, 'opacity': 0});

	$('#mask').fadeIn("fast").fadeTo('fast', 0.8, function(){
		$('#main').append('<div id="flash-box"></div>');
		
		if (appType == "koupelne"){
			$('#flash-box').animate({'height':'530px'}, 500, function(){
				if (lang == 'cz'){													  
				$('#flash-box').flash({ src: 'img/sys/koupelnove-studio.swf', width: '952', height: '530', flashvars: { lang: 'cz'}})
				} else {$('#flash-box').flash({ src: 'img/sys/koupelnove-studio-en.swf', width: '952', height: '530', flashvars: { lang: lang}})}
			})
		} else if (appType == "tlacitka"){
			var tlacitkatorLeft = ($(window).width() - 952)/2;
			
			$('body').append('<div id="tlacitkator"><div id="tlacitkator-in"></div></div>')
			$('#tlacitkator').css({'left': tlacitkatorLeft});
			$('#tlacitkator-in').html('<div id="notes">Vyberte si tlačítko a podklad!</div><div id="filter"><a id="cat0" class="selected">vše</a><a id="cat1">základní</a><a id="cat2">vkládaná</a><a id="cat3">senzorová</a></div></div></div><div id="maska"><img src="img/_/main/tlacitkator/maska.png" border="0" alt="maska" width="405" height="415" /></div><div id="mixer"><div id="mixer-in"><div id="img-button"><img src="img/_/main/tlacitkator/button-default.gif" width="250" height="250" alt="" border="0" /></div><div id="img-background"><img src="img/_/main/tlacitkator/background-default.gif" width="350" height="350" alt="" border="0" /></div></div></div><div id="logo"></div><div id="text"></div><div id="backgrounds"><a class="plus" title="plus"></a><div id="backgrounds-in"><div class="data"></div></div><a class="minus" title="minus"></a></div><div id="buttons"><a class="plus" title="plus"></a><div id="buttons-in"><div class="data"></div></div><a class="minus" title="minus"></a></div>')//.append;
			$('#filter a').each(function(index) {
				$(this).click(function () {
					$(this).siblings().removeClass('selected');
					$(this).addClass('selected');
					$("#buttons-in .data").css({'left': 0}).empty();
					ajaxer(index);
				});
			});
			ajaxer('0');
			
		}
	});	
	$('#mask').click(
		function() {exitFlash()}
	);
	//alert (lang);
	return false;
}
function exitFlash(){
	$('#flash-box').remove();
	$('#tlacitkator').remove();
	
	$('#mask').fadeOut(500, function(){
		$(this).remove();
	})
}


function parseXml(xml) {  
	if (jQuery.browser.msie) {  
		var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");  
		xmlDoc.loadXML(xml);  
		xml = xmlDoc;  
	}  
	return xml;  
}  

function ajaxer(category){
$.ajax({
	url: "tlacitkator.xml",
	dataType: ($.browser.msie) ? "text" : "xml" ,
	error: function(){alert('Oooooooooooops! Wtf?!')},
	success: function(xml) {
		
		var globalaccelerator = 280;
		var globalimagepath = "img/_/main/tlacitkator/";
		var globalspeed = 0;
		var speedX = 0; 
		var xml = parseXml(xml);  

		var buttonsArray = new Array();
		$("buttons", xml).find('button').each(function(){
			var rowButton = new Array();
			//<button id="1" title="m170-bila" link="?link" bigimg="m170-bila.png" smallimg="small_m170-bila.jpg"  ></button> 
			// id title link bigimg smallimg
			rowButton[0] = $(this).attr('id');
			rowButton[1] = $(this).attr('title');
			rowButton[2] = $(this).attr('link');
			rowButton[3] = $(this).attr('bigimg');
			rowButton[4] = $(this).attr('smallimg');
			rowButton[5] = $(this).attr('category');


			if (category == 0){
				buttonsArray.push(rowButton);
			} else {
				if (category == rowButton[5]){
					buttonsArray.push(rowButton);
					var buttonRand = 0;
				}
				
			};
		});
		if (category == 0){
			var buttonRand = (Math.floor(Math.random()*buttonsArray.length))-1;
		} else {
			var buttonRand = 0;
		}

		var buttonswidth = 140 * buttonsArray.length;
		globalspeedx = globalaccelerator * buttonsArray.length;
		// zisti sirku posuvneho bloku podla poctu obrazkov * 140px
		$('#buttons-in .data').width(buttonswidth);
		
		// nastavi sirku bloku

		for (var i=0;i<buttonsArray.length;i++){
			//alert (buttonsArray[i][0]);
			$('<a href="'+ buttonsArray[i][2] +'" title="'+ buttonsArray[i][1] +'" rel="'+ buttonsArray[i][3] +'"><span><img src="'+globalimagepath+ buttonsArray[i][4] +'" class="" width="120" height="120" alt="" border="0" /></span></a>').appendTo($('#buttons-in .data'))
			.click(function(){
				$(this).siblings().removeClass('selected');
				$(this).addClass('selected');
				
				//var buttonName = 
				var tempImage = $('<img />').attr('src', globalimagepath + $(this).attr('rel'));
				
				//$('#text').html('<a href="'+$(this).attr('href')+'">'+ $(this).attr('title')+'</a>);
				$('#text').html('Vybrané tlačítko: <a href="'+$(this).attr('href')+'">'+ $(this).attr('title')+'</a>');
				
				$('#img-button img').fadeOut('slow', function() {
					$(this).remove();
					$("#img-button").fadeIn('10000').append(tempImage);
				});
				
				return false;
				
			});
		}

		if ( buttonswidth >= $('#buttons-in').width()){
			//alert($('#buttons-in').width() + " - "+buttonswidth);

			$('#buttons .minus').hover(function(){
				var maxX = $('#buttons-in').width()-$('#buttons .data').width();
				var speedX = globalspeedx - (globalspeedx / ($('#buttons .data').width() - $('#buttons-in').width()))*(-$('#buttons .data').position().left);
				//var speedX = 2000;
				//alert (speedX);
		
				$('#buttons .data').animate({'left': maxX}, speedX, 'linear');
			}, function() { $('#buttons .data').stop()})
			
			$('#buttons .plus').hover(function(){
				var maxX = $('#buttons-in').width()-$('#buttons .data').width();
				//var speedX = (($('#buttons .data').width() - $('#buttons-in').width())/2000)*2000;
				var speedX = (globalspeedx / ($('#buttons .data').width() - $('#buttons-in').width()))*(-$('#buttons .data').position().left);
				
				//alert (speedX);
				$('#buttons .data').animate({'left': 0}, speedX, 'linear');
			}, function() { $('#buttons .data').stop()});
		} else {
			$('#buttons .minus').unbind('mouseenter').unbind('mouseleave');
			$('#buttons .plus').unbind('mouseenter').unbind('mouseleave');
		}

	
		var backgroundsArray = new Array();
		$(xml).find('background').each(function(i){
			rowBackground = new Array();
			// <background id="1" title="dasdas" bigimg="small_b01.jpg" smallimg="b01.jpg" />
			rowBackground[0] = $(this).attr('id');
			rowBackground[1] = $(this).attr('title');
			rowBackground[2] = $(this).attr('smallimg');
			rowBackground[3] = $(this).attr('bigimg');
			backgroundsArray.push(rowBackground);
		});
		
		var backgroundsheight = 140 * backgroundsArray.length;
		var globalspeedy = globalaccelerator * backgroundsArray.length;
		// zisti vysku posuvneho bloku podla poctu obrazkov * 140px
		$('#backgrounds-in .data').height(backgroundsheight);
		// nastavi vysku bloku
		//alert($("#backgrounds-in a.selected").length);
	

		for (var i=0;i<backgroundsArray.length;i++){
			$('<a title="'+ backgroundsArray[i][1] +'" rel="'+ backgroundsArray[i][3] +'"><span><img src="'+ globalimagepath + backgroundsArray[i][2] +'" width="120" height="120" alt="'+ backgroundsArray[i][1] +'" border="0" /></span></a>').appendTo($('#backgrounds-in .data'))
			.click(function(){
				$(this).siblings().removeClass('selected');
				$(this).addClass('selected');

				var backgroundName = $(this).attr('rel');
				
				$('#img-background img').fadeOut('slow', function() {
					$(this).remove();
					$("#img-background").fadeIn('1000').append('<img src="'+ globalimagepath + backgroundName +'" />');
				});
			});
		}
		$('#backgrounds .minus').hover(function(){
			var maxY = $('#backgrounds-in').height()-$('#backgrounds .data').height();
			var speedY = globalspeedy - (globalspeedy / ($('#backgrounds .data').height() - $('#backgrounds-in').height()))*(-$('#backgrounds .data').position().top);
			$('#backgrounds .data').animate({'top': maxY}, speedY, 'linear');
		}, function() { $('#backgrounds .data').stop()});
	
		$('#backgrounds .plus').hover(function(){
			var speedY = (globalspeedy / ($('#backgrounds .data').height() - $('#backgrounds-in').height()))*(-$('#backgrounds .data').position().top);
			$('#backgrounds .data').animate({'top': 0}, speedY, 'linear');
		}, function() { $('#backgrounds .data').stop()});
		
		if ($("#backgrounds-in a.selected").length == 0){ 
			var backgroundRand = (Math.floor(Math.random()*backgroundsArray.length))-1;
			$("#backgrounds-in a").removeClass('selected');
			$("#backgrounds-in a:eq(" + backgroundRand + ")").addClass('selected');
			var backgroundName = $("#backgrounds-in a:eq(" + backgroundRand + ")").attr('rel');
			$('#img-background img').fadeOut('slow', function() {
				$(this).remove();
				$("#img-background").fadeIn('10000').append('<img src="'+ globalimagepath + backgroundName +'" />');
			});
		}
		
/* random ------------------------------------------ */
		$("#buttons-in a").removeClass('selected');
		$("#buttons-in a:eq(" + buttonRand + ")").addClass('selected');
		var tempImage = $('<img />').attr('src', globalimagepath + $("#buttons-in a:eq(" + buttonRand + ")").attr('rel'));
		$('#text').html('Vybrané tlačítko: <a href="'+$("#buttons-in a:eq(" + buttonRand + ")").attr('href')+'">'+ $("#buttons-in a:eq(" + buttonRand + ")").attr('title')+'</a>');
		$('#img-button img').fadeOut('slow', function() {
			$(this).remove();
			$("#img-button").fadeIn('10000').append(tempImage);
		});
/* random ------------------------------------------ */		

	}
}) // koniec ajaxu
} // koniec ajaxeru

$(document).ready(function() {

    setInterval( "slideSwitch()", 5000 );
	$('a.koupelnove-studio').click(function(){
		startFlash('koupelne');
	})
	$('a.tlacitkator').click(function(){
		startFlash('tlacitka');
		return false;
	})	
});