$(document).ready(function() {
	//adjust height
	if($("#content_loader").height() < $("#sidebar").height()){
		$("#content_loader").height($("#sidebar").height());
	}else{
		$("#content_loader").height('auto');	
	}
	
	/* = HIGHLIGHT CURRENT PAGE
	----------------------------------------------------*/
	var currentfile = jQuery.url.attr("file");
    var currentaction = jQuery.url.param("a");
    var currentdir = jQuery.url.attr("directory");
     
	 $("#navigation li a").each(function (i) {
		if ($(this).attr('href') == "http://"+window.location.hostname+currentdir) {
			$(this).parents("li").addClass('selectednav');          
		}else if($(this).attr('href') == "http://"+window.location.hostname+"/"+currentfile) {
			 $(this).parents("li").addClass('selectednav');
		}
	 });
	 /* = END HIGHLIGHT CURRENT PAGE
	----------------------------------------------------*/
	
	/* = jQuery UI BUTTONS
	----------------------------------------------------*/
	$(".button").button();	
	
	
	/* = PROMOS
	----------------------------------------------------*/
	if($('#promoslides').length > 0){
	   $('.articlenav').html('<div class="nav"></div>');
	   $('#promoslides').cycle({ 
		fx:     'fade', 
		pager: '.nav',
		delay:  -2000,
		timeout: 8000
		});
	}
	/* = END PROMOS
	----------------------------------------------------*/
	
	
	/* = SEARCH
	----------------------------------------------------*/
	//clear search box pn focus
	$("#q").focus(function(){
		if($(this).val()=="SEARCH"){
			$(this).val("");	
		}	
	}).blur(function(){
		if($(this).val()==""){
			$(this).val("SEARCH");	
		}	
	});
	
	/* = END SEARCH
	----------------------------------------------------*/
	
	
	
	/* = TEXT SIZE
	----------------------------------------------------*/
	  // Increase Font Size
	  $(".textup").click(function(){
		var currentFontSize = $('.post-text').css('font-size');		
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		var newFontSize = currentFontSizeNum*1.2;
		
		$('.post-text').css('font-size', newFontSize);
		return false;
	  });
	  
	  // Decrease Font Size
	  $(".textdown").click(function(){
		var currentFontSize = $('.post-text').css('font-size');
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		var newFontSize = currentFontSizeNum*0.8;
	
		$('.post-text').css('font-size', newFontSize);
		return false;
	  });
	  
	/* = END TEXT SIZE
	----------------------------------------------------*/
	
	
	/* = LOAD CONTACT FORM
	----------------------------------------------------*/
     if($("#contact_us_form").length > 0){
		$(".contact-form-other").hide();
        $.ajax({
          url: "http://"+window.location.hostname+"/ajax.php",
          data: "action=getcontactform",
          success: function(frm){
              $("#contact_us_form").html(frm);
          }
               
          });    
     }
	 /* = END LOAD CONTACT FORM
	----------------------------------------------------*/
	
	/* = SUBMIT CONTACT FORM
	----------------------------------------------------*/
     $('#contact_us_form').ajaxForm({target: "#contact_us_form"});
	 $('#contactform').ajaxForm({target: "#contactform"});
	 $('#email_form').ajaxForm({target: "#email_form"});
	
	/* = END SUBMIT CONTACT FORM
	----------------------------------------------------*/
	
	
	
	 
	


	/* = EMAIL THIS PAGE
	----------------------------------------------------*/
    $(".opendialog").click(function(){
        var url = $(this).attr("href");
		$("body").append('<div id="modal_dialog"></div>');
        $.ajax({
          url: "http://"+window.location.hostname+"/ajax.php",
          data: "action=getemailform&url="+url,
          success: function(frm){
              $("#modal_dialog").html(frm);
			  
          }
               
          });                           
        $("#modal_dialog").dialog({
            title: "Email this page",
            width: 540,
            height:450,
            modal: true,
            close: function(){
                $("#modal_dialog").dialog('destroy');
				$("#modal_dialog").remove();
            }
        });
        return false;
    });
	
	/*=	PAGINATE
	-----------------------------------------------------*/
	/*//how much items per page to show
    var show_per_page = 10;
    //getting the amount of elements inside content div
    var number_of_items = $('#paginate-this').not(".cat-issues").children().size();
	
	var item_counter = 0;
	//calculate height
	$('#paginate-this').not(".cat-issues").children().each(function(){
		if($(this).height() < 21){
			show_per_page = show_per_page+1;
		}
	});
	
	if(show_per_page > 20){
		show_per_page = 20;
	}
    //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);

    //now when we got all we need for the navigation let's make it '

	//    
	//    what are we going to have in the navigation?
	//        - link to previous page
	//        - links to specific pages
	//        - link to next page
	//    
	var navigation_html = '';
	if(number_of_items > show_per_page){
		
		
    
    var current_link = 0;
		while(number_of_pages > current_link){
			navigation_html += '<a class="page_link" href="javascript:go_to_page(' + current_link +')" longdesc="' + current_link +'" target="_top" style="display:none;">'+ (current_link + 1) +'</a>';
			current_link++;
		}
	
	navigation_html += '<a class="previous_link" href="javascript:previous(' + number_of_pages +');" target="_top" style="display:none;">&laquo; Previous Page</a>';
	navigation_html += '<a class="next_link" href="javascript:next(' + number_of_pages +');" target="_top">Next Page &raquo;</a>';
	navigation_html += '<div class="clear"></div>';
	
	
	}//end if
	
	if($('#current_page').val() == 0){
		$(".previous_link").hide();
	}
	

    $('#pagination').not(".cat-issues").html(navigation_html);

    //add active_page class to the first page link
    //$('#pagination .page_link:first').addClass('active_page');

    //hide all the elements inside content div
    $('#paginate-this').not(".cat-issues").children().css('display', 'none');

    //and show the first n (show_per_page) elements
    $('#paginate-this').children().slice(0, show_per_page).css('display', 'block');*/

	
	/*=CYCLE DATES
	-----------------------------------------------------*/
	
	if($(".eventdate").length > 0){
	   $(".eventdate").cycle({ 
				fx:     'fade', 
				speed:  'fast', 
				timeout: 0, 
				next:   '#next2', 
				prev:   '#prev2'
	   });
	}
	
	/*=END CYCLE DATES
	-----------------------------------------------------*/
	
	/* = SIGNUP FORM FOCUS
	-----------------------------------------------------*/
	$("#signup input, #signup_two input").not("#signup_submit").focus(function(){
		
		$(this).val("");
	
		}).blur(function(){
		if($(this).val() == $(this).prev("label").text() || $(this).val() == ""){
			$(this).val($(this).prev("label").text());
		}
	//$(this).prev("label").val();
	});
	
	/* = GALLERIA
	---------------------------*/
	//show all flicker
	if($("#all-images").length > 0){
		var flickrgroup = $("#all-images").attr("title");
		$('#all-images').css({"height":"600px","margin-bottom":"20px"});
		
		// Load the classic theme
		Galleria.loadTheme('http://'+window.location.hostname+'/js/galleria/themes/classic/galleria.classic.js');
		
		// Initialize Galleria
		$('#all-images').galleria({
			debug: true,
			flickr: flickrgroup,//group:1708246@N20
			flickrOptions: {
			   
				sort: 'date-posted-desc'
			}
		});
	
	}
});

