$(document).ready(function(){

	$().piroBox({
		my_speed: 300, //animation speed
		bg_alpha: 0.5, //background opacity
		slideShow : 'true', // true == slideshow on, false == slideshow off
		slideSpeed : 3, //slideshow 
		close_all : '.piro_close' // add class .piro_overlay(with comma)if you want overlay click close piroBox
    });

	$(".p-main").hide();
	$("#start-p").show();
	$("#tresc").hide();
	$("#tresc").show('blind', 10000);
	
	// $(".p-main").hide();
	// $("#news").show();

	/*** FIX FOR IE ***/
	$.ajaxSetup({ cache: false });  // TURN OFF CACHE JSON
	
			$('#fadein').innerfade({
				speed: 2000,
				timeout: 5000,
				type: 'sequence',
				containerheight: '508px'
			});
	
	var pozycja2 = "720px";
    $("#menu li").bind('click', function(){
		var index = $("li").index(this)+1;
		switch (index) {
			case 1: 
				pozycja2 = "570px"; 
				$('.news-full').hide();
				$('.news').show();
				$('.bottom-text').hide();
				$('.bottom-img').show();
				$('.bottom-img').html('<img src="images/bottom_sax1.jpg" /><img src="images/bottom_2.jpg" />');
				break;
			case 2: 
				pozycja2 = "510px";
				$('.bottom-text').hide();
				$('.bottom-img').show();
				$('.bottom-img').html('<img src="images/bottom_1.jpg" /><img src="images/bottom_8.jpg" />');
				break;
			case 3: 
				pozycja2 = "425px"; 
				$('.bottom-text').hide();
				$('.bottom-img').show();
				$('.bottom-img').html('<img src="images/bottom_3.jpg" /><img src="images/bottom_9.jpg" />');
				break;
			case 4: 
				pozycja2 = "330px";
				$('.bottom-text').hide();
				$('.bottom-img').show();
				$('.bottom-img').html('<img src="images/bottom_klarnet1.jpg" class="bottom-img-klarnet"/><img src="images/bottom_klarnet2.jpg" class="bottom-img-klarnet" /><img src="images/bottom_klarnet3.jpg" class="bottom-img-klarnet" />');
				break;
			case 5: 
				pozycja2 = "257px";
				$('.bottom-text').hide();
				$('.bottom-img').show();
				$('.bottom-img').html('<img src="images/bottom_7.jpg" /><img src="images/bottom_puz1.jpg" />');
				break;
			case 6: 
				pozycja2 = "172px"; 
				$('.bottom-text').hide();
				$('.bottom-img').show();
				$('.bottom-img').html('<img src="images/bottom_4.jpg" /><img src="images/bottom_6.jpg" />');
				break;
		}
		$("#main-middle .ui-effects-wrapper").stop(true,true);
		$(".p-main").hide();		
		var tresc = $(this).children("a").attr("href");
		$(tresc+"-p").show();
		// $('body').scrollTop(0);
	});
	
	var pozycja = "720px";
    $("#menu li").mouseover(function(){
		var index = $("li").index(this)+1;
		switch (index) {
			case 1: pozycja = "570px"; break;
			case 2: pozycja = "510px"; break;
			case 3: pozycja = "425px"; break;
			case 4: pozycja = "330px"; break;
			case 5: pozycja = "257px"; break;
			case 6: pozycja = "172px"; break;
		} 
		$("#menu-arrow").stop().animate({ right : pozycja }, 600);     
    }).mouseout(function(){
		$("#menu-arrow").stop().animate({ right : pozycja2 }, 600);
	});
	
	$("a[href=#start]").click(function(){
		pozycja2 = "720px";
		$("#main-middle .ui-effects-wrapper").stop(true,true);
		$(".p-main").hide();		
		var tresc = $(this).attr("href");
		$(tresc+"-p").show();
		$('.bottom-img').hide();
		$('.bottom-text').show();
	});
	
	$("a[href=#start]").mouseover(function(){
		pozycja = "720px";
		$("#menu-arrow").stop().animate({ right : pozycja }, 600);     
    }).mouseout(function(){
		$("#menu-arrow").stop().animate({ right : pozycja2 }, 600);
	});
	
	$('body').delegate('.news-link','click',function(){
		$(window).scrollTop('0');
		var string = $(this).attr('href');
		var id = string.split('-');
		$("a[href=#news]").trigger("click");
		$("a[href=#news]").parent("li").trigger("mouseover");
		var action = 'get_one_news';
		var data = { id: id[1]};
		var img = $(this).find('img').clone();
		$.getJSON('ajax.php', { action: action, data: data }, function(data_back){
			$.each(data_back.data, function(key, val){
				$('.news-short-img').html(img);
				$('.news-short-title').html(val.title);
				$('.news-long-text').html(val.description);
				$('.news-pdf').html('');
				$.getJSON('ajax.php', { action: 'get_files', data: { productid: id[1], keyword: 'news_pdf' } }, function(data_back){
					$.each(data_back.data, function(key, val){
						$('.news-pdf').append('<a href="'+val.path+val.name+'" title="'+val.title+'" target="_blank" ><img src="/images/pdf.png" /></a>');
					});
				});
				// $('.news-img').html('');
				// $.getJSON('ajax.php', { action: 'get_files', data: { productid: id[1], keyword: 'news' } }, function(data_back){
					// $.each(data_back.data, function(key, val){
						// $('.news-img').append('<a rel="gallery" class="pirobox_gal'+val.productid+'" href="'+val.path+val.name+'" ><img class="gallery-img" src="'+val.path+val.thumb+'" /></a>');
					// });
				// });
				$('.news').hide();
				$('.news-full').show();
			});
		});
	});
	
	$('#galerie-p').delegate('.gallery-img','mouseover mouseout', function(e){
		if(e.type === 'mouseover'){
			$(this).css({opacity: 0.7});
		}else{
			$(this).css({opacity: 1});
		}
	});
	
	$('body').delegate('.news-link','mouseover mouseout', function(e){
		if(e.type === 'mouseover'){
			$(this).find('.events-short-img').css({opacity: 0.7});
		}else{
			$(this).find('.events-short-img').css({opacity: 1});
		}
	});

	$('#galerie-p').delegate('.gallery-img','click', function(e){
		$('.gallery-small').hide();
		$(window).scrollTop('0');
		$(this).parent('.gallery-small').next('.gallery-hidden').show();
	});
	
	$('#galerie-p').delegate('.gallery-back','click', function(e){
		$(this).parent('.gallery-hidden').hide();
		$(window).scrollTop('0');
		$('.gallery-small').show();
	});
	
	$('#news-p').delegate('.news-back','click', function(e){
		$("a[href=#news]").parent("li").trigger("click");
	});
	
	// $('.gallery-description').each(function(){
		// var val = $(this).html();
		// $(this).html(nl2br(val));
	// });
	
	// $('.contents').each(function(k, v){
		// var contents = $(this).html();
		// $(this).html(nl2br(contents));
	// });
	
	
	if(window.location.hash) {
		var hash = window.location.hash;
		var splithash = hash.split('-');
		// if(hash != "#start"){
			// $('.bottom-text').hide();
			// $('.bottom-img').show();
		// }else{
			// $('.bottom-img').hide();
			// $('.bottom-text').show();
		// }
		if(hash == "#start"){
			$("a[href="+hash+"]").trigger("click");
			$("a[href="+hash+"]").trigger("mouseover");
		}else if(splithash[0] == '#newsdetail'){
			$("#news-p a[href="+hash+"]").trigger("click");
		}else{
			$("a[href="+hash+"]").parent("li").trigger("click");
			$("a[href="+hash+"]").parent("li").trigger("mouseover");
		}
	}
		
});

// Jquery function
// -------------------------------

/************* JQUERY fn.serializeJSON ************/

(function( $ ){
 $.fn.serializeJSON=function() {
  var json = {};
  jQuery.map($(this).serializeArray(), function(n, i){
   json[n['name']] = n['value'];
  });
  return json;
 };
})( jQuery );

function redirect(url){
   window.location = url;
}

/************* JQUERY fn.msgJSON ************/

function nl2br(str){
		return str.replace(/\n/gi, '<br/>').replace(/\s{2}/gi, "&nbsp;&nbsp;");
}

// Other function
// -------------------------------

function reload(){
   window.location.reload();
}


