// JavaScript Document
$(document).ready(function(){

	$("#spec02").css("display", "none");
	$("#spec03").css("display", "none");
	
	$(".hide").css("display", "none");

	$("#newwork_container").css("display", "none");
	$("#blog_container").css("display", "none");
	
	$("#new02").css("display", "none");
	$("#new03").css("display", "none");

	$(".btn_spec").click(upaliSpec);

	$("#btn_new01").click(upaliNew);
	$("#btn_new02").click(upaliNew);
	$("#btn_new03").click(upaliNew);

	$(".btn_novo").click(upaliNovo);
	$(".btn_izdvojeno").click(upaliIzdvojeno);
	$(".btn_blog").click(upaliBlog);

	setAbbrs();

	//$("a.fancy_thumb").colorbox();
	$("a.fancy_thumb").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300, 'zoomOpacity':true, 'overlayShow': true, 'overlayOpacity': 1 });
	
	setKvaliteta ();
	setKomunikacija ();
	
	//goDown();
	resizeSite();
	
	var height;
});

function goDown() {
	//window.scroll (0, document.height);
}

function upaliNovo(e) {
	$("#newwork_container").css("display", "block");
	$("#specials_container").css("display", "none");
	$("#blog_container").css("display", "none");
	resizeSite();
}

function upaliIzdvojeno(e) {
	$("#newwork_container").css("display", "none");
	$("#specials_container").css("display", "block");
	$("#blog_container").css("display", "none");
	resizeSite();
}

function upaliBlog(e) {
	$("#newwork_container").css("display", "none");
	$("#specials_container").css("display", "none");
	$("#blog_container").css("display", "block");
	resizeSite();
}

function upaliSpec(e) {
	//console.log($(e.target).attr("rel"));
	$(".spec_div").css("display", "none");

	$(".btn_spec").attr("class", "btn_off btn_spec");


	$("#"+$(e.target).attr("rel")).css("display", "block");
	$(e.target).attr("class", "btn_on btn_spec");

	$("#navigation").css("bottom", "0px");
	$("#eng_navigation").css("bottom", "0px");

	resizeSite();
}

function upaliNew(e) {
	//console.log($(e.target).attr("rel"));
	$("#new01").css("display", "none");
	$("#new02").css("display", "none");
	$("#new03").css("display", "none");

	$("#btn_new01").attr("class", "btn_off");
	$("#btn_new02").attr("class", "btn_off");
	$("#btn_new03").attr("class", "btn_off");


	$("#"+$(e.target).attr("rel")).css("display", "block");
	$(e.target).attr("class", "btn_on");
	
	$("#navigation").css("bottom", "0px");
	$("#eng_navigation").css("bottom", "0px");

	resizeSite();
}

$(window).resize(function(){
	resizeSite();
});

function resizeSite () {
	//alert("resize");
	var container_height = $("#container").height();

	$("body").css("height", container_height+"px");
	
	$("#navigation").css("bottom", "6px");
	$("#eng_navigation").css("bottom", "6px");

	var body_height = $("body").height();
	var document_height = document.height;
	var window_height = $(window).height();
	
	//alert(container_height, body_height, document_height, window_height);

	//console.log(container_height, body_height, document_height, window_height);

	//alert (document.body.offsetHeight, document.body.scrollHeight);
	//document.body.offsetHeight = container_height+40;
	//document.body.scrollHeight = container_height+40;
	//alert ($("body").height());
	
	//alert(container_height + "," + window_height, + "," + body_height);
	//alert ("bh:" + body_height);
	
	if (container_height > window_height) {		
		$("body").css("height", container_height+40+"px");
		$("#container").css("bottom", window_height-container_height-40);
	} else {
		$("#container").css("bottom", "0px");
	}	
}

function tocke (rel) {
	$("[rel='" + rel + "'].tocka").bind("click", ocjeni);
	$("[rel='" + rel + "'].tocka").bind("mouseover", tockeOver);
	$("[rel='" + rel + "'].tocka").bind("mouseout", tockeOut);
}

