// =============================================
//                   jQuery
// =============================================

$(document).ready(function(){
    
    /* 
    *   OPACITY FUNCTIONS WITH IE & WEBKIT RESTRICTION:
    *   they have issues with opacity for PNG images with alpha channel
    */
    if( $.browser.msie == false )
    {

        /*
        *   Main menu handling
        */
        $("#main-menu a").css( 'opacity', '0.6' );
        $("#main-menu a img[src*=active]").parent().css( 'opacity', '1' );
        
        $("#main-menu a").hover( function() {
        
            $(this).animate({ opacity: 1 }, 300 );
        
        }, function() {
        
            if( $(this).children("img").is(":not([src*=active])") ) $(this).animate({ opacity: 0.6 }, 300 );
        
        } );


        /* 
        *   Opaque buttons
        */
        $(".opbutton").css("opacity","0.6");
        $(".opbutton").hover(function(){
         $(this).animate({opacity: 1}, "fast");
        },function(){
         $(this).animate({opacity: 0.6}, "fast");
        });

    }


    /*
    *   Controlpanel tabs handler 
    */
    $("div.configbox > div[id]").hide();
    $("div.configbox > div[id]:first").show();
    
  	$("ul.configtabs li").click( function() {
      $("ul.configtabs li").removeClass("active");
      $(this).addClass("active");
      
      var expand_id = $(this).attr("rel");
      
      $("div.configbox > div[id]").hide();
      $("div.configbox > div[id="+expand_id+"]").fadeIn();
    } );


    /*
    *   Up menu handler
    */
    $("#box > .close").click( function() {
    
      $("#box").fadeOut();
      $("#up-menu div.button").removeClass("active");
      $("#checkout-blog").fadeIn();
    
    });
    
    $("[upmenu]").click( function() {
    
      $("#up-menu div.button").removeClass("active");
      $(this).addClass("active");
      
      if( $("#box").is(":hidden") )
      {
          $("#checkout-blog").fadeOut();
          $("#box").fadeIn();
      }
      
      $("#box > div").hide();
      
      var targetObjId = $(this).attr("upmenu");
      var targetObj = $("[id="+targetObjId+"]");
      
      $(targetObj).fadeIn();
  
    });


    /* Licence handling */
    $("div.license div.title").toggle( function() {
      $(this).addClass("expanded");
      $("#license").slideDown();
    }, function() {
      $(this).removeClass("expanded");
      $("#license").slideUp();
    } );


    /* Simple object toggle function */
    $("[toggle]").click( function() {
      
      var targetObjId = $(this).attr("toggle");
      var targetObj = $("[id="+targetObjId+"]");
      
      if( $(targetObj).is(":hidden") ) $(targetObj).slideDown();
      else $(targetObj).slideUp();
      
      return false;
  
    });


    /* Simple object show function */
    $("[show]").click( function() {
      
      var targetObjId = $(this).attr("show");
      var targetObj = $("[id="+targetObjId+"]");
      
      $(targetObj).slideDown();
  
    });

    
    /* Aboutme networks hover animation */
    $("div.aboutme-networks a").css( "opacity", "0.6" );
    $("div.aboutme-networks a").hover( function() {
        $(this).animate( {height: "55px", marginTop: "5px", opacity: 1}, "fast" );
    }, function() {
        $(this).animate( {height: "32px", marginTop: "14px", opacity: 0.6}, "fast" );
    } );
    
    
    /* Guest Book - Comment form handler */

    $("#post-comment > div > .toggler").toggle( function() {
          $(this).addClass("expanded");
          $("#comment-form").slideDown();
    }, function() {
          $(this).removeClass("expanded");
          $("#comment-form").slideUp();
    } );
    

    /* Comments -> user avatar:hover info */
    $("div.leftb p").css("opacity","0");
    $("div.leftb p").hover(function(){
     $(this).animate({opacity: 1}, "fast");
    },function(){
     $(this).animate({opacity: 0}, "fast");
    });


    /* Comments - Reply function */
    $("[replyto]").click( function() {
      
      var commentId = $(this).attr("replyto");
      
      $("#comment-form").slideDown();
      
      insertAtCursor( 'message' , '{' + commentId + '}' );
    
      scrollTo( 'post-comment' );
    
    } );
    
    
    /* Video Downloads counter */
    $('div.video > div.infobar > a.download').click( function(){
      
      var downloadsObj = $('div.video > div.infobar > h3 > span');
      var downloads = $(downloadsObj).html();
      
      downloads = downloads/1 + 1;
      
      $(downloadsObj).html(downloads);
      
    } );

    /*
    *   ITEM FILTERS 
    */
    
    /* Items with downloads  */
    $("div.filters .filter-downloads").click( function() {
      $("#items a[downloads=0]").slideUp();
    } );

    /* Reset  */
    $("div.filters .filter-reset").click( function() {
      $("#items a").fadeIn();
    } );

    

    /*
    *   PLUGIN TRIGGERS
    */

  	/* Resizable textarea */
      $('textarea.resizable:not(.processed)').TextAreaResizer();


});


