/*
Author:
Sanjin Celeski
Kreativni odjel
www.kreativni.hr
*/

$(document).ready(function() {


	$('.hide').hide();

	$('[data-click]').click(function(){
		var target = $(this).attr('data-click');
		var $target = $(target);
		$target.slideToggle();
	});
	
	$('[data-resize]').click(function(){
		var width = $(this).attr('data-resize');
		var ratio = $(this).attr('height') / $(this).attr('width');
		var height = width * ratio;
	    $(this).animate({
		    width: width,
			height: height
		}, 500, function() {
		    // Animation complete.
			var position = Math.round(($('.overview').position().top)*(-1));
			scroller.tinyscrollbar_update(position);
		});
	});	


	/************************************* Tiny Scrollbar *************************************/

	if( $('#main').length > 0 ) {
		var scroller = $('#main');
		scroller.tinyscrollbar();
	}


	/****************************** Tiny Scrollbar (CUSTOM BUTTONS) ********************************/

	scroll_step = 100;
	scroll_stop = 50;
	scroll_container = $('.viewport').height();

	$('.scroll.up').click(function(){
		var from = Math.round(($('.overview').position().top)*(-1));
		var to = from - scroll_step;
				
		if( from < 50 ) {
			scrolling(from,from-scroll_stop,scroll_container);
			setTimeout(function(){ scrolling(from-scroll_stop,0,scroll_container) },250);
		} else {
			if(to < 0) to = 0;
			scrolling(from,to,500);
		}
	});

	$('.scroll.down').click(function(){
		var from = Math.round(($('.overview').position().top)*(-1));
		var to = from + scroll_step;
		
		if( to > $('#content').height() - scroll_container ) {
			// step 1 (scroll over)
			to = from+scroll_stop;
			scrolling(from,to,250);
			// step 2 (scroll back)
			from = $('#content').height() - scroll_container;
			setTimeout(function(){ scrolling(to,from,250) },250);
		} else {
			if( to > $('#content').height() ) to = to + $('#content').height() - scroll_container;
			scrolling(from,to,500);
		}
	});

	function scrolling(from,to,duration) {
		
		var from = {property: from};
		var to = {property: to};		

		jQuery(from).animate(to, {
			duration: duration,
			step: function() {
				scroller.tinyscrollbar_update(Math.round(this.property));
			}
		});

	}


	/************************************ INTRO LOGO ROTATION *******************************************/

	var rotation = function (){
	  $("#intro-lang img").rotate({
	     angle:0,
	     animateTo:360*100, // IE fix (*100)
	 	 duration:4000*100, // IE fix (*100)
	     callback: rotation,
	     easing: function (x,t,b,c,d){        // t: current time, b: begInnIng value, c: change In value, d: duration
	         return c*(t/d)+b;
	     }
	  });
	}
	if( $('#intro-lang').length > 0 ) rotation();
		
	
	/*********************************** BREADCRUMB GENERATION *******************************************/
	
	function breadcrumb(){
		$('#breadcrumb').html('');
		// FIRST
		var href = $('#menu .active').attr('href');
		var html = $('#menu .active').html();
		$('#breadcrumb').append("<a href='"+href+"'>"+html+"</a");
		// SECOND
		var href = $('#submenu .active').attr('href');
		var html = $('#submenu .active').html().toUpperCase();
		if(href!=null) $('#breadcrumb').append("<a href='"+href+"'>"+html+"</a");
	}	
	if( $('#breadcrumb').length > 0 ) breadcrumb();


	/*********************************** GOOGLE MAPS *******************************************/

	function initialize() {

		// Poreč

		if(lang=="hr") my_title = "CENTRALA POREČ";
		if(lang=="en") my_title = "POREC CENTRAL";

		myLocation = new Array(); // Poreč
		myLocation["LatLng"] = new google.maps.LatLng(45.227129,13.597767);
		myLocation["Content"] = "";
		myLocation["Content"] += "<strong>"+my_title+"</strong>";
		myLocation["Content"] += "<br/>";
		myLocation["Content"] += "M. Grahalića 1, Poreč";
		myLocation["Content"] += "<br/>";
		myLocation["Content"] += "T +385 51 427 520";

	    var myOptions = {
	      zoom: 15,
	      center: myLocation["LatLng"],
		  scrollwheel: false,
	      mapTypeId: google.maps.MapTypeId.ROADMAP
	    };
	    var map1 = new google.maps.Map(document.getElementById("map1"), myOptions);

	    var infowindow1 = new google.maps.InfoWindow({
	        content: myLocation["Content"]
	    });

	    var marker1 = new google.maps.Marker({
	        position: myLocation["LatLng"],
	        map: map1
	    });

	    google.maps.event.addListener(marker1, 'click', function() {
	      infowindow1.open(map1,marker1);
	    });


		// Rijeka

		if(lang=="hr") my_title = "POSLOVNICA RIJEKA";
		if(lang=="en") my_title = "RIJEKA BRANCH";

		myLocation = new Array();
		myLocation["LatLng"] = new google.maps.LatLng(45.352326,14.373357);
		myLocation["Content"] = "";
		myLocation["Content"] += "<strong>"+my_title+"</strong>";
		myLocation["Content"] += "<br/>";
		myLocation["Content"] += "D. Tadijanovića 5, Rijeka";
		myLocation["Content"] += "<br/>";
		myLocation["Content"] += "T +385 51 493 510";

	    var myOptions = {
	      zoom: 11,
	      center: myLocation["LatLng"],
		  scrollwheel: false,
	      mapTypeId: google.maps.MapTypeId.ROADMAP
	    };
	    var map2 = new google.maps.Map(document.getElementById("map2"), myOptions);

	    var infowindow2 = new google.maps.InfoWindow({
	        content: myLocation["Content"]
	    });

	    var marker2 = new google.maps.Marker({
	        position: myLocation["LatLng"],
	        map: map2
	    });

	    google.maps.event.addListener(marker2, 'click', function() {
	      infowindow2.open(map2,marker2);
	    });
	

  	}

	if( $('#map1').length > 0 ) initialize();


	/*************************************** REFERENCE *******************************************/

    // IE bug fix
	$('#referentna-lista > a').click(function(){
		if ( $.browser.msie == undefined ) return;
		if ( $.browser.msie && $.browser.version > 7 ) return;
		var html = $(this).html().toUpperCase();
		$('#breadcrumb').append("<a class='third'>"+html+"</a");
		$('#referentna-lista > a').hide();
		$(this).next().show();
		scroller.tinyscrollbar_update();
	});


	$('#referentna-lista section > a').click(function(){

		// fix jumping
		$(this).next("div").toggle();
		var height = $(this).next("div").height();
		$(this).next("div").height(height);
		$(this).next("div").toggle();

		$(this).next("div").slideToggle(function(){
			var position = Math.round(($('.overview').position().top)*(-1));
			scroller.tinyscrollbar_update(position);
		});
		return false;
		
	});
	
	
	function reference() {
		if(window.location.hash) {
			var element = $("#referentna-lista a[href='"+window.location.hash+"']");
			var html = element.html().toUpperCase();
			$('#breadcrumb').append("<a class='third'>"+html+"</a");
			$('#referentna-lista > a').hide();
			$(element).next().show();
			scroller.tinyscrollbar_update();
		} else {
			$('#referentna-lista > a').show();
			$('#referentna-lista div').hide();
			$('#breadcrumb a').eq(2).remove();
		}
	}

	$(window).bind( 'hashchange', function() {
		if( $('#referentna-lista').length > 0 ) reference();
	});

	if( $('#referentna-lista').length > 0 ) reference();


	/*************************************** KONTAKT *******************************************/

	function email() {

		var ime = $("form input[name='ime']").val();
		var tvrtka = $("form input[name='tvrtka']").val();
		var email = $("form input[name='email']").val();
		var opis = $("form textarea[name='opis']").val();
		
		if(opis=="") return false;
		
		$.post(base_url+"index.php/email/", { ime:ime, tvrtka:tvrtka, email:email, opis:opis },
		   function(data) {
			 if(lang=="hr") $("form.kontakt").html("Hvala.");
			 if(lang=="en") $("form.kontakt").html("Thank you.");
	    });
				
	}
	
	$("form.kontakt input[type='submit']").click(function(e){
		e.preventDefault();
		email();
	});


	/**************************** GALLERY ******************************/

   $('a[rel^=galerija]').lightBox({
	overlayBgColor: '#FFF',
	overlayOpacity: 0.6,
	containerResizeSpeed: 350
   });


});
