$(function(){
    $(window).resize(function(){
        
		var size = getPageSize();
		var h = size[1];
		var w = size[0];
		var wh = size[3];
		var ww = size[2];
	
		if(wh < h) 
		{ 
			if ($.browser.msie) {
				setTimeout('$(".gallery_overlay").height('+size[1]+');',1000)
			} else
				$(".gallery_overlay").css("height",h); 			
		}
		else $(".gallery_overlay").css("height",wh);
			
		if(ww < w) $(".gallery_overlay").css("width",ww);
		else $(".gallery_overlay").css("width",ww);	
	
		var ph = $("#photos").height();
		var pw = $("#photos").width();
			
		if(wh > ph) var top = (wh/2)-(ph/2);
		else var top = 50;
		
		$("#photos").css("left",(ww/2)-(pw/2)).css("top",top);
		$("html,body").animate({scrollTop: 0}, 1000);
    });
});


$(document).ready(function() {
    $('#mycarousel').jcarousel({
    	auto: 5,
		scroll: 1,
		wrap: 'circular'
    });


	var content = $('.content_area');
	content.jScrollPane({animateTo:true});
	$('a.scroll_orange').bind('click', function() { 
		var targetElementSelectorString = $(this).attr('rel');
		content[0].scrollTo(targetElementSelectorString);
		return false;
		
	});


});


$("#k_posalji").live("click", function() {
	var k_email = $("#k_email").val();
    var k_ime_prezime = $("#k_ime_prezime").val();
    var k_telefon = $("#k_telefon").val();
    var k_text = $("#k_text").val();
	$.ajax({
		url: 'include/ajax.php',
		type: "post",
		data: "type=kontakt&k_ime_prezime="+k_ime_prezime+"&k_email="+k_email+"&k_text="+k_text+"&k_telefon+"+k_telefon,
		success: function(result) {
			if(result.match("SUCCESS!")) {
				var msg = result.split("SUCCESS!");
				alert_msg('<p>'+msg[1]+'</p>','tick');
				$("#k_ime_prezime").val("");
				$("#k_email").val("");	
				$("#k_text").val("");	
				$("#k_telefon").val("");	
			} else {
				alert_msg('<p>'+result+'</p>','error');		
			}
			sURL = unescape(window.location.pathname);
			//window.location.href = sURL;
		}
	});
});

$(".proizvodi-podgrupe").live("click", function(e) {

	var tooltip = $("div.tooltip");
	var id = $(this).attr("rel");

	tooltip.html('');
	tooltip.css("top",e.pageY-10).css("left",e.pageX-10);
	tooltip.show();

	$.ajax({
		url: 'include/ajax.php',
		type: "post",
		data: "type=proizvodi-podgrupe&id="+id,
		success: function(result) {
			tooltip.html(result);
		}
	});
});


$(".proizvodi").live("click", function(e) {
	var id = $(this).attr("rel");

	$("div#title p.title").hide();
	$("div#images").html('<div style="margin:50px 390px"><img alt="" src="gfx/ajax-loader.gif"></div>');
	$.ajax({
		url: 'include/ajax.php',
		type: "post",
		data: "type=proizvodi&id="+id,
		success: function(result) {
			var html = result.split("&&");
			$("html,body").animate({scrollTop: 180}, 1000);
			$("div#title p#proizvodi_"+id).show();
			$("div#images").html(html[0]);
			
			
			if(html[1] < 11) {
				$('#mycarousel').jcarousel({
			    	//auto: 5,
					scroll: 1
					//wrap: 'circular',
			    });
				
				$(".jcarousel-container").css("padding","10px 20px");
					$(".jcarousel-container-horizontal").css("width","820px");
					$(".jcarousel-clip-horizontal").css("width","800px");				
				$(".jcarousel-prev").css("display","none");
				$(".jcarousel-next").css("display","none");
			} else {
				$('#mycarousel').jcarousel({
			    	auto: 5,
					scroll: 1,
					wrap: 'circular'
			    });				
				if($.browser.webkit) {
					$(".jcarousel-container").css("padding","10px 20px");
					$(".jcarousel-container-horizontal").css("width","830px");
					$(".jcarousel-clip-horizontal").css("width","840px");
					$(".jcarousel-prev").css("display","none");
					$(".jcarousel-next").css("display","none");	
					$(".jcarousel-item-11").html('<img class="thumbs" alt="'+id+'" src="gfx/right.png" alt="" style="width:40px; height:40px; margin-top:10px; border:none;"/>');
				}
			}
			sURL = unescape(window.location.pathname);
			//window.location.href = sURL;
		}
	});
});


