function rotator(straight){
	var curr = $('.top_news_txts:visible').index('.top_news_txts');
	if(straight){
	  var next = 1;
	  if(curr == $('.top_news_txts').length-1) next = 0;
	  else next = curr + 1;
	}
	//reverse
	else{
		if(curr == 0) next = $('.top_news_txts').length-1;
		else next = curr-1;
	}

	if($('#head_img1')) var hots = $('#top_news_img').find('.headliner_container');
  var hots_txt = $('#top_news_txt').find('.top_news_txts');
  var hots_dgt = $('#digits_rotator').find('.headliner_digit');

  $(hots[curr]).fadeOut('slow');
  $(hots_txt[curr]).fadeOut('slow');
  $(hots_dgt[curr]).removeClass('active_digit');

  var count_to = next + 1;

  $(hots[next]).fadeIn('slow');
  $(hots_txt[next]).fadeIn('slow');
  $(hots_dgt[next]).addClass('active_digit');
}

function gallery_rotator(type, straight){
//	alert($('#right_'+type+'s_container').html());
	var curr = $('#right_'+type+'s_container').find('.gallery_right_'+type+'_txt:visible').index('.gallery_right_'+type+'_txt');
	if(straight){
	  var next = 1;
	  if(curr == $('#right_'+type+'s_container').find('.gallery_right_'+type+'_txt').length-1) next = 0;
	  else next = curr + 1;
	}
	//reverse
	else{
		if(curr == 0) next = $('#right_'+type+'s_container').find('.gallery_right_'+type+'_txt').length-1;
		else next = curr-1;
	}

	if($('#right_'+type+'1')) var hots = $('#right_'+type+'s_container').find('.gallery_right');
  var hots_txt = $('#right_'+type+'s_container').find('.gallery_right_'+type+'_txt');
  var hots_pointers = $('#right_'+type+'s_container').find('.gallery_right_'+type+'_pointer');
  var hots_dgt = $('#right_'+type+'s_container').find('.right_gallery_digits').find('.right_galleries_digit');

  $(hots[curr]).fadeOut('slow');
  $(hots_txt[curr]).fadeOut('slow');
  $(hots_pointers[curr]).hide();
  $(hots_dgt[curr]).removeClass('active_digit');

  var count_to = next + 1;

  $(hots[next]).fadeIn('slow');
  $(hots_pointers[next]).show();
  $(hots_txt[next]).fadeIn('slow');
  $(hots_dgt[next]).addClass('active_digit');
}

function photogallery_rotator(straight){
	var photos = $('.photo');
	var thumbs = $('.th_content');
	var curr = $('.photo:visible').index('.photo');
	var next = 1;
	
	if(straight){
	  if(curr == $('.photo').length-1) next = 0;
	  else next = curr + 1;
	}
	//reverse
	else{
		if(curr == 0) next = $('.photo').length-1;
		else next = curr-1;
	}
	$(photos[curr]).fadeOut('slow');
	$(thumbs[curr]).removeClass('active_th');
	
	$(photos[next]).fadeIn('slow');
	$(thumbs[next]).addClass('active_th');
}


function start_cycle(){
  $(document).everyTime(5000, "cycle", function(i){
		rotator(true);
  });
}

function start_gallery_cycle(type){
  $(document).everyTime(3000, type+"cycle", function(i){
		gallery_rotator(type, true);
  });
}

function photogallery_slideshow(){
  $(document).everyTime(5000, "photogallery", function(i){
		photogallery_rotator(true);
  });
}

function menu_tabs_bind_mouse_event(){
  $('.menu_tabs').mouseover(function(){
    $(this).addClass('active_tab');
  });
  $('.menu_tabs').mouseout(function(){
    $(this).removeClass('active_tab');
  });
}

function gallery_initiator(type){
  start_gallery_cycle(type);
	if($('#right_'+type+'1')){
    $('#right_'+type+'s_container').find('.gallery_right_'+type+'_pointer').mouseover(function(){
	    $(document).stopTime(type+"cycle");
    });
    $('#right_'+type+'s_container').find('.gallery_right_'+type+'_pointer').mouseout(function(){
	    start_gallery_cycle(type);
  	});
	}
  $('#right_'+type+'s_container').find('.gallery_right_'+type+'_txt').mouseover(function(){
    $(document).stopTime(type+"cycle");
  });
  $('#right_'+type+'s_container').find('.gallery_right_'+type+'_txt').mouseout(function(){
    start_gallery_cycle(type);
  });
  $('#right_'+type+'s_container').find('.right_'+type+'_scrollers').click(function(){
    $(document).stopTime(type+"cycle");
		if($(this).attr("id") == 'right_'+type+'_ff') gallery_rotator(type, true);
		else gallery_rotator(type, false);
  });
}
 
