var slide;
function slideSwitch() {
  var $active = $('#slideshow img.active');
  if ( $active.length == 0 ) $active = $('#slideshow img:last');
  var $next =  $active.next().length ? $active.next() : $('#slideshow img:first');
  $active.addClass('last-active');
  $next.css({opacity: 0.0})
  .addClass('active')
  .animate({opacity: 1.0}, 1000, function() {
    $active.removeClass('active last-active');
  });
}
function bgShow($next, $active, player) {
  index = $("#bg div").index($next);
  $active.addClass('last-active');
  $next.css({opacity: 0.0})
  .addClass('active')
  .animate({opacity: 1.0}, 1000, function() {
    $active.removeClass('active last-active');
  });
  if(player==false)
   slide = setTimeout("bgRotate()", refreshTime);
}
function bgLoad($next, $active, player) {
  if (screen.width <= 1280) {
    resolution = 12;
  } else if (screen.width <= 1700) {
    resolution = 17;
  } else {
    resolution = 20;
  }
  $("#loader").show();
  img = new Image();
  index = $("#bg div").index($next);
  img.src = '/bg/bg'+resolution+'_'+bgs[index]+'.jpg';
  img.onload = function() {
    $next.css('background-image', 'url("'+img.src+'")');
    $("#loader").hide();
    bgShow($next, $active, player);
  };
}
function bgRotate() {
  var $active = $('#bg div.active');
  if ( $active.length == 0 ) $active = $('#bg div:last');
  var $next =  $active.next().length ? $active.next() : $('#bg div:first');
  if($next.css('background-image')=="none") {
    bgLoad($next, $active, false);
  } else {
    bgShow($next, $active, false);
  }
}
function photo(img, src) {
	_img = new Image();
	_img.src = src;
	_img.onload = function() {
//		max_w = 100;
//		max_h = 100;
//		if(img.parents('li').hasClass('cat')) {
			max_w = 140;
			max_h = 105;
//		}

		img.width(max_w-10).height(max_h-10).attr("src", src).css("padding", "1px").css("border", "4px solid #99782e");

		img.hover(function() {
			$(this).stop().animate({width:max_w, height:max_h, marginLeft: '-5px', marginTop: '-5px'}, 200);
		}, function() {
			$(this).stop().animate({width:max_w-10, height:max_h-10, marginLeft: 0, marginTop: 0}, 200);
		});
	};
	img.attr("src", "/i/ajax-loader.gif");
}

function changeRel(margin_top, _url) {
	$("a[rel=jx]").each(function(i) {
		href = $(this).attr('href').replace(/.html/g, '');
		$(this)
		.attr('href', '#'+href)
		.attr('rel', href)
		.removeAttr('rel');
	});

	$('a[rel=lightbox]').fancybox({
		'titleShow'     : true,
		'titlePosition' : 'inside',
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'easingIn'      : 'easeOutBack',
		'easingOut'     : 'easeInBack',
		'overlayOpacity': 0.8,
		'overlayColor'	: '#000'
	});
	$('a[rel=youtube]').click(function() {
		$.fancybox({
			'padding'		: 0,
			'titlePosition'	: 'inside',
			'autoScale'		: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'title'			: this.title,
			'width'			: 500,
			'height'		: 405,
			'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
			'type'			: 'swf',
			'swf'			: {'wmode': 'transparent', 'allowfullscreen': 'true'},
			'overlayOpacity': 0.8,
			'overlayColor'	: '#000'
		});
		return false;
	});
	$("#photos a > div > img").each(function(i, __img) {
		img = $(__img);
		photo(img, img.attr("src"));
	});

	if($("#menu").height() < 450)
		$("#cat_menu").css('left', '200px');
	//$("#cat_menu").css("top", margin_top-30);
	_margin_top = margin_top;
	if(_url=="/")
		_margin_top += 100;
	$("#top").height(_margin_top+20);
	//$("#content").css("top", _margin_top+2);
	//$("#photos").css("top", _margin_top+2);
}
function setFlash(site) {
  try {
    document.getElementById("menu2").ustaw('anim8');
    document.getElementById("menu2").ustaw(site);
  } catch(e) { setTimeout('setFlash("'+site+'")', 500); }
}

