var dzialy = new Array('ad', 'nm', 'aw', '4d');
var moduly = new Array('blog', 'praca', 'press-room', 'oferta');

var modules_lightbox = new Array('b');
var modules_content = new Array('d');

var path = 'http://www.supremum.pl';

function track_click(object) {
	var track = object.attr("href").replace(path,"");
	pageTracker._trackPageview(track);
}

function trigger_url(hash) {
	finish_lightbox();
	//if(hash){
	hash = hash.replace("#","");
	var arr = hash.split("/");
	if(!arr[2]) {
		arr[2] = modules_content[0];
	}
	if(!arr[3]) {
		arr[3] = dzialy[0];
	}
	//if(arr[0]=='start') {
	if(jQuery.inArray(arr[1], modules_lightbox)>=0) {
		cur_href = document.location.href.replace(document.location.hash, "");
		cur_href = cur_href.replace(cur_href.slice(cur_href.lastIndexOf("/")),"");
		$("#caly").after("<a id=\"tol\"></a>");
		$("#tol").attr({
			rel: "news_details",
			href: cur_href+hash
		});
		$('a[rel*=news_details]').lightBox();
		$("#tol").click();
		move_selector($('#mm-ad'));
	}
	else if(jQuery.inArray(arr[2], modules_content)>=0) {
		var addr = $('#mm-'+arr[3]).attr("href");
		addr = addr.slice(0, addr.lastIndexOf("/"));
		addr = addr.slice(0, addr.lastIndexOf("/"));
		if(arr[4]) {
			var mod = arr[4];
			mod = mod.replace(/.htm/, "");
		} else {
			var mod = 'blog';
		}
		if(arr[5]) {
			var page = arr[5];
		} else {
			var page = 1;
		}
		$('#mm-'+arr[3]).attr({
			href: addr+'/'+mod+'/'+page
		});
		content_click($('#mm-'+arr[3]));
	}
	//}
	//}
}

function finish_lightbox() {
	//alert(hash);
	$('#jquery-lightbox').remove();
	$('#jquery-overlay').fadeOut(function() { $('#jquery-overlay').remove(); });
	// Show some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
	$('embed, object, select').css({ 'visibility' : 'visible' });
}

function change_hash(object) {
	var hash = document.location.hash;
	var cur_href = document.location.href.replace(document.location.hash, "");
	cur_href = cur_href.replace(cur_href.slice(cur_href.lastIndexOf("/")),"");
	var new_hash = '#'+object.attr("href").replace(cur_href, "");
	//alert(hash+new_hash);
	track_click(object);
	if(hash!=new_hash) {
		recentHash = new_hash;
		document.location.hash=new_hash;
		//document.location.href=cur_href+new_hash;
	}
}

function content_click(object) {
	//stop_selector();
	$('#lightbox-loading-link,#lightbox-secNav-btnClose').click();
	var id=object.attr("id");
	var cl = id.slice(-2);
	if(jQuery.inArray(cl, dzialy)<0) {
		object.animate({
			top: "-10px"
		}, 100);
	}
	$("#answer").hide();
	$('#loader').show();
	var addr = object.attr("href").replace("/d/","/dzial_r/");
	$.post(addr, '', function(text) {
		$('#loader').hide();
		my_callback(text, id);
		change_hash(object);
		move_selector(object)
		$('#logo_list').hide();
		$('#produkty').fadeTo("fast", 0);
		$('#loader_pf').show();
		$.post(path+'/c/pf', {dzial: cl}, function(text){
			//$("#klienci").fadeOut("fast").html(text);
			//$('#loader_pf').hide();
			$('#logo_list').fadeIn("fast");
			$("#klienci").fadeIn("fast").html(text);
			create_pf_navigation();
			$.post(path+'/c/show_products', {dzial: cl}, function(text){
				$('#produkty').fadeTo("fast", 1).html(text);
			});
		});
	});
}

function oferta_click(object) {
	$('#lightbox-loading-link,#lightbox-secNav-btnClose').click();
	var id=object.attr("id");
	var cl = id.slice(-2);
	if(jQuery.inArray(cl, dzialy)<0) {
		object.animate({
			top: "-10px"
		}, 100);
	}
	$("#answer").hide();
	$('#loader').show();
	var addr = object.attr("href").replace("/d/","/dzial_r/");
	$.post(addr, '', function(text) {
		$('#loader').hide();
		my_callback(text, id);
		change_hash(object);
	});
}
function stop_selector() {
	var selector = $('#main_menu_selector');
	var subs = $('#main_menu_subs div');
	selector.stop();
	subs.stop();
}