$(document).ready(function(){

	menu_tabs_bind_mouse_event();

  $('.menu_tabs').each(function(i){
    //alert(this);
    $(this).click(function(){
			//alert($(this).attr("id"));
			var item_slider;
			var this_id = $(this).attr("id")
			switch(this_id){
				case 'news_tab':
					item_slider = 'news_slider';
					break;
				case 'galleries_tab':
					item_slider = 'galleries_slider';
					break;
				case 'party_tab':
					item_slider = 'party_slider';
					break;
				case 'contacts_tab':
					item_slider = 'contacts_slider';
					break;
			}
      if( $('#menu_slider').is(':visible') ){
    		$(this).unbind('mouseout');
  			$('.active_tab:not(#'+this_id+')').mouseover(function(){
  			  $(this).addClass('active_tab');
  			});
  			$('.active_tab:not(#'+this_id+')').mouseout(function(){
  			  $(this).removeClass('active_tab');
  			});
				$('.active_tab:not(#'+this_id+')').removeClass('active_tab');
      	if( $('#'+item_slider).is(':visible') ){
					$('#'+item_slider).fadeOut(400);
					$('#menu_slider').delay(400).slideToggle();
					$('#'+this_id).removeClass('active_tab');
  				$(this).mouseout(function(){
				    $(this).removeClass('active_tab');
				  });
				}else{
					$('.item_slider:visible').fadeOut(400);
					$('#'+item_slider).delay(400).fadeIn(400);
				}
			}else{
    		$(this).unbind('mouseout');
				$('#menu_slider').slideToggle();
				$('#'+item_slider).delay(400).fadeIn(400);
			}
    });
  });
	if($('#head_txt1')){
    start_cycle();
		if($('#head_img1')){
	    $('.headliner_container').mouseover(function(){
  	    $(document).stopTime("cycle");
	    });
	    $('.headliner_container').mouseout(function(){
  	    start_cycle();
    	});
		}
    $('.top_news_txts').mouseover(function(){
      $(document).stopTime("cycle");
    });
    $('.top_news_txts').mouseout(function(){
      start_cycle();
    });
    $('.tops_scrollers').click(function(){
      $(document).stopTime("cycle");
			if($(this).attr("id") == "top_ff") rotator(true);
			else rotator(false);
    });
	}
	if($('#right_photo_txt1')){
		if($('.gallery_right_photo_txt').length > 1) gallery_initiator('photo');
	}
	if($('#right_video_txt1')){
		if($('.gallery_right_video_txt').length > 1) gallery_initiator('video');
	}
	
	if($('#main_photo_box')){
		photogallery_slideshow();
		var playing = true;
		$('#gallery_rw').click(function(){
			$('#gallery_play').find('img').attr('src', '/images/photo_show/play.png');
			playing = false;
			$(document).stopTime("photogallery");
			photogallery_rotator(false);
		});

		$('#gallery_ff').click(function(){
			$('#gallery_play').find('img').attr('src', '/images/photo_show/play.png');
			playing = false;
			$(document).stopTime("photogallery");
			photogallery_rotator(true);
		});
		$('#gallery_play').click(function(){
			if(playing){
				$('#gallery_play').find('img').attr('src', '/images/photo_show/play.png');
				$(document).stopTime("photogallery");
				playing = false;
			}else{
				$('#gallery_play').find('img').attr('src', '/images/photo_show/pause.png');
				photogallery_slideshow();
				playing = true;
			}
		});
		$('.th_content').click(function(){
			$('#gallery_play').find('img').attr('src', '/images/photo_show/play.png');
			$(document).stopTime("photogallery");
			playing = false;
			var photos = $('.photo');
			var thumbs = $('.th_content');
			var pos = $('.th_content').index($(this));
			var curr = $('.photo:visible').index('.photo');

			$(photos[curr]).fadeOut('slow');
			$(thumbs[curr]).removeClass('active_th');
	
			$($('.photo').get(pos)).fadeIn('slow');
			$($('.th_content').get(pos)).addClass('active_th');
		});
	}
});
