///////////////////////////////////////////////////////////////////////////
//                            Image Carousel                             //
///////////////////////////////////////////////////////////////////////////

function mycarousel_initCallback(carousel) {
    $('#pageNav .next').bind('click', function() {
        carousel.next();
        return false;
    });
    $('#pageNav .prev').bind('click', function() {
        carousel.prev();
        return false;
    });
    $('#pageNav a').bind('click', function() {
	 $(this).blur();
        carousel.scroll($.jcarousel.intval($(this).attr('id').slice(3)));
        carousel.startAuto(0);
        carousel.stopAuto();
        return false;
    });
};


function mycarousel_VisibleInCallbackBeforeAnimation(carousel,item,idx,state) {
	$("#pageNav a").removeClass('selected');
	$("#pageNav a#ss_"+idx+"").addClass('selected');
};

function mycarousel_VisibleInCallbackAfterAnimation(carousel,item,idx,state) { };


///////////////////////////////////////////////////////////////////////////
//                             Pagintation                               //
///////////////////////////////////////////////////////////////////////////

function previous(){
	new_page = parseInt($('#current_page').val()) - 1;
	//if there is an item before the current active link run the function
	if($('.selected').prev('.page_link').length==true){
		go_to_page(new_page);
	}
}

function next(){
	new_page = parseInt($('#current_page').val()) + 1;
	//if there is an item after the current active link run the function
	if($('.selected').next('.page_link').length==true){
		go_to_page(new_page);
	}
}

function go_to_page(page_num){
	//get the number of items shown per page
	var show_per_page = parseInt($('#show_per_page').val());

	//get the element number where to start the slice from
	start_from = page_num * show_per_page;

	//get the element number where to end the slice
	end_on = start_from + show_per_page;

	//hide all children elements of content div, get specific items and show them
  //$('#pagintationContent').children().css('display', 'none').slice(start_from, end_on).css('display', 'block');
	$('#pagintationContent').children().hide().slice(start_from, end_on).fadeIn("slow");

	/*get the page link that has longdesc attribute of the current page and add active_page class to it
	and remove that class from previously active page link*/
	$('.page_link[longdesc=' + page_num +']').addClass('selected').siblings('.selected').removeClass('selected');

	//update the current page input field
	$('#current_page').val(page_num);
}



///////////////////////////////////////////////////////////////////////////
//                            JQuery Stuff                               //
///////////////////////////////////////////////////////////////////////////