// =============================================
//            Recent items slideshow
// =============================================

$move_by = 338;
$frame_left = 0;
$frame_no = 1;
$start_middle = 0;

$(document).ready(function()
{

	$(".nav-left").hide();

	$max_clicks = $(".slideitems").children().size();
	$imgCont = $move_by * $max_clicks;
	
	$max_clicks = $max_clicks - 1;
	
	$(".slideitems").css({width : $imgCont});

	$(".nav-right").click(function()
		{
			/* Set the new position & frame number */
			$new_frame_no = (($frame_no/1)+1);
			$new_left = (($frame_left/1) - $move_by);
			
			if($new_frame_no == $max_clicks)
				{$(".nav-right").fadeOut();}
			else if($frame_no == 1)
				{$(".nav-left").fadeIn();}
			
			/* Check if we're moving too far over */
			if($new_frame_no > $max_clicks)
				{
					/* Move all the way right, to the beginning*/
					$new_left = 0;
					$new_frame_no = 1;
				}
			$new_left_attr = $new_left+"px";
			$(".slideitems").animate({left: $new_left_attr}, 800 );
			$frame_left = $new_left;
			$frame_no = $new_frame_no;
		});



	$(".nav-left").click(function()
		{
			/* Set the new position & frame number */
			$new_frame_no = (($frame_no/1)-1);
			$new_left = (($frame_left/1) + $move_by);
			
			if($new_frame_no == 1)
				{$(".nav-left").fadeOut();}
			else if($frame_no == $max_clicks)
				{$(".nav-right").fadeIn();}
				
			
			/* Check if we're moving too far over */
			if($new_frame_no <= 0)
				{
					/* Move the images all the way left, minus one frame */
					$new_left = -($move_by*$max_clicks)+$move_by;
					$new_frame_no = $max_clicks;
				}
			$new_left_attr = $new_left+"px";
			$(".slideitems").animate({left: $new_left_attr}, 800 );
			$frame_left = $new_left;
			$frame_no = $new_frame_no;
			
		});
});



// ================== Custom ==================

  function scrollTo( objId ){
    $('html, body').animate({
    scrollTop: $("#"+objId).offset().top
    }, 500);
  }
  
  function getValue(objId){
    return document.getElementById(objId).value;
  }
  
  function giveValue(what, where){
    document.getElementById(where).value=what;
  }
  
  function giveContent(what, where){
    document.getElementById(where).innerHTML=what;
  }



// =========  Text Insert ========= 

  function insertAtCursor(obj,val) {
  
      var o = document.getElementById(obj);
  
      o.focus();
        
      if (document.selection) {
  
          sel = document.selection.createRange();
  
          sel.text = val;
  
          }
  
      else if (o.selectionStart || o.selectionStart == '0') {
  
          var startPos = o.selectionStart;
  
          var endPos = o.selectionEnd;
  
          o.value = o.value.substring(0,startPos) + val + o.value.substring(endPos, o.value.length);
  
          }
  
      else {
  
          o.value += val;
  
          }
  
      }
  
  
  function insertAroundSelection(obj,startVal,endVal) {
  
      var o = document.getElementById(obj);
  
      o.focus();
        
      if (document.selection) {
  
          sel = document.selection.createRange();
  
          var selText = sel.text;
  
          sel.text = startVal + selText + endVal;
  
          }
  
      else if (o.selectionStart || o.selectionStart == '0') {
  
          var startPos = o.selectionStart;
  
          var endPos = o.selectionEnd;
  
          o.value = o.value.substring(0,startPos) + startVal + o.value.substring(startPos,endPos) + endVal + o.value.substring(endPos,o.value.length);
  
          }
  
      else {
  
          insertAtCursor(obj, startVal + endVal);
  
          }
  
      }

