/****************************************************************
developed by: Bryce Sluman - bsluman@corp.vendio.com
description: function to manage the dynamic loading and paging of 
application snippits based on filters and pagination


****************************************************************/
//get the filter param from the url else use the default
  if(query_arr[0]){
    var default_filter="#"+query_arr[0];
  }else{
    var default_filter = '#all_apps';
  }
  
var app_count = 5;//1 based

var charm_cross = new Array()
charm_cross['#all_apps']=['#image_hosting','#research','#counters','#counters_pro','#gallery','#gallery_pro','#reviser','#zoomstream','#watermarking','#buyer_appreciation'];
charm_cross['#most_popular']=['#image_hosting','#research','#gallery','#buyer_appreciation'];
charm_cross['#free']=['#counters','#gallery','#buyer_appreciation'];
charm_cross['#ebay_only']=['#research','#counters_pro','#gallery','#gallery_pro','#reviser','#buyer_appreciation'];
charm_cross['#sa_apps']=['#image_hosting','#research','#counters','#gallery','#gallery_pro','#reviser','#buyer_appreciation'];
charm_cross['#imaging']=['#image_hosting','#gallery','#gallery_pro','#zoomstream','#watermarking'];
charm_cross['#marketplace_mgt']=['#research','#reviser'];
charm_cross['#marketing']=['#image_hosting','#counters','#counters_pro','#gallery','#gallery_pro','#buyer_appreciation'];
charm_cross['#cust_comm']=['#buyer_appreciation'];
charm_cross['#business_mgt']=['#research','#reviser','#buyer_appreciation'];
charm_cross['#analytics']=['#research','#counters','#counters_pro'];

$().ready(function() {
  $('#applications_list').attr('style','display: none');
  setList(default_filter,0,app_count,1,'wrap');
  setClickEvents(0,app_count,1);

});        
/*assign click events to filter links via DOM traversal
parameters: start
*/
function setClickEvents(start,stop,page){
    $('ul.controls li a').each(function(id){
    var curr_id = '#'+this.id
        $(this).click(function(id){
	        $('ul.controls li a').removeClass("selected");
	        $(this).addClass("selected");
	        pageTracker._trackPageview(this.href);
	        //clear list
	        $("#applications").html('');
	        $("#applications").css('display','none');
	            var arr = charm_cross[curr_id];
	            var arr_count = charm_cross[curr_id].length;
	            for(i=start;i<stop;i++){
	                $("#applications").append($(arr[i]).html());
	            }
	            setPagination(curr_id,arr_count,start,stop,page);
                setCounts(arr_count,start,stop);
             
	         $("#applications").fadeIn("slow");
	         $.scrollTo( '#app_content_top', 500, { easing:'swing', queue:true, axis:'xy' } );
	         return false;
        });
    });
}

function setList(filter,start,stop,page,scroll){
    var arr = charm_cross[filter];
    var arr_count = arr.length;
    //remove selected class from all filter buttons
    $('.controls li a').removeClass("selected");
    //add "selected" class to selected filter
    $(filter).addClass("selected");
    //clear list
    $("#applications").html('');
    for(i=start;i<stop;i++){
        $("#applications").append($(arr[i]).html());
    }
    $.scrollTo( '#'+scroll, 500, { easing:'swing', queue:true, axis:'xy' } );
    //build footer
    setPagination(filter,arr_count,start,stop,page);
    setCounts(arr_count,start,stop);
}
function setPagination(filter,count,start,stop,page){
    if(app_count<count){
    $(".text_right").html('');
    //create pagination
    var lower = 0;
    var upper = app_count;
    //get total number of pages
        var num_pages = Math.ceil(count/app_count);
	        for(i=1;i<=num_pages;i++){
	        //set selected class for pagination
	        if(i==page){
	            var selected="current_page";
	        }else{
	            var selected="";
	        }
	        //numbered list of pages
	        if(i==num_pages){
	           $('.text_right').append('<a href="#" class="'+selected+'" onclick="setList(\''+filter+'\','+lower+','+upper+','+i+',\'app_content_top\');return false;">' + i + '</a>');
	        }else{
	           $('.text_right').append('<a href="#" class="'+selected+'" onclick="setList(\''+filter+'\','+lower+','+upper+','+i+',\'app_content_top\');return false;">' + i + '</a> | ');
	        }
	        lower = upper;
	        upper = upper+app_count;
	        }
	        //previous link
	        if(start!=0){
	            $('.text_right').prepend('<a href="#" onclick="setList(\''+filter+'\','+(start-app_count)+','+(start)+','+(page-1)+',\'app_content_top\'); return false;">previous</a> | ');
	        }
	        //next link
	        if(page<num_pages){
	            $('.text_right').append(' | <a href="#" onclick="setList(\''+filter+'\','+(stop)+','+(stop+app_count)+','+(page+1)+',\'app_content_top\'); return false;">next</a>');
	        }
    }else{
        $('.text_right').html('');
    }
}

function setCounts(count,start,stop){
    start++;
    //stop++;
    if(stop<=count){
        $('.text_left').html('Showing ' + start + ' - ' + stop + ' of <span class="highlight">' + count + '</span> results');
    }else{
        $('.text_left').html('Showing ' + start + ' - ' + count + ' of <span class="highlight">' + count + '</span> result(s)');
    }
}