function move_selector(object) {
	var selector = $('#main_menu_selector');
	/*var subs = $('#main_menu_subs div');
	var sub1 = $('#sub1');
	var sub2 = $('#sub2');
	var sub3 = $('#sub3');
	var sub4 = $('#sub4');

	var id = object.attr("id");
	var sub1_heights = new Array();
	sub1_heights['mm-ad'] = 100;
	sub1_heights['mm-nm'] = 90;
	sub1_heights['mm-aw'] = 70;
	sub1_heights['mm-4d'] = 100;

	var sub2_heights = new Array();
	sub2_heights['mm-ad'] = 40;
	sub2_heights['mm-nm'] = 40;
	sub2_heights['mm-aw'] = 113;
	sub2_heights['mm-4d'] = 50;

	var sub3_heights = new Array();
	sub3_heights['mm-ad'] = 70;
	sub3_heights['mm-nm'] = 113;
	sub3_heights['mm-aw'] = 40;
	sub3_heights['mm-4d'] = 100;

	var sub4_heights = new Array();
	sub4_heights['mm-ad'] = 90;
	sub4_heights['mm-nm'] = 70;
	sub4_heights['mm-aw'] = 90;
	sub4_heights['mm-4d'] = 40;

	var ah = 19;

	subs.find('a').hide();*/

	//$('#main_menu_subs').animate({
		//height: 0
	//}, 100, function() {
		selector.animate({
			height: 0
		}, 100, function() {
			selector.animate({
				width: object.css('width'),
				left: (object.position().left)
			}, 200, function() {
				selector.animate({
					height: "103px"
				}, 100, function(){
					/*$('#main_menu_subs').animate({
						height: 120
					}, 100);
					sub1.animate({
						height: sub1_heights[id]
					}, 100, function() {
						var a1 = sub1.find('a');
						a1.css({
							marginTop: sub1_heights[id]-ah
						});
						a1.removeClass();
						a1.addClass(id);
						a1.fadeIn(200);
					})
					sub2.animate({
						height: sub2_heights[id]
					}, 100, function(){
						var a2 = sub2.find('a');
						a2.css({
							marginTop: sub2_heights[id]-ah
						});
						a2.removeClass();
						a2.addClass(id);
						a2.fadeIn(200);
					})
					sub3.animate({
						height: sub3_heights[id]
					}, 100, function(){
						var a3 = sub3.find('a');
						a3.css({
							marginTop: sub3_heights[id]-ah
						});
						a3.removeClass();
						a3.addClass(id);
						a3.fadeIn(200);
					})
					sub4.animate({
						height: sub4_heights[id]
					}, 100, function(){
						var a4 = sub4.find('a');
						a4.css({
							marginTop: sub4_heights[id]-ah
						});
						a4.removeClass();
						a4.addClass(id);
						a4.fadeIn(200);
					})
				})*/
			})
		})
	});
	var id=object.attr("id");
	var cl = id.slice(-2);
	
	var src = $('#chmurka').attr("src");
	src = src.slice(0, src.lastIndexOf("_"))
	$('#chmurka').fadeTo("fast", 0, function() {
		$(this).attr("src", src+'_'+cl+'.png').fadeTo(1000,0, function(){
			$(this).fadeTo("fast", 1)
		});
	})
	//$('#main_menu_subs a').each(function(i){
		//var addr = $(this).attr("href");
		//addr = addr.slice(0, addr.lastIndexOf("/"));
		////addr = addr.slice(0, addr.lastIndexOf("/"));
		//$(this).attr({
			//href: addr+'/napisy-'+cl+'-'+(i+1)
		//});
	//})
}