function unTocke (rel) {
	//console.log(rel);
	$("[rel='" + rel + "'].tocka").unbind("click");
	$("[rel='" + rel + "'].tocka").unbind("mouseover");
	$("[rel='" + rel + "'].tocka").unbind("mouseout");
}

function ocjeni(e) {
	var rev = $(e.target).parent().attr("rev");
	var rel = $(e.target).parent().attr("rel");
	$.post('/ajax/rate', {item: rel, tocke:rev}, updatePoruka, "json");
}
function tockeOver(e) {
	var rev = $(e.target).parent().attr("rev");
	var parent_div = $(e.target).parent().parent();

	parent_div.mouseout(tockeOutAll);

	for (var i = 1; i <= rev; i++) {
		parent_div.children("[rev='"+i+"']").children("img").attr("src", "/images/tocka_big.gif");
	}
}
function tockeOut(e) {
	$(e.target).attr("src", "/images/tocka_small.gif");
}
function tockeOutAll(e) {
	var parent_div = $(e.target).parent().parent();
	for (var i = 1; i <= 5; i++) {
		parent_div.children("[rev='"+i+"']").children("img").attr("src", "/images/tocka_small.gif");
	}
}
function updatePoruka (item) {
	var broj_tocaka = item[1];
	var tocke = "";
	for (var i = 1; i <= broj_tocaka; i++) {
		tocke += '<img src="/images/tocka_small.gif" alt="tocka" />';
	}
	//console.log(item);
	$("."+item).html("<strong>donirali ste: "+tocke+"</strong>");
	unTocke (item[0]);
}

function setAbbrs () {
	//console.log($("abbr"));
	$("abbr").bind("mouseover", hoverAbbr);
	$("abbr").bind("mouseout", unhoverAbbr);
	$("abbr").each(function(i) {
		$(this).attr("rel", $(this).attr("title"));
		$(this).attr("title", "");
	});
}

function mouseMove (e) {
	//console.log(e.pageX +', '+ e.pageY);
	$("#carret").css("top", e.pageY + 20);
	$("#carret").css("left", e.pageX + 20 );
	//console.log($(window).width() - e.pageX);
	$("#carret").css("max-width", $(window).width() - e.pageX - 80);
	$("#carret").css("display", "block");
}

function hoverAbbr (e) {
	$().bind ("mousemove", mouseMove);
	$("#carret").html($(e.target).attr("rel"));
}

function unhoverAbbr (e) {
	$().unbind ("mousemove", mouseMove);
	$("#carret").css("display", "none");
	//console.log(e.target);
}

function setKvaliteta () {
	$(".group_div").css ("display", "none");
	$(".slovo_link").bind ("click", showKvaliteta);	
}

function showKvaliteta (e) {

	var divtofire = $(e.target).attr("rel");
	if (divtofire == "sve") {
		$(".slovo_div").css ("display", "block");
	} else {
		$(".slovo_div").css ("display", "none");
		$("#"+divtofire).css ("display", "block");
	}
	$(".btn_on").attr("class", "btn_off");
	$(e.target).attr("class", "btn_on");
	
	resizeSite();
}

function setKomunikacija () {
	$("#print_container").css("display", "none");
	
	$(".btn_print").bind ("click", showPrint);
	$(".btn_web").bind ("click", showWeb);	
}

function showPrint (e) {
	$("#web_container").css("display", "none");
	$("#print_container").css("display", "block");
	
	$("#navigation").css("bottom", "0px");
	$("#eng_navigation").css("bottom", "0px");
	
	if ($(e.target).attr("class") == "btn_print down") {
		goDown();
	}
	
	resizeSite();
	resizeSite();
}

function showWeb (e) {
	$("#web_container").css("display", "block");
	$("#print_container").css("display", "none");
	
	$("#navigation").css("bottom", "0px");
	$("#eng_navigation").css("bottom", "0px");
		
	//console.log($(e.target).attr("class"));
	
	if ($(e.target).attr("class") == "btn_web down") {
		goDown();
	}
	
	resizeSite();
	resizeSite();
}