/* = PAGINATION
	----------------------------------------------------*/
function previous(number_of_items){

    new_page = parseInt($('#current_page').val()) - 1;
    //if there is an item before the current active link run the function
    //if($('.active_page').prev('.page_link').length==true){
        go_to_page(new_page);
    //}
	if($('#current_page').val() > 0){
		$(".previous_link").show();
	}else{
		$(".previous_link").hide();
	}
	
	if($('#current_page').val() == (parseInt(number_of_items)-1)){
		$(".next_link").hide();
	}else{
		$(".next_link").show();
	}

}

function next(number_of_items){
    new_page = parseInt($('#current_page').val()) + 1;
    //if there is an item after the current active link run the function
    //if($('.active_page').next('.page_link').length==true){
        go_to_page(new_page);
    //}
	if($('#current_page').val() == (parseInt(number_of_items)-1)){
		$(".next_link").hide();
	}else{
		$(".next_link").show();
	}
	
	if($('#current_page').val() > 0){
		$(".previous_link").show();
	}else{
		$(".previous_link").hide();
	}

}
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
    $('#paginate-this').children().css('display', 'none').slice(start_from, end_on).css('display', 'block');

    /*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('active_page').siblings('.active_page').removeClass('active_page');

    //update the current page input field
    $('#current_page').val(page_num);
    //scroll to top
    $('html, body').animate({scrollTop:0}, 1);
    var page = parseInt(page_num) + 1;
    //update heading
    $('.pagination-title').remove();
	$("#paginated-content").prepend('<h4 class="pagination-title">Page: '+page+'</h4>');
    
}


/* = SUBMIT CONTACT FORM
	----------------------------------------------------*/
function submitcontactform(id){
	 $('#'+id).ajaxForm({target: "#"+id});
	/* $('#contactform').ajaxForm({target: "#contactform"});
	 $('#email_form').ajaxForm({target: "#email_form"});*/
}


/*= ZEBRA TABLE
-------------------------------------------------------*/
function zebratable(){
        $("tbody:nth-child(odd)").addClass("odd");
        jQuery.extend(
        jQuery.expr[':'].contains = function(a,i,m){
        return (a.textContent || a.innerText || "").toLowerCase().indexOf(m[3].toLowerCase())>=0;
        });
        $(".search_calendar_events").keyup(function(){
                var elem = $(this).parent("div").next(".eventstable");
                //$(this).parent("div").next(".eventstable").children("tbody").hide();
                $(elem).children("tbody").hide();
                $(elem).children("tbody").removeClass("odd");
                
                var findarticle = $(this).val();
                if(findarticle != ""){
                    $(elem).children("tbody:contains("+findarticle+")").show();
                }else{
                    $(elem).children("tbody").show();
                }
        }).blur(function(){
            if($(this).val() == ""){
                $(elem).children("tbody").show();   
            }   
            
        });
    }
	
	