function my_callback(text, id) {
	$("#right").fadeIn("slow").html(text);
	var cl = id.slice(-2);
	if(jQuery.inArray(cl, dzialy)>=0) {
		$('#right-inn').removeClass();
		$('#right-inn').addClass(id);
		$('#tab_menu #oferta').removeClass();
		$('#tab_menu #oferta').addClass('oferta-'+cl);
		$("#main_menu a").each(function(i) {
			if($(this).attr("id")==id) {
				$(this).parents("li").attr('class', "active");
			} else {
				$(this).parents("li").attr('class', "");
			}
		});
		$('#kontakt').removeClass();
		$('#kontakt').addClass('kontakt-'+cl);
	}
	//if(jQuery.inArray(id, moduly)>=0) {
	//$("#main_menu a").each(function(i) {
	//var addr = $(this).attr("href");
	//addr = addr.slice(0, addr.lastIndexOf("/"));
	//addr = addr.slice(0, addr.lastIndexOf("/"));
	//$(this).attr({
	//href: addr+'/'+id+'/1'
	//});
	//});
	//}

	$('a[rel*=news_details]').lightBox();

	$(".tab-link").click(function(e) {
		e.preventDefault();
		oferta_click($(this));
	});

	$(".page-link").click(function(e) {
		e.preventDefault();
		next_prev_click($(this));
	});

	$("a#blog").hover(function() {
		przesun($("a#blog"), 0, -5, 0, 0);
	},
	function () {
		przesun($("a#blog"), 0, 0, 0, 0);
	});

	$("a#praca").hover(function() {
		przesun($("a#praca"), 0, -5, 0, 0);
	},
	function () {
		przesun($("a#praca"), 0, 0, 0, 0);
	});

	$("a#press-room").hover(function() {
		przesun($("a#press-room"), 0, -5, 0, 0);
	},
	function () {
		przesun($("a#press-room"), 0, 0, 0, 0);
	});

	$("a#oferta").hover(function() {
		przesun($("a#oferta"), 0, -5, 0, 0);
	},
	function () {
		przesun($("a#oferta"), 0, 0, 0, 0);
	});

	$("div#rss-cont").hover(function() {
		$(this).stop(true);
		$(this).animate({
			backgroundPosition: "2px top",
			width: "40px",
			right: "37px"
		}, 100)
	},
	function () {
		$(this).stop(true);
		$(this).animate({
			backgroundPosition: "0px top",
			width: "38px",
			right: "39px"
		}, 100)
	})
}

function next_prev_click(object) {
	$("#answer").fadeOut("fast");
	$('#loader').fadeIn("slow");
	var addr = object.attr("href").replace("/d/","/blog_r/");
	var page = addr.slice(addr.lastIndexOf("/")+1);
	$.post(addr, '', function(text) {
		$('#loader').hide();
		my_callback2(text, page);
		change_hash(object);
	});
}

function my_callback2(text, page) {
	$("#answer").fadeIn("slow").html(text);
	$('a[rel*=news_details]').lightBox();
	$(".page-link").click(function(e) {
		e.preventDefault();
		next_prev_click($(this));
	});
	$("#main_menu a").each(function(i) {
		var addr = $(this).attr("href");
		addr = addr.slice(0, addr.lastIndexOf("/"));
		$(this).attr({
			href: addr+'/'+page
		});
	});
}

function next_prev_post_click(object) {
	$("#post_view").fadeOut("fast");
	$('#lightbox-loading').fadeIn("slow");
	var addr = object.attr("href").replace("/show/","/show_r/");
	var addr = addr.replace("/oferta/","/oferta_r/");
	var addr = addr.replace("/praca/","/praca_r/");
	//var addr = object.attr("href");
	var page = addr.slice(addr.lastIndexOf("/")+1);
	$.post(addr, '', function(text) {
		$('#lightbox-loading').hide();
		my_callback3(text, page);
		change_hash(object);
	});
}

function my_callback3(text, page) {
	$("#post_view").fadeIn("slow").html(text);
	//$('a[rel*=news_details]').lightBox();
	//$(".page-link").click(function(e) {
	//e.preventDefault();
	//next_prev_click($(this));
	//});
	$(".np-post").click(function(e) {
		e.preventDefault();
		next_prev_post_click($(this));
	});
	$('#lightbox-secNav-btnClose,.lightbox-secNav-btnClose').click(function(e) {
		e.preventDefault();
		finish_lightbox();
		return false;
	});
	$('#commentform').submit(function() {
		send_form($(this));
		return false;
	});
	$('span.month').click(function() {
		$(this).next('ul').toggle();
	});
}

function send_form(object) {
	$('#loader_comment').fadeIn('fast');
	var str = object.serialize();
	var action = object.attr("action").replace("wp-comments-post.php", "wp-comments-post2.php");
	$.post(action, str, function(text) {
		$("#comments_div").html(text);
		$('#commentform').submit(function() {
			send_form($(this));
			return false;
		});
	});
}

function przesun(object, left_move, top_move, position_left, position_top) {
	//$('#tab_menu a').stop();
	if(!object.parent("li").hasClass("active")) {
		object.animate({
			left: (position_left+left_move)+"px",
			top:  (position_top+top_move)+"px"
		}, 100)
	}
}
var recentHash=window.location.hash;
function pollHash() {
	var recentHash1 = recentHash.replace("#","");
	var arr = recentHash1.split("/");

	var currentHash = window.location.hash;
	currentHash = currentHash.replace("#","");
	var arr2 = currentHash.split("/");

	//alert(window.location.hash+recentHash);

	if (window.location.hash==recentHash) {
		return;
	}
	recentHash = window.location.hash;
	if(jQuery.inArray(arr[1], modules_lightbox)<0 || jQuery.inArray(arr2[2], modules_content)<0) {
		trigger_url(recentHash);
	}
	else {
		finish_lightbox();
	}
}

