//custom javascript
 $(document).ready(function() {
     //get url file
     var currentfile = jQuery.url.attr("file");
     var currentdir = jQuery.url.attr("directory");

     //highlight selected link
     if(currentfile != "login.php" && currentdir=="/"){
         $(".mainnavigation li a").each(function (i) {
            if ($(this).attr('href') == "../"+currentfile) {
                $(this).parents("li").addClass('selectednav');          
            }
            if (!currentfile) {
                $(".mainnavigation li:first").addClass('selectednav');
            } 
          });
      }
      //zebra table
      $(".zebratable tbody tr:nth-child(odd)").addClass("odd");
      
      //category nav
      $("#latestcategorynav a").unbind("click").click(function(){
         $("#latestposts").html('<span class="loader">LOADING...</span>');
         var href = $(this).attr("href");
         var splitcat = href.split("?");
         var cat =splitcat[1];// href.replace(/..\/index.php\?/, "");
         $("#latestposts").load('../ajax.php?action=loadlatestpost&'+cat);
         $("#latestcategorynav li").removeClass('selectednav');
         $(this).parents("li").addClass('selectednav');
         return false;
                                                         
      });
      
      //alphabetnav
      $("#alphabeth a").unbind("click").click(function(){
          var letter = $(this).text().toLowerCase();
          $("#alphabeth li").removeClass('selectednav');
          $(this).parents("li").addClass('selectednav');
          if(letter == "view all"){
            $("#members .member").show();
          }else{
          $("#members .member").each(function (i) {
            if ($(this).attr('title') == letter) {
                $(this).show();
            }else{
                $(this).hide();
            } 
          });
          }
          return false;
          
      });
     if($("#promoslides").length > 0){ 
		$('#promoslides').cycle({ 
			fx:     'fade', 
			delay:  -2000,
			timeout: 8000
		});
	 }
    
    if($(".sidecal").length > 0){
        $(".maincontentblock .sidecal").each(function(){
            var id = $(this).attr("id");
            var filename = id.split("_-");
            highlightdates(filename[1],filename[2]);
        });
    }
    
    if($(".cal_times").length > 0){
        $(".maincontentblock .cal_times").each(function(){
            var id = $(this).attr("id");
            var filename = id.split("_-");
            showcaltimes(filename[1]);
        });
    }
	
	shuffle( $('.artist-members div') );
	
	//show only top 10 members
	$(".artist-members .member").each(function(i){
		if(i < 12){
			$(this).show();	
		}else{
			$(this).hide();	
		}										   
											   
	});
	//show all members
	$(".show-all-members").click(function(){
	   	
		if($(this).text()=="Show All"){
			$(this).text("Hide All");
		}else{
			$(this).text("Show All");
		}
		$(".artist-members .member").each(function(i){
			if(i > 12){
				$(this).toggle();	
			}										   
												   
		});	
		
		return false;									  
	});

 });
 
 function shuffle(e) {               // pass the divs to the function
    var replace = $('<div>');
    var size = e.size();

    while (size >= 1) {
       var rand = Math.floor(Math.random() * size);
       var temp = e.get(rand);      // grab a random div from our set
       replace.append(temp);        // add the selected div to our new set
       e = e.not(temp); // remove our selected div from the main set
       size--;
    }
    $('.artist-members').html(replace.html() );     // update our container div with the
                                             // new, randomized divs
}

 
  //highlight dates  
  function highlightdates(filename, article){   
    $.ajax({
          url: "../ajax.php",
          data: "action=getevents&filename="+filename+"&article="+article,
          dataType: "json",
          success: function(natDays){
                     var monthLocale = new Array(12);
                                        monthLocale[1] = "January";
                                        monthLocale[2] = "February";
                                        monthLocale[3] = "March";
                                        monthLocale[4] = "April";
                                        monthLocale[5] = "May";
                                        monthLocale[6] = "June";
                                        monthLocale[7] = "July";
                                        monthLocale[8] = "August";
                                        monthLocale[9] = "September";
                                        monthLocale[10] = "October";
                                        monthLocale[11] = "November";
                                        monthLocale[12] = "December";
                     var defaultdate = monthLocale[natDays[0][0]]+" "+ natDays[0][1] +","+ natDays[0][2];
                     $("#cal_-"+filename+"_-"+article).datepicker({
                        numberOfMonths: [1, 1],
                        showCurrentAtPos: 0,
                        beforeShowDay: function (date){
                            for (i = 0; i < natDays.length; i++) {
                                  if (date.getMonth() == natDays[i][0] - 1 && date.getDate() == natDays[i][1]  && date.getFullYear() == natDays[i][2]) {
                                     
                                    return [true,"ui-state-active tooltip"];
                                  }
                                }
                                
                              
                              
                              return [false, ""];
                              
                        },
                        defaultDate: new Date (defaultdate),
                        onSelect: function(dateText, inst) { 
                        //alert(dateText);
                        }
                        
                    });
                   }
                   
         });
              
  }
  
  //highlight dates  
  function showcaltimes(article){   
    $.ajax({
          url: "../ajax.php",
          data: "action=geteventtimes&article="+article,
          success: function(caltimes){
                     $("#cal_-"+article).html(caltimes);
                   }
                   
         });
              
  }
    
 //end custom javascript