$(function(){

	// top menu
	$('ul.mainMenu li:first')
		.mouseover(function(){
			$('div.subMenu').fadeIn(300);
		})
		.mouseout(function(){
			$('div.subMenu').hide();
		});
	$('div.subMenu')
		.mouseover(function(){
			$(this).show();
			$('ul.mainMenu li.button1').addClass('hover');
		})
		.mouseout(function(){
			$(this).hide();
			$('ul.mainMenu li.button1').removeClass('hover');
		});

	var params = {
		wmode: "transparent"
	}

	$('body').append('<div id="miarka">&nbsp;</div>');
	var miarka = $('#miarka').css({
		position: 'fixed',
		bottom: 0
		//,
		//height: 0
	});
	var of = miarka.offset();
	var swfh=0;
	
	$(window).resize(function(){
		$('#miarka').css({
			position: 'absolute',
			bottom: 0,
			height: 0,
			width: 2
		});
		
		setSWFh();
		$('#mainFlash').height(swfh);
		$('#pasjaFlash').height(swfh);
	});
	
	function setSWFh(){
		of = miarka.offset();
		var min = 380;
		var max = 655 + 74;
		
		if(of.top<max && of.top>min)
		{
			swfh=of.top;
		}
		else if(of.top<min)
		{
			swfh=min;
		}
		else
		{
			swfh=max;
		}
	}
	setSWFh();
	var flashvars = {
		h: swfh-50
	};
	var params = { wmode: "transparent" };
	
	var startconfigvars = {};

	if($('.contentFlash').data('hash')){
		startconfigvars.productHash = $('.contentFlash').data('hash');
	}
	if($('.contentFlash').data('sklep')){
		startconfigvars.shop = $('.contentFlash').data('sklep');
	}

	swfobject.embedSWF("/swf/Start.swf", "mainFlash", "100%", swfh, "9.0.0", null, flashvars, { wmode: "transparent" } );
	swfobject.embedSWF("/swf/StartConfig.swf?v=6&site=http://new.lilou.pl/", "configFlash", "100%", '700', "9.0.0", null, startconfigvars, { wmode: "transparent" } );
//	swfobject.embedSWF("/swf/StartConfig.swf?v=6", "configFlash", "100%", '700', "9.0.0", null, startconfigvars, { wmode: "transparent" } );
	swfobject.embedSWF("/swf/StartPasja.swf", "pasjaFlash", "100%", swfh, "9.0.0", null, flashvars, null);
	swfobject.embedSWF("/swf/Menu.swf", "menuFlash", "100%", "130", "9.0.0", null, null, params);

	// h1 naglowki
	$('h1').attr('id', 'h1title');
	var flashvars = {
		tekst: $('h1').text().replace(/%/i, '%25').replace(/&/i, '%26'),
		textAlign: "center",
		tekstSize: "18",
		tekstColor: "0x8a8a8a"
	};

	swfobject.embedSWF("/swf/Naglowek.swf", 'h1title', "900", '22', "9.0.0", null, flashvars, params);

	// h2 nowosci
	$('h2.h2title').each(function(index) {

		var element = $(this);
		
		var flashvars = {
			tekst: element.text().replace(/%/i, '%25').replace(/&/i, '%26'),
			textAlign: "left",
			tekstSize: "18",
			tekstColor: "0x393939"
		}

		if (element.find('a').attr('href')) {
			flashvars.link = element.find('a').attr('href');
		}

		$(this).attr('id', 'h2title'+index);
		swfobject.embedSWF("/swf/Naglowek.swf", 'h2title'+index, "750", '22', "9.0.0", null, flashvars, params);
		$('#h2title'+index).css('margin-left', '-3px');
	});

	// h2 salony
	$('h2.h2salony').attr('id', 'h2salony').parent('div').css('margin-left', '40px').css('display', 'block');
	var flashvars = {
		tekst: $('h2.h2salony').text().replace(/%/i, '%25').replace(/&/i, '%26'),
		textAlign: "left",
		tekstSize: "24",
		tekstColor: "0x6f6f6f"
	};
	swfobject.embedSWF("/swf/Naglowek.swf", 'h2salony', "500", '28', "9.0.0", null, flashvars, params);

	//Salony
	$('.lista ul').each(function(){
		// $(this).find('object').first().attr('width', 190);
		$(this).find('li:eq(1)').find('h3').css('width', 145);
	})

	// faq
	$('.faqlist dt span').each(function(index) {
		var flashvars = {
			tekst: $(this).text().replace(/%/i, '%25').replace(/&/i, '%26'),
			textAlign: "left",
			tekstSize: "18",
			tekstColor: "0x393939",
			selectable: false
		};
		$(this).attr('id', 'title'+index);
		swfobject.embedSWF("/swf/Naglowek.swf", 'title'+index, "900", '25', "9.0.0", null, flashvars, params);
	});

	// daty, nowosci
	$('div.date').each(function(index){
		var date = $(this).children('div').attr('id').split('-');
		$(this).children('div').attr('id',date.join('')+'_'+index);
		swfobject.embedSWF("/swf/BrelokData.swf", $(this).children('div').attr('id'), "100", "122", "9.0.0", null, {dzien: Number(date[3]), miesiac: Number(date[2]), rok: Number(date[1])}, params);
	})


	$("a[rel^='prettyPhoto']").colorbox({
		current:'"Zdjęcie {current} z {total}',
		previous: 'Poprzednie',
		next: 'Następne',
		close: 'Zamknij'
	});

	$("div.zestawy a.lbox").colorbox({
		inline:true,
		width:'900px',
		previous: 'Poprzednie',
		next: 'Następne',
		close: 'Zamknij'
	});

	$("div.star a").colorbox({
		inline:true,
		width:'900px',
		previous: 'Poprzednie',
		next: 'Następne',
		close: 'Zamknij'
	});

	$("div.star:even").addClass('left');
	$("div.star:odd").addClass('right');

	$("a[rel^='mediaPhoto']").colorbox({
		current:'"Zdjęcie {current} z {total}',
		previous: 'Poprzednie',
		next: 'Następne',
		close: 'Zamknij'
	});
	
	$('#bigNewsImg').click(function(){
		$('.fotolist a:first').click();
	});

	// naglowki dla stron statycznych

	// h2 nowosci
	$('.wrapper.pages .content h2').each(function(index) {

		var h = $(this).outerHeight();

		var flashvars = {
			tekst: $(this).html().replace(/%/i, '%25').replace(/&/i, '%26'),
			textAlign: "left",
			tekstSize: "18",
			tekstColor: "0x393939"
		};

		$(this).attr('id', 'h2title'+index);
		swfobject.embedSWF("/swf/Naglowek.swf", 'h2title'+index, "750", h, "9.0.0", null, flashvars, params);
		$('#h2title'+index).css('margin-left', '-3px');
	});


	// ask
	$('.wrapper.faq.ask .content h2, .wrapper.auth .content h2').each(function(index) {

		var h = $(this).outerHeight();

		var flashvars = {
			tekst: $(this).html().replace(/%/i, '%25').replace(/&/i, '%26'),
			textAlign: "left",
			tekstSize: "18",
			tekstColor: "0x393939"
		};

		$(this).attr('id', 'h2title'+index);
		swfobject.embedSWF("/swf/Naglowek.swf", 'h2title'+index, "750", h, "9.0.0", null, flashvars, params);
		$('#h2title'+index).css('margin', '0 0 20px 70px');
	});

	//zestawy
	$('.wrapper.zestawy .content h2').each(function(index) {

		var h = $(this).outerHeight();

		var flashvars = {
			tekst: $(this).html().replace(/%/i, '%25').replace(/&/i, '%26'),
			textAlign: "left",
			tekstSize: "18",
			tekstColor: "0x393939"
		};

		$(this).attr('id', 'h2title'+index);
		swfobject.embedSWF("/swf/Naglowek.swf", 'h2title'+index, "750", h, "9.0.0", null, flashvars, params);
	});

	// stars
	$('.wrapper.gwiazdozbior .content h2').each(function(index) {

		var h = $(this).outerHeight();

		var flashvars = {
			tekst: $(this).html().replace(/%/i, '%25').replace(/&/i, '%26'),
			textAlign: "left",
			tekstSize: "18",
			tekstColor: "0x393939"
		};

		$(this).attr('id', 'h2title'+index);
		swfobject.embedSWF("/swf/Naglowek.swf", 'h2title'+index, "750", h, "9.0.0", null, flashvars, params);
	});

	// stars
	$('.wrapper.media .content h2').each(function(index) {

		var h = $(this).outerHeight();

		var flashvars = {
			tekst: $(this).html().replace(/%/i, '%25').replace(/&/i, '%26'),
			textAlign: "left",
			tekstSize: "18",
			tekstColor: "0x393939"
		};

		$(this).attr('id', 'h2title'+index);
		swfobject.embedSWF("/swf/Naglowek.swf", 'h2title'+index, "750", h, "9.0.0", null, flashvars, params);
	});
	


});