var per_page = 4;
var carousel_width = 384;

function create_pf_navigation() {
	$('div#logo_list ul').css({
		overflow: 'hidden'
	});
	var carousel_elements = $('#logo_list li').each(function(i) {
		this.id = 'element_'+i;
	});
	$('#next').attr('name', 0);
	$('#prev').attr('name', -per_page);

	var carousel_length = carousel_elements.length;
	var pages = Math.ceil(carousel_length/per_page);
	var i = 0;
	while(i<pages) {
		$('div#pagination').append('<div class="page" id="page_'+i+'">&nbsp;</div>');
		if(i==0) {
			$('#page_'+i).addClass('active_page');
		}
		i++;
	}
	$('#next').click(function() {
		var ktory = $(this).attr('name');
		move(1, ktory);
	});

	$('#prev').click(function() {
		var ktory = $(this).attr('name');
		move(-1, ktory);
	});

	$('.page').click(function() {
		var page = $(this).attr("id");
		page = page.substring(5);
		var active_obj = $('#pagination .active_page');
		var active_page = active_obj.attr("id");
		active_page = active_page.substring(5);
		//alert(page-active_page);
		var pages = page-active_page;
		if(pages!=0) {
			if(pages<0) {
				var start = parseInt($('#next').attr('name'))+((page-active_page)*per_page);
			}
			else {
				var start = parseInt($('#prev').attr('name'))+((page-active_page)*per_page);
			}
			move(page-active_page, start);
		}
	});
}

function move(pages, start) {
	var carousel_elements = $('#logo_list li');
	var carousel_length = carousel_elements.length;

	start = parseInt(start);
	pages = parseInt(pages);
	//alert(pages+" "+start);
	var position = $('#logo_list ul').position();
	$('#logo_list ul').animate({
		left: (position.left-(pages*carousel_width))+"px"
	} ,100,'',usun(start, per_page*pages, pages));
	$('.page').removeClass('active_page');
	if(pages>0){
		if(start>=0) {
			var p = (((start%carousel_length)/per_page)+1)%(carousel_length/per_page);
		}
		else {
			var p = (((start%carousel_length)/per_page)+1+(carousel_length/per_page))%(carousel_length/per_page);
		}
	}
	else {
		if(start>=0) {
			var p = (start%carousel_length)/per_page;
		}
		else {
			var p = (((start%carousel_length)/per_page)+(carousel_length/per_page))%(carousel_length/per_page);
		}
	}
	//alert(p);
	$('#page_'+p).addClass('active_page');
	pageTracker._trackPageview('/portfolio/page/'+p);
}

function usun(start, ile, kierunek) {
	start = parseInt(start);
	ile = parseInt(ile);
	i=start;
	var first = $('#logo_list li:first');
	var ost = $('#logo_list li:last');
	if(kierunek>0) {
		ost = parseInt(ost.attr("id").substring(8))+1;
		first = parseInt(first.attr("id").substring(8));
		var j=0;
		while(j<ile) {
			var img = $('#logo_list li#element_'+(first+j)+' img');
			img  = img.attr("src");
			$('#logo_list li#element_'+(first+j)).remove();
			$('#logo_list ul').append('<li id="element_'+(ost+j)+'"><img src="'+img+'" alt="" /></li>');
			i++;
			j++;
		}

		/*** do poprawy !!! ***/
		var position = $('#logo_list ul').position();
		$('#logo_list ul').css({
			//'margin-left': ((1+(start/ile))*ile*86)+'px'
			'margin-left': -(position.left-(kierunek*carousel_width))+'px'
		})
		/*** koniec ***/

		$('#prev').attr('name', i-ile);
		$('#next').attr('name', i-ile+per_page);
	}
	else {
		ost = parseInt(ost.attr("id").substring(8));
		first = parseInt(first.attr("id").substring(8))-1;
		var j=0;
		//while(i>(start-ile)) {
		while(j<-ile) {
			var img = $('#logo_list li#element_'+(ost-j)+' img');
			img  = img.attr("src");
			$('#logo_list li#element_'+(ost-j)).remove();
			$('#logo_list ul').prepend('<li id="element_'+(first-j)+'"><img src="'+img+'" alt="" /></li>');
			i--;
			j++;
		}
		/*** do poprawy !!! ***/
		var position = $('#logo_list ul').position();
		$('#logo_list ul').css({
			//'margin-left': (((1+(start/ile))*ile*86)-(ile*86))+'px'
			'margin-left': -(position.left-(kierunek*carousel_width))+'px'
		})
		/*** koniec ***/
		$('#prev').attr('name', i-ile-per_page);
		$('#next').attr('name', i-ile);
	}
}

