

var current_image = 0;
var current_thumb_pos = 0;
var current_image_loaded = 1;
var is_loading_news = false;
var showing_contact = false;
var showNext = false;

/*var browserIE = {
	  version: function() {
	    var version = 999;
	    if (navigator.appVersion.indexOf("MSIE") != -1)
	      version = parseFloat(navigator.appVersion.split("MSIE")[1]);
	    return version;
	  }
	}*/

function next_image() {
  current_image++;
  if (current_image > images.length - 1) {
    current_image = 0;
  }
  fade_image();
}

function prev_image() {
  current_image--;
  if (current_image < 0) {
    current_image = images.length - 1;
  }
  fade_image();
}

function show_image(image) {
  current_image = image;
  fade_image();
}

function fade_image() {
  var $show_image = $('<img />').attr('src',  images[current_image]);
  $show_image.fadeTo(0,0);
  $('#image').append($show_image);
  $show_image.fadeTo(200, 1, function() {
    $('#image img').first().remove();
  });
  $('.portofolio-image-title').text(titles[current_image]);
  var $show_image = $('<img />').attr('src',  images[current_image + 1]);
  $('#preloader').html($show_image);
}

function ajax_load_feed(){
  if (isFeed) {
    if (!is_loading_news) {
      if  ($(window).scrollTop() > $(document).height() - $(window).height() - 2000){
        is_loading_news = true;
        //alert("WOHA");
        $.ajax({
          url: '/templates/ajax-news-feed.php?start_pk=' + startLoadFeedPk + '&load_page_no=' + loadPageNo,
          success: function(data) {
            $('#right-col').append(data);
            //alert(jQuery.trim(data).length);
            if (jQuery.trim(data).length > 0) {
              is_loading_news = false;
            }
          }
        });
        loadPageNo++;
      }
    }
  }
}
  
  
if (isMobile()) {
  loadCss('/css/mobile.css');
}


$(document).ready(function() {

  $('#frontpage-image').nivoSlider();
        
        
  if(pageType == 'node_gallery') {
    $('#image').click(function(eventObj) {
      var location = $(this).offset();
      var x = eventObj.pageX - location.left;
      if (x < 435) {
        prev_image();
      } else {
        next_image();
      }
      //next_image();
    });
    $('.next-image').click(function() {
      next_image();
    });
    $('.prev-image').click(function() {
      prev_image();
    });
    var image1 = $('<img />').attr('src',  images[current_image + 1]);
  }

  /*if (browserIE.version() <= 7) {
    
  }*/
  
  
  // Calendar
  $('.calendar-prev').click(function() {
    i = 0;
    $('#caldendar .month').each( function(){
      if (i > 0) {
        if($(this).is(':visible')) {
          $prevMonth.fadeTo(200,1);
          $(this).fadeTo(200,0,function() {
            $(this).hide();
          });
        }
      }
      i++;
      $prevMonth = $(this);
    });
    //$('#caldendar-long').animate({left: '+=141'},400);
  });
  showNext = false;
  $('.calendar-next').click(function() {
    $('#caldendar .month').each( function(){
      if(showNext) {
        $(this).fadeTo(160,1);
        showNext = false;
        return false;
      }
      if($(this).is(':visible')) {
        showNext = true;
        $(this).fadeTo(160,0,function() {
          $(this).hide();
        });
      }
    });
    showNext = false;
    //$('#caldendar-long').animate({left: '-=141'},400);
  });
  
  
  // Kontakt
  $('#menu-contact').click(function() {
    if (showing_contact) {
      $('#contact').animate({height:0},400);
      showing_contact = false;
    } else {
      $('#contact').animate({height:200},400);
      showing_contact = true;
    }
  });
  
  if (isFeed) {
    $('html, body').animate({scrollTop:0}, 0);
    //$(window).scrollTop();
  }
  
  if (mobile) {
    if (pageType == "node_news_item" || (arg0 = "daily-operations" && arg1.indexOf('-') != -1)) {
  		$goBackBtn = $('<a class="feed-go-back">BACK TO FEED</a>');
  		$goBackBtn.click(function() {
  		  history.back();
        return false;
      });
		  $('#right-col').append($goBackBtn);
    }
		$('#right-col #show-older').remove();
  	if (currentFeedPage >= maxFeedPage) {
  		$nextFeedPage = $('');
		} else {
  		$nextFeedPage = $('<a class="feed-next-page" href="/daily-operations/' + (currentFeedPage + 1) + '">SHOW MORE</a>');
  	}
		$('#right-col').append($nextFeedPage);
	} else {
  	$(window).scroll(ajax_load_feed)
  }
  
  $('#show-older').click(function() {
    is_loading_news = true;
    isFeed = true;
    $.ajax({
      url: '/templates/ajax-news-feed.php?start_pk=' + startLoadFeedPk + '&load_page_no=' + loadPageNo,
      success: function(data) {
        $('#right-col').append(data);
        //alert(jQuery.trim(data).length);
        if (jQuery.trim(data).length > 0) {
          is_loading_news = false;
        }
      }
    });
    loadPageNo++;
    $(this).remove();
  });
	
	
  
  
});