function resizeH(value)
{
	if ( value < 80 ) {
		value = 80
	}
	$('#menuFlash').height(value);
}

// galeria inspiracje

$(document).ready(gallery);

function gallery()
{
	var images = $('.list img');
	var list = $('.small .list');
	var medium = $('.inspirationsGallery .medium');
	var width = $('.inspirationsGallery .small').width();

	var scrollWidth = 0;
	var maxOffset = 0;
	var widths = [];
	var last;

	medium.find('a.cboxElement').hide();
	var counter = 0;

	// IE i jego cache'owanie...
	if ($.browser.msie) {
		var time = new Date();
		time = time.getTime();
		images.each(function(){
			$(this).attr('src', this.src+'?time='+time);
		})
	}
	
	images.load(function(){
		scrollWidth += $(this).width()+12;
		widths[counter] = $(this).width()+10;
		counter++;
		if (counter === images.size()){
			navigate();
		}
	})

	function navigate(){
		
		list.css('width', scrollWidth);
                if(scrollWidth > width) {
                    maxOffset = scrollWidth-width;
                } else {
                    maxOffset = 0;
                }

		// move thumbs list at the bottom
		$('.inspirationsGallery').append( $('.inspirationsGallery > .small' ) );

		$('.inspirationsGallery').removeClass('loading');

		list.fadeIn(500);
		last = images.eq(0);
		last.animate({marginTop:0}, 500).css('border-color', '#CCC')
		medium.find('a').eq(0).fadeIn(1000);

		images.click(function(event, offset){
			var fromLeft = 0;

			if (!offset){
				var index = images.index(this);

				medium.find('a.cboxElement').hide();
				medium.find('a.cboxElement').eq(index).fadeIn(1000);

				if (last) { last.stop(true, false).animate({marginTop:10}, 500).css('border-color', '#FFF') }
				$(this).stop(true, false).animate({marginTop:0}, 500).css('border-color', '#CCC')
				
				for (var i = 0; i < index; i++){
					fromLeft = fromLeft + widths[i];
				}

				var toCenter = (width/2)-(widths[index]/2);
				var scrollTo = fromLeft-toCenter;

				scrollTo = (fromLeft < toCenter) ? 0 : scrollTo ;

			} else {
				var scrollTo = offset-parseInt(list.css('left'));
				scrollTo = scrollTo-width < -maxOffset ? 0 : scrollTo;
			}

			scrollTo = (scrollTo > maxOffset) ? maxOffset : scrollTo;

			list.stop(true, false).animate({left:-scrollTo}, 1000)
			
			last = $(this);
		})
	
	}

	// przyciski do przewijania
	var nav = $('.inspirationsGallery').append('<div class="button prev"/><div class="button next"/>');
	
	$('.button.prev').click( function(){
			last.prev().trigger('click', [-width])
		} );

	$('.button.next').click( function(){
			last.next().trigger('click', [width])
		} );
}

$(document).ready(function() {
	$('a[href=#]').click(function(){return false;});
});

function mobileDetect() {
	var ua = navigator.userAgent.toLowerCase();
	//alert(ua);
	if((ua.indexOf("android 1.5") > -1) || (ua.indexOf("android 1.6") > -1) || (ua.indexOf("android 2.1") > -1)) {
		var isAndroid = 1;
	}
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))  || isAndroid) {
		if (document.cookie.indexOf("iphone_redirect=false") == -1) {
   			$('.contentMobile').show();
		}
	}
}