$(document).ready(function() {
	setInterval(pollHash, 500);
	$('div#kontakt_subpage').remove();
	$('div#logo_list ul').css({
		overflow: 'hidden'
	});
	$('div.product div').hide();
	$('a[rel*=news_details]').lightBox();

	var hash = document.location.hash;
	if(hash) {
		trigger_url(hash);
	}
	/** funkcje do strony startowej **/
	$('div.start_txt div').css({
		height: 0,
		overflow: 'hidden'
	});
	$('.dzial').hide();
	$('#s_ad').fadeIn('slow', function() {
		$('#s_nm').fadeIn('slow', function() {
			$('#s_aw').fadeIn('slow', function() {
				$('#s_4d').fadeIn('slow');
			})
		});
	});
	$('#dzialy a').hover(function() {
		var id = $(this).parent('div').attr("id").slice(-2);
		var p = $('#txt_'+id).find('div');
		var pr = $('#prod_'+id);
		p.stop(true, true);
		$(this).stop(true);
		pr.stop(true, true);
		$(this).fadeTo(300, 0);
		pr.fadeIn(300);
		//p.slideDown(300);
		p.animate({
			height: '200px'
		}, 300);
		
	},
	function() {
		var id = $(this).parent('div').attr("id").slice(-2);
		var p = $('#txt_'+id).find('div');
		var pr = $('#prod_'+id);
		p.stop(true, true);
		$(this).stop(true);
		pr.stop(true, true);
		$(this).fadeTo(300, 1);
		pr.fadeOut(300);
		p.slideUp(300);
	});

	/** koniec strony startowej **/

	/** funkcje budujące nawigację po logotypkach **/
	create_pf_navigation();
	/** **/

	$('#next').click(function() {
		var ktory = $(this).attr('name');
		move(1, ktory);
	});

	$('#prev').click(function() {
		var ktory = $(this).attr('name');
		move(-1, ktory);
	});

	$('.page').click(function() {
		var page = $(this).attr("id");
		page = page.substring(5);
		var active_obj = $('#pagination .active_page');
		var active_page = active_obj.attr("id");
		active_page = active_page.substring(5);
		//alert(page-active_page);
		var pages = page-active_page;
		if(pages!=0) {
			if(pages<0) {
				var start = parseInt($('#next').attr('name'))+((page-active_page)*per_page);
			}
			else {
				var start = parseInt($('#prev').attr('name'))+((page-active_page)*per_page);
			}
			move(page-active_page, start);
		}
	});

	$("a#blog").hover(function() {
		przesun($("a#blog"), 0, -5, 0, 0);
	},
	function () {
		przesun($("a#blog"), 0, 0, 0, 0);
	});

	$("a#praca").hover(function() {
		przesun($("a#praca"), 0, -5, 0, 0);
	},
	function () {
		przesun($("a#praca"), 0, 0, 0, 0);
	});

	$("a#press-room").hover(function() {
		przesun($("a#press-room"), 0, -5, 0, 0);
	},
	function () {
		przesun($("a#press-room"), 0, 0, 0, 0);
	});

	$("a#oferta").hover(function() {
		przesun($("a#oferta"), 0, -5, 0, 0);
	},
	function () {
		przesun($("a#oferta"), 0, 0, 0, 0);
	});

	$("div#rss-cont").hover(function() {
		$(this).stop(true);
		$(this).animate({
			backgroundPosition: "2px top",
			width: "40px",
			right: "37px"
		}, 100)
	},
	function () {
		$(this).stop(true);
		$(this).animate({
			backgroundPosition: "0px top",
			width: "38px",
			right: "39px"
		}, 100)
	})

	$(".dzial-link").click(function(e) {
		e.preventDefault();
		content_click($(this));
	});

	$(".tab-link").click(function(e) {
		e.preventDefault();
		oferta_click($(this));
	});

	$(".page-link").click(function(e) {
		e.preventDefault();
		next_prev_click($(this));
	});

	$(".oferta-link").click(function(e) {
		e.preventDefault();
		oferta_click($(this));
	});

	$('ul#archive>li span.month').click(function() {
		alert('t');
		$(this).find('ul').toggle();
	});
});