$(document).ready(function() {
	ratio = 1900/1000;
	_ratio = $(window).width()/$(window).height();
	margin_top = 200;//parseInt($(window).height()/4);
	$("#top").height(margin_top+20);
	$("#menu").css("top", margin_top);

	var flashvars = {};
	var params = {};
	params.wmode = "transparent";
/*
	m_width = 265;
	m_height = 390;
	if(($(window).height()-margin_top-50) < m_height) {
		__ratio = ($(window).height()-margin_top-50)/m_height;
		m_width *= __ratio;
		m_height *= __ratio;
		$("#cat_menu").css('left', '200px');
	}
	swfobject.embedSWF("/menu.swf", "menu_f", m_width, m_height, "9.0.0", "/expressInstall.swf", flashvars, params);
*/
  swfobject.embedSWF("/menu3.swf", "menu2", 930, 180, "9.0.0", "/expressInstall.swf", flashvars, params);

	$("a.f").hover(function() { $(this).css({backgroundPosition: 'bottom'}); }, function() { $(this).css({backgroundPosition: 'top'}); });
	$("a.y").hover(function() { $(this).css({backgroundPosition: 'bottom'}); }, function() { $(this).css({backgroundPosition: 'top'}); });

	if(ratio < _ratio) { $("#bg img").width($(window).width()+20); $("#top > img").width($(window).width()+20); }
	else { $("#bg img").height($(window).height()+20); $("#top > img").height($(window).height()+20); }

	url = window.location.pathname.match(/([a-z,0-9]+).html/);
	if(url != null) changeRel(margin_top, url[0]);
	else changeRel(margin_top, "/");

	$("#menu a img").hover(function() {
		$("#menu a img").attr("src", "/i/karta2.png");
	}, function() {
		$("#menu a img").attr("src", "/i/karta1.png");
	});

	$("#content form[name=karta] input").change(function() {
		sum = 0;
		i = $("#content form[name=karta] input[name=karta1]");
		if(!isNaN(i.val())) sum += parseInt(i.val())*100; else i.val("0");
		i = $("#content form[name=karta] input[name=karta2]");
		if(!isNaN(i.val())) sum += parseInt(i.val())*200; else i.val("0");
		i = $("#content form[name=karta] input[name=karta3]");
		if(!isNaN(i.val())) sum += parseInt(i.val())*300; else i.val("0");
		i = $("#content form[name=karta] input[name=karta4]");
		if(!isNaN(i.val())) sum += parseInt(i.val())*400; else i.val("0");

		$("#content form table tr:last td span").html(sum+" zł");
	});
	$("#content form[name=karta] img.send").hover(function() {
		$(this).attr("src", "/i/zamow2.png");
	}, function() {
		$(this).attr("src", "/i/zamow1.png");
	}).parent().click(function() {
		sum = 0;
		i = $("#content form[name=karta] input[name=karta1]");
		if(!isNaN(i.val())) sum += parseInt(i.val())*100; else i.val("0");
		i = $("#content form[name=karta] input[name=karta2]");
		if(!isNaN(i.val())) sum += parseInt(i.val())*200; else i.val("0");
		i = $("#content form[name=karta] input[name=karta3]");
		if(!isNaN(i.val())) sum += parseInt(i.val())*300; else i.val("0");
		i = $("#content form[name=karta] input[name=karta4]");
		if(!isNaN(i.val())) sum += parseInt(i.val())*400; else i.val("0");
		if(sum==0) alert("Musisz wybrać ilość kuponów");
		else $("#content form[name=karta]").submit();
	});

  $.each(bgs, function() {
    $("#bg").append('<div></div>');
  });
  bgRotate();
  setInterval("slideSwitch()", 3000);
  //$("#slideshow").css({'cursor': 'pointer'}).click(function() {
  //  window.location = '#tatuaz,galeria';
  //});

	$.address.change(function(event) {
		site_name = event.value;
		_s = site_name.split(",");
		if(_s[0]=="studio"||_s[0]=="/studio") setFlash("anim1");
    else if(_s[0]=="tatuaz"||_s[0]=="/tatuaz") setFlash("anim2");
    else if(_s[0]=="kolczykowanie"||_s[0]=="/kolczykowanie") setFlash("anim3");
    else if(_s[0]=="linki"||_s[0]=="/linki") setFlash("anim6");
    else if(_s[0]=="kontakt"||_s[0]=="/kontakt") setFlash("anim7");

		if(site_name == "sklep") window.location = 'http://www.99piercing.pl';
		else if(site_name == "warkoczyki") window.location = 'http://www.warkoczyki.pl';
		else if(site_name != "/") {
			$("#include").load(site_name+".html?jx=1", function() {
				changeRel(margin_top, site_name);
			});
		}
	});
});

