// menu
var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;
var flag = false;
var newCommentId = 1;
var newPostId = 1;

// main slider
var width = 442;
var right = 1;
var wait = false;
var enter = false;
var interval = 6000;
var obj;

$(document).ready(function() {  
  // main nav drop-downs
  $('#main_nav_list > li').bind('mouseover', jsddm_open)
  $('#main_nav_list > li').bind('mouseout',  jsddm_timer)
  
  // main slider
  obj = document.getElementById("slider-part");
  //timer = setInterval("scroll()",interval);

  $("#slider-main").mouseenter(function(){
	enter = true;
  });
  $("#slider-main").mouseleave(function(){
	enter = false;
  });

    $("#main-media1").click(function(){
	  wait = true;
	  $("#slider-part").animate({scrollLeft:width*0},500);
	});
	$("#main-media2").click(function(){
	  wait = true;
	  $("#slider-part").animate({scrollLeft:width*1},500);
	});
	$("#main-media3").click(function(){
	  wait = true;
	  $("#slider-part").animate({scrollLeft:width*2},500);
	});
	$("#main-media4").click(function(){
	  wait = true;
	  $("#slider-part").animate({scrollLeft:width*3},500);
    });
  
  // log in form - every page
  $("body").click(function(){
    if (flag)
	  $(".login-form").hide(300);
  });
  $("#login_btn").click(function(){	
	if ($(".login-form").css("display")=="block") {$(".login-form").fadOut(300);} else {$(".login-form").slideDown(500);};
	return false;
  });
  $(".login-form").mouseenter(function() {
	flag = false;
  });
  $(".login-form").mouseleave(function() {
	flag = true;
  });
  
  // all pages
  $('#ajax1').hide();
  $('#ajax2').hide();
  $('#ajax5').hide();
  $('#ajaxResult1').hide();
  $('#ajaxResult2').hide();
  $('#ajaxResult5').hide();
  $('#resInfo').hide();
  $.localScroll(); // {target: '#content'}
  
  // adding new image gallery page
  $('#addTitle').blur(function(){
    $('#ajax1').show();
	$('#ajax1').fadeOut();
	var thisitem = $('#addTitle').val();
	var itemlen = thisitem.length;
	if (itemlen < 5) { setTimeout("finishAjax('ajaxResult1', 'Title too short', 'ajax1')", 400); }
	else { 
	  $('#ajaxResult1').fadeOut("slow"); 
	}
    return false;
  });
  
  $('#addDescr').blur(function(){
    $('#ajax2').show();
	$('#ajax2').fadeOut();
	var thisitem = $('#addDescr').val();
	var itemlen = thisitem.length;
	if (itemlen < 5) { setTimeout("finishAjax('ajaxResult2', 'Description too short', 'ajax1')", 400); }
	else { 
	  $('#ajaxResult2').fadeOut("slow"); 
	}
    return false;
  });
  
  // adding new video page
  $('#videoURL').blur(function(){
	  $('#ajax2').show();
      $.post("/ajax.php?action=getVideoInfo", 
		{ s: $('#videoURL').val() }, 
	    function(response){
		  if (response != "empty") { 
            setTimeout("finishAjax('resInfo', '"+escape(response)+"', 'ajax2', 'slidedown')", 400);
		  }
    });
    return false;
  });
  
  // resource activity on resource page
  $('#resActivity').change(function(){
	  $('#ajax5').show();
      $.post("/ajax.php?action=setResActivity", 
		{ id: $('#resActivity').attr("title"), val: $('#resActivity').val() }, 
	    function(response){
          $('#ajax5').fadeOut();
          setTimeout("finishAjax('ajaxResult5', '"+escape(response)+"')", 400);
    });
    return false;
  });
  
  // comments
  $("a[id^=commentQuote]").click(function(){
    s = new String($(this).attr("id"));
	id = s.substr(12);
	var quote = escape ($('#commentText'+id).html());
	//alert (unescape (quote));
	$("#commentFormTxt").val('<blockquote>' + unescape(quote) + '</blockquote>');
	$('#commentAdd').localScroll();
	$("#commentFormTxt").focus();
  });
  
  $('#commentSubmit').click(function(){
	  $('#ajax1').fadeIn();
      $.post("/ajax.php", 
		{ action: $('#commentFormAction').val(), uri: $('#commentURI').val(), val: $("#commentFormTxt").val() }, 
		function(response){
        if (response != "failure") { setTimeout("addComment('"+escape(response)+"')", 500); }
		else { 
		  $('#ajax1').hide();
		  $('#commentResponse').fadeIn(600);
		}
      });
      return false;
  });
  
  
  // forum thread posts
  $("a[id^=postQuote]").click(function(){
    s = new String($(this).attr("id"));
	id = s.substr(9);
	var quote = escape ($('#postText'+id).html());
	//alert (unescape (quote));
	$("#postFormTxt").val('<blockquote>' + unescape(quote) + '</blockquote>');
	$('#addreply').localScroll();
	$("#postFormTxt").focus();
  });
  
  $('#postSubmit').click(function(){
	  $('#ajax1').fadeIn();
	  	  
      $.post("/ajax.php", 
		{ action: $('#postFormAction').val(), thread_id: $('#threadID').val(), val: $("#postFormTxt").val(), newPostId: newPostId }, 
		function(response){
        if (response != "failure") { setTimeout("addPost('"+escape(response)+"')", 500); }
		else { 
		  $('#ajax1').hide();
		  $('#postResponseError').fadeIn(600);
		}
      });
      return false;
  });
});
   
function jsddm_open()
{  jsddm_canceltimer();
   jsddm_close();
   ddmenuitem = $(this).find('ul').css('visibility', 'visible');}

function jsddm_close()
{  if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}

function jsddm_timer()
{  closetimer = window.setTimeout(jsddm_close, timeout);}

function jsddm_canceltimer()
{  if(closetimer)
   {  window.clearTimeout(closetimer);
      closetimer = null;}}

document.onclick = jsddm_close; 

function finishAjax (id, response, ajaxId, slide) {
  $('#'+ajaxId).fadeOut();
  $('#'+id).html(unescape(response));
  if (slide == 'slidedown') { $('#'+id).slideDown(); }
  else { $('#'+id).fadeIn(); }
}

function addComment (response) {
  $("#commentFormTxt").val('');
  $('#ajax1').hide();
  $('#commentNew').append('<div class="comment" id="commentNew' + newCommentId + '" style="display:none">' + unescape(response) + '</div>');
  if ($('#commentNew').css('display') == 'none') { 
    $('#commentNew' + newCommentId).show();
    $('#commentNew').slideDown(600); 
  }
  else { $('#commentNew' + newCommentId).slideDown(600); }
  newCommentId = newCommentId + 1;
}

function addPost (response) {
	$('#newPosts').append(unescape(response));
	$('#ajax1').hide();
	$('#postNew' + newPostId).slideDown(600);
	$("#postFormTxt").val('');
	newPostId = newPostId + 1;
}

// main slider
function scroll()
{
	if (obj.scrollLeft==0)
		right = 1;
	else if (obj.scrollLeft==width*3)
		right = -1;
	if (!wait && !enter) {
		num = obj.scrollLeft / width + 1 + right;
		$("a[id^=main-media]").removeClass("active"); 
		$("#main-media"+num).addClass('active');
		$("#slider-part").animate({scrollLeft:right*width+obj.scrollLeft},500);
	}
		wait = false;
}

$(function(){
  $("a[id^=main-media]").click(function(){
  $("a[id^=main-media]").removeClass("active");
  $(this).addClass('active');
  });
}); 