$(document).ready(function() {
  
  /*
   * Sign In Box
   */
   $("a#btnSignin").colorbox({inline:true, href:"#signin", opacity:0.7});
   $("input[type=text]").focus(function() { $(this).addClass("focus"); });
   $("input[type=text]").blur(function() { $(this).removeClass("focus"); });  
   $("a[rel*=gallery]").colorbox({preloading:true, maxWidth:"640px", scalePhotos:true});
   $('.galleryItems p a').bind('click', function() {
   var rel = $(this).attr("href");
   rel = rel.substring(1, rel.length);
       $('a[rel=' + rel + ']').eq(0).trigger('click');
       return false;
   });
  
  /*
   * Logo Hover
   */
  $("#header a #logo").hover(function() {
    // are we on the homepage?
    hasSelected = $("#headerContent ul li:first-child a").hasClass("selected");
    $("#headerContent ul li:first-child a").addClass("selected");
  }, function() {
    // Only remove the class if we are not on the homepage
    if(!hasSelected)
      $("#headerContent ul li:first-child a").removeClass("selected");
  });
  

  /*
   * Homepage Tabs
   * Not the best solution for tabs but we only have a simple case here
   */
  $("#tabsNav a#news").click(function() {
    $("#tabsBio").hide();
    $("#tabsNews").fadeIn("slow");
    $(this).addClass("selected");
    $("#tabsNav a#bio").removeClass("selected");
    return false;
  });
  
  $("#tabsNav a#bio").click(function() {
    $("#tabsNews").hide();
    $("#tabsBio").fadeIn("slow");
    $(this).addClass("selected");
    $("#tabsNav a#news").removeClass("selected");
    return false;
  });
  
  
   /*
   * Press Tabs
   * Not the best solution for tabs but we only have a simple case here
   */
  $("#tabsNav a#press").click(function() {
    $("#appearancesContent, #appearancesLinks").hide();
    $("#pressContent, #pressLinks").fadeIn("slow");
    $(this).addClass("selected");
    $("#tabsNav a#appearances").removeClass("selected");
    return false;
  });
  
  $("#tabsNav a#appearances").click(function() {
    $("#pressContent, #pressLinks").hide();
    $("#appearancesContent, #appearancesLinks").fadeIn("slow");
    $(this).addClass("selected");
    $("#tabsNav a#press").removeClass("selected");
    return false;
  });

  
  /*
   * Press Release Tabs
   */
   $("#pressLinks a").click(function() {
    $("#pressLinks a").removeClass("selected");
    $(this).addClass("selected");
    $("div [id*=press-]").hide();
    $($(this).attr("href")).fadeIn();
    $('html, body').animate({scrollTop:0}, 'slow');
    return false;
   });
   
   /*
   * Apperances Tabs
   */
   $("#appearancesLinks a").click(function() {
    $("#appearancesLinks a").removeClass("selected");
    $(this).addClass("selected");
    $("div [id*=appearances-]").hide();
    $($(this).attr("href")).fadeIn();
    $('html, body').animate({scrollTop:0}, 'slow');
    return false;
   });
  
   /*
   * Videos
   */
   $("#videoLinks a").click(function() {
    $("#videoLinks a").removeClass("selected");
    $(this).addClass("selected");
    $("div [id*=video-]").hide();
    $($(this).attr("href")).fadeIn();
    return false;
   });
  
  
  /*
   * Remove any default values on click
   */
   $("#listName").click(function() { if($(this).val() == "Name") { $(this).val(""); } });
   $("#listEmail").click(function() { if($(this).val() == "Email") { $(this).val(""); } });
   $("#lgnEmail").click(function() { if($(this).val() == "Email") { $(this).val(""); } });
   $("#lgnPassword").click(function() { if($(this).val() == "Password") { $(this).val(""); } });
   $("#fgtEmail").click(function() { if($(this).val() == "Email") { $(this).val(""); } });
   $("#txtName").click(function() { if($(this).val() == "Name") { $(this).val(""); } });
   $("#txtEmail").click(function() { if($(this).val() == "Email") { $(this).val(""); } });
   $("#txtCode").click(function() { if($(this).val() == "Enter Code") { $(this).val(""); } });
   
   /*
    * Carousel Call
    */
    $("#slideShowContainer").jcarousel({
      scroll: 1,
      auto: 4,
      wrap: 'both',
      easing: 'easeOutQuad',
      animation: 1000,
      buttonNextHTML: null,
      buttonPrevHTML: null,
      initCallback: mycarousel_initCallback,
      itemVisibleInCallback: {
        onBeforeAnimation: mycarousel_VisibleInCallbackBeforeAnimation,
        onAfterAnimation: mycarousel_VisibleInCallbackAfterAnimation
      }
    });
    
    $("#bannerShowContainer").jcarousel({
      scroll: 1,
      auto: 2,
      wrap: 'both',
      easing: 'easeOutQuad',
      animation: 1000,
      buttonNextHTML: null,
      buttonPrevHTML: null
    });
    
    
    /*
     * Validation Rules 
     */
     $("#listForm").validate();
     $("#commentForm").validate();
     $("#contactForm").validate();
     $("#downloadForm").validate();
     
     
    /*
     * Pagintation
     */
     
      //how much items per page to show
    	var show_per_page = 4;
    	//getting the amount of elements inside content div
    	var number_of_items = $('#pagintationContent').children().size();
    	//calculate the number of pages we are going to have
    	var number_of_pages = Math.ceil(number_of_items/show_per_page);
    
    	//set the value of our hidden input fields
    	$('#current_page').val(0);
    	$('#show_per_page').val(show_per_page);

    	var navigation_html = '<p class="left"><a class="previous_link" href="javascript:previous();">Prev</a></p>';
    	var current_link = 0;
    	navigation_html += '<p class="middle">';
    	while(number_of_pages > current_link){
    		navigation_html += '<a class="page_link" href="javascript:go_to_page(' + current_link +')" longdesc="' + current_link +'">'+ (current_link + 1) +'</a>';
    		current_link++;
    	}
    	navigation_html += '</p>';
    	navigation_html += '<p class="right"><a class="next_link" href="javascript:next();">Next</a></p>';
    
    	$('.pageNav').html(navigation_html);

    	//add active_page class to the first page link
    	$('.pageNav .page_link:first').addClass('selected');
    
    	//hide all the elements inside content div
    	$('#pagintationContent').children().hide();
    
    	//and show the first n (show_per_page) elements
    	$('#pagintationContent').children().slice(0, show_per_page).show();
    	
    	$(".pageNav a").click(function() {
        $('html, body').animate({scrollTop:0}, 'slow');
    	});
     

});