/* Hoverize function */
function hoverize(elem, hover_class){
    $(elem).hover(
      function() {
        $(this).addClass(hover_class);
      },
      function(){
        $(this).removeClass(hover_class);
      }
    );
};

/* Mark tables first and last elements */   
function table_time(){
 $('tr > td:last-child, tr > th:last-child').addClass('last');
 $('tr > td:first-child, tr > th:first-child').removeClass('last').addClass('first');
}

var active_player_id = '';

function showVideo(zeroBasedSlideIndex) {
  active_player_id = jQuery('#cycler-videos div.content-media:eq(' + zeroBasedSlideIndex + ') p').attr('id') + '-object';
  var player = document.getElementById(jQuery('#cycler-videos div.content-media:eq(' + zeroBasedSlideIndex + ') p').attr('id') + '-object');
  if (typeof player != "undefined" && player != null && typeof player.addModelListener != "undefined") {
    player.sendEvent("PLAY","true");
  }
  else {
    setTimeout(function(){showVideo(zeroBasedSlideIndex)}, 100);
  }
}

function videoSendEvent(selector, event, state) {
  var player = document.getElementById(selector);
  if (typeof player != "undefined" && player != null && typeof player.addModelListener != "undefined") {
    player.sendEvent(event,state);
  }
}

// Facebook Share

function fbs_click(url, title)
{
  u = (url ? url : location.href);
  t = (title ? title : document.title);
  window.open('http://www.facebook.com/sharer.php?u=' +
    encodeURIComponent(u) + '&t=' + encodeURIComponent(t),
    'sharer', 'toolbar=0,status=0,width=626,height=436');
  return false;
}

function collect_select(selector, check_hash) {
  if(check_hash && window.location.hash) {
    var hash = window.location.href.substr(window.location.href.indexOf("#") + 1);
    $(selector + " select").val(hash);
  }
  var str = "";
  $(selector + " select option:selected").each(function () {str = $(this).text();});
  $(selector + ' input').val(str);
}

function validate_email(email) {
  var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
  if(reg.test(email)) return true;
  return false;
}

/* On document ready do */
$(document).ready(function(){

collect_select('#collect_select', true);
$('#collect_select select').change(function(){collect_select('#collect_select', false);});

$("#tv5submitbutton").click(function(e){
  var isValid = true;
  jQuery('#tv5poll-form').find('input.halfbox:not').each(function(){
    if (jQuery(this).val() == ''){
      isValid = false;
      //jQuery(this).parent().find('label').addClass('not-valid');
      //else jQuery(this).parent().find('label').removeClass('not-valid');
    }
  });
  if (isValid==false){
    e.preventDefault();
    alert("Sva polja su obavezna pa Vas molimo da ih ispravno popunite!");
  }

});

$("#frmPosaosubmitbutton").click(function(e){
  jQuery('#frmPosao').find('.not-valid').removeClass('not-valid');
  var isValid = true;
  jQuery('#frmPosao').find('.required input').each(function(){if (jQuery(this).val() == ''){isValid = false;}});
  jQuery('#frmPosao').find('.email input').each(function(){if (!validate_email(jQuery(this).val())) isValid = false;});
  if (isValid==false){
    e.preventDefault();
    alert("Molimo Vas ispravno popunite polja označena zvjezdicom!");
  }
});

    /* Fade / Out effect */
    $("#izdvojeno ul.horizontal").show().tabs({ fx: {opacity: 'toggle' } });
    $("#izdvojeno2 ul.horizontal").show().tabs({ fx: {opacity: 'toggle'} });
    
/*     $(document).pngFix(); */
    
    /* Add support for hover on these elements in IE6*/
    if(jQuery.browser.msie && jQuery.browser.version == '6.0'){
      hoverize('span.btn', 'btn_hover');
      hoverize('#subnav li', 'li_hover');
    };

    /* Mark on */    
    table_time();
    
    /*Jump menu function for dropdowns*/
    $('.jump_menu').bind('change', function(){
      window.location.href = $(this).val();
    });
    
    
   /* RASPORED **********************************************************/    
   hoverize('.kanal-top', 'k_hover');
   hoverize('.t_program tr', 'tr_hover');
    
   /* Set entire row as link */    
    $(".t_program tr").click(function(){
      window.location.href = $(this).find('td:nth-child(2) > a')[0].href;
    });
    
    $('#raspored .kanal-program').hide();
    $('#raspored .k_active .kanal-program').show();
    
    $('#raspored .kanal-top').click(function(){
      $(this).parent().toggleClass('k_active');
      $(this).next(".kanal-program").slideToggle("slow");
    });
       


    /* Slideshow define buttons */
    function mycarousel_initCallback(carousel) {     
      jQuery('.ss-next').bind('click', function() {
            carousel.next();
            return false;
          });
          jQuery('.ss-prev').bind('click', function() {
            carousel.prev();
            return false;
          });
    };

    /* Count elements inside the slideshow as callback */
    ukupno = $('.slideshow li').length;
    info =  $('.ss-info');
    function mycarousel_itemVisibleInCallbackAfterAnimation(carousel, item, idx, state) { 
        info.html(idx +'/'+ukupno);
    };

     
   /* Start slideshow */    
   jQuery(".slideshow").jcarousel({
        visible:1,
        scroll:1,
        initCallback: mycarousel_initCallback,
        itemVisibleInCallback: {
            onAfterAnimation:  mycarousel_itemVisibleInCallbackAfterAnimation
        },
        buttonNextHTML: null,
        buttonPrevHTML: null
    });
          
          jQuery('#cycler-videothumbs').cycle({
            fx: 'scrollHorz',
            speed: 1000,
            timeout: 0,
            delay: 0,
            sync: true,
            cleartype: 1,
            next: '#cycler-videothumbs .nav-right',
            prev: '#cycler-videothumbs .nav-left'
          });
          jQuery('#cycler-videos').cycle({
            fx: 'scrollHorz',
            speed: 'normal',
            timeout: 0,
            pager: '#cycler-videothumbs',
            pagerAnchorBuilder: function(idx, slide) {return '#cycler-videothumbs div.slide-element:eq(' + idx + ') a';},
            before: function (currSlideElement, nextSlideElement, options, forwardFlag) {
              videoSendEvent(jQuery(currSlideElement).find('p').attr('id') + '-object','STOP','true');
            },
            pagerClick: function (zeroBasedSlideIndex, slideElement) {
              showVideo(zeroBasedSlideIndex);
            }
          });
          jQuery('#cycler-images a.thickbox').append('<span class="cycler-images-thickbox"><span class="cycler-images-thickbox-position"><span class="thickbox-inner-bg"></span><span class="thickbox-inner"></span></span></span>');

});



