jQuery(document).ready(function($){
  
 
// NAVIGATION HOVER!

  $('.sc_tab').click(function() {
  var which = $(this).attr('title');
  $(this).parent().parent().find('.sc_tab_single_box').css('display','none'); 
  $(this).parent().parent().find('.sc_tab_single_box').eq(which).css('display','block');
  });

  $('.menu-item').click(function() {
    //window.location('http://www.freshface.cz');
    window.location = $(this).find('a:first').attr('href');
    //alert(';dsds');
    //window.location($(this).find('a:first').attr('href'));
  });
    var dropdown_level = 0;
    
    $('.sub-menu').parent().find('a:first').addClass('nav_sub_arrow');
    $('#nav_wrapper .menu').children('li').children('a').addClass('top_level');
    $('#nav_wrapper .menu').children('li').children('a').removeClass('nav_sub_arrow');
    
    $('#nav_wrapper .menu-item').hover(function(){
      if(dropdown_level == 0){
            $('#nav_wrapper .menu').find('a').removeClass('nav_sub_arrow_active');  
            //$('#nav_wrapper .menu').find('a').addClass('nav_sub_arrow_passive');  
          $(this).addClass('main_hover_left');
          $(this).children('a').addClass('main_hover_right');
            $('.sub-menu').parent().find('a:first').addClass('nav_sub_arrow');
        $('#nav_wrapper .menu').children('li').children('a').addClass('top_level');
        $('#nav_wrapper .menu').children('li').children('a').removeClass('nav_sub_arrow');
      //alert('sasa');
      }                                                                                             
       

      $(this).find('.sub-menu:first').stop(true,true).slideDown(200).show();        
      $(this).find('a:first').addClass('nav_sub_arrow_active');
      $('#nav_wrapper .menu').children('li').children('a').removeClass('nav_sub_arrow_active');       
      dropdown_level++;
    },function(){            
      $(this).find('.sub-menu:first').stop(true,true).slideUp(0);
      $(this).find('a:first').removeClass('nav_sub_arrow_active');
      dropdown_level--;
       if(dropdown_level == 0){
        $(this).removeClass('main_hover_left');
          $(this).children('a').removeClass('main_hover_right');
       }
    } );
  /*  $('.sub-menu').parent().find('a:first').addClass('nav_sub_arrow');    
    $('.menu').children('li').children('a').removeClass('nav_sub_arrow');
     $('.menu').children('li').children('a').addClass('top_level');
    $('.menu').find('a').addClass('sub_menu_active');
    $('.top_level').removeClass('sub_menu_active');
    
  $('#nav_wrapper .menu-item').hover(function(){

//   alert('ds');
    if($(this).children('a:first').hasClass('top_level') == true)
    {
    //  $(this).find('a').removeClass('nav_sub_arrow_active');
      $(this).addClass('main_hover_left');
      $(this).find('a:first').addClass('main_hover_right');
    }
    else
    {
     
    }
     $(this).parent().parent().addClass('nav_sub_arrow_active');
    // $(this).find('.sub-menu').parent().children('a:first').addClass('nav_sub_arrow_active');
    dropdown_level++;
    // DROPDOWN //
    $(this).find('.sub-menu:first').stop(true,true).slideDown(200).show();
  },function(){
    dropdown_level--;
    $(this).children('a:first').removeClass('nav_sub_arrow_active');
    $(this).removeClass('main_hover_left');
    $(this).find('a:first').removeClass('main_hover_right');
    if(dropdown_level== 0)
    {
        $('#nav_wrapper .menu-item').find('a').removeClass('nav_sub_arrow_active');
    }
    //alert(dropdown_level);
    // DROPDOWN //
    
    $(this).find('.sub-menu:first').stop(true,true).slideUp(0);
  } );
   
   */
});

var $j = jQuery.noConflict();
$j(document).ready(function(){

	$j("div.scrollable").scrollable({
		size: 1,
		items: '#thumbs',
		hoverClass: 'hover',
		next:'.nextPage',
		prev:'.prevPage',
		speed: 400,
		keyboard: false,
		clickable: false
	}).navigator();
	$j('#featured .prevPage').hide();

	// get access to the API
	var api = $j("div.scrollable").data("scrollable");
	var s_total = 1;
	var s_now = 1;
	var end_reached = false;
	var paged = 2; // We already show 2 posts, so AJAX start with 3rd page.

	// do something upon scroll
	api.onSeek(function() {
		s_total = this.getSize() / 3;
		s_now = this.getIndex() + 1;
	});

	$j(".right.browse").click(function() {
		//console.info("TOtal is " + s_total + " NOw @ " + s_now);
		//if(s_total == (s_now + 2)) {
			if(end_reached) {
				//console.info("No more posts found");
			}
			else {
			//console.info("Load another item now......");
			addDivItem(); replaceDivItem();
			}
		if(s_now==1){
			$j('#featured .prevPage').show();
		}
	});

	$j(".left.browse").click(function() {
		if(s_now==2){
			$j('#featured .prevPage').hide();
		}else{
			$j('#featured .prevPage').show();
		}
	});

	$j(".scrollable .loading").live("click", function(){
    	//console.info("Working... ");
	    $j(this).remove();
	});

	function addDivItem() {
    	// get handle to scrollable API
		var api = $j("div.scrollable").data("scrollable");
	    // use API to add our new item. after the item is being added seek to the end
		api.addItem($j("#loading div.loading").clone());
		api.end();
	}

	$j('#featured .nextPage').bind("ajaxSend", function() {
		$j(this).hide();
	}).bind("ajaxComplete", function() {
		if(end_reached) { $j(this).hide(); } else { $j(this).show(); }
	});

	$j('#featured .prevPage').bind("click", function() {
		$j('#featured .nextPage').show();
	});

	function replaceDivItem() {
    	// get handle to scrollable API
		var api = $j("div.scrollable").data("scrollable");
		$j.ajax( {
			url: template_url + '/ajax.php',
			type: 'get',
			data: "paged="+paged,
			success: function( r ) {
				$j(".scrollable .loading").trigger('click');
				if($j.trim(r) == '') {
					//alert('No older posts present to show!');
					end_reached = true;
				} else {
					if($j(r).find("h2").length < 3) end_reached = true;
	    			api.addItem(r);
					paged = paged + 1;
				}
				api.end();
			},
			error: function( r ) {
				$j(".scrollable .loading").trigger('click');
				//alert('Oops! Sorry unable to load more posts!');
				api.prev();
			}
		});
	}
});