$("img.thumbs").live("click", function() {
	var ids = $(this).attr("alt").split("-");
	$(".gallery_show").show();
	var size = getPageSize();
	var h = size[1];
	var w = size[0];
	var wh = size[3];
	var ww = size[2];
			
	if(wh < h) 
	{ 
		if ($.browser.msie) {
			setTimeout('$(".gallery_overlay").height('+size[1]+');',1000)
		} else
			$(".gallery_overlay").css("height",h); 			
	}
	else $(".gallery_overlay").css("height",wh);
			
	if(ww < w) $(".gallery_overlay").css("width",ww);
	else $(".gallery_overlay").css("width",ww);
	
	$("#photos").css("width",128).css("height", 15).css("border","none").html();
	$("#photos").css("top",(wh/2)-(15/2)).css("left",(ww/2)-(128/2)).css("z-index","5000").css("position","absolute").html("<img src='gfx/ajax-loader.gif'  alt=''/>");
	
	$.ajax({
		url: 'include/ajax.php',
		type: "post",
		data: "type=gallery_show&ppg_id="+ids[0]+"&p_id="+ids[1],
		success: function(result) {
			var html = result.split("&&");
			$("#photos").html(html[0]);


			
			$('#photos').galleryView({
				panel_width: 600,
				panel_height: 475,
				frame_width: 60,
				frame_height: 60,
				transition_speed: 400,
				transition_interval: 0,
				background_color: '#333333',
				border: '2px solid black',
			    easing: 'easeInOutQuad',
				pause_on_hover: true,
				nav_theme: 'custom',
				overlay_height: 52,
				filmstrip_position: 'top',
				overlay_position: 'bottom',
				fade_panels:true
			});		
			if(html[1] == 1) {
				$("img.nav-next").hide();
				$("img.nav-prev").hide();
			}
			var ph = $("#photos").height();
			var pw = $("#photos").width();
			
			if(wh > ph) var top = (wh/2)-(ph/2);
			else var top = 50;
			
			$("#photos").css("top",top).css("left",(w/2)-(pw/2)).css("z-index","5000").css("position","absolute");
			
			$("html,body").animate({scrollTop: 0}, 1000);

		}
	});
});


$(".gallery_show").live("mouseout",function() {
	$(".gallery_overlay").live("click", function() {
		$(".gallery_show").hide();											
	});
});


$(".close_gallery").live("click", function() {
	$(".gallery_show").hide();									   
});

$("div.tooltip").live("mouseout", function() {
		$("div.tooltip").hide();	
	}).live("mouseover", function() {
		$("div.tooltip").show();	
});




function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
	}, function() {
        carousel.startAuto();
    });
};

function getPageSize(){
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {
	xScroll = window.innerWidth + window.scrollMaxX;
	yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
	xScroll = document.body.scrollWidth;
	yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
	xScroll = document.body.offsetWidth;
	yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	// console.log(self.innerWidth);
	// console.log(document.documentElement.clientWidth);
	
	if (self.innerHeight) { // all except Explorer
	if(document.documentElement.clientWidth){
	windowWidth = document.documentElement.clientWidth; 
	} else {
	windowWidth = self.innerWidth;
	}
	windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
	windowWidth = document.documentElement.clientWidth;
	windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
	windowWidth = document.body.clientWidth;
	windowHeight = document.body.clientHeight;
	}
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
	pageHeight = windowHeight;
	} else { 
	pageHeight = yScroll;
	}
	
	// console.log("xScroll " + xScroll)
	// console.log("windowWidth " + windowWidth)
	
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
	pageWidth = xScroll;
	} else {
	pageWidth = windowWidth;
	}
	// console.log("pageWidth " + pageWidth)
	
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}









