jQuery().ready(function($) {
	var cache = []; 
  var current_menu_item = $('#currentmenu').val();
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
  
  // Setup custom scroll bars
  $('#rightcolumn').jScrollPane();

  // Setup carousel  
  $('#carousel').infiniteCarousel({
  	transitionSpeed : 1000,
  	displayTime : 5000,
  	textholderHeight : .25,
  	displayProgressBar : 1
  });
  
  /* menu system logic and animation - start */
  $('.submenulink').click(function() {
    link = this.id;
    $('#'+link+'div').toggle('slide');
    return false;
  });
  
  $('#menu ul li').click(function(e) {
    id = this.id;
    current_menu_item = parseInt($(this).attr("menu"));
    
    if ($(this).attr('class') == 'inactivemenu') {      
      $('.activemenu').removeClass('activemenu').addClass('inactivemenu');
      $(this).removeClass('inactivemenu').addClass('activemenu');
    }
    if (id == 'more') {
      $('#moresubmenu').show();
    }
    else
      $('#moresubmenu').hide();
  });
  
/*
  $("div#menu a").click(function(e) {
    current_menu_item = parseInt($(this).parent().attr("menu"));
    //var ismore = $('#ismore').val();
    if (current_menu_item > 1) {
      e.preventDefault();
      $.ajax({
         url: $(this).attr("href"),
         data: {},
         beforeSend: function() {
           $("div#maincontent").animate({ marginTop: "20px" }, "fast").animate({ marginTop: "-1000px" }, "fast", function() {
             $("div#content").html("<div id='loading-animation'><img src='/wp-content/themes/immedia/images/ajax-loader.gif' alt='loading...' /></div>");
           });
         },
         success: function (data) {
           $("div#content").css("display", "none").html("<div id='maincontent'>"+$(data).find("#maincontent").html()+"</div>").fadeIn("fast");
         },
         dataType: 'html'
      });
    }
  });
*/
  
  $("div #menu li").mouseenter(function () {
    var id = this.id;
     if ($(this).attr('menu') == 1 || id == 'more')
     {
      $('.activemenu').removeClass('activemenu').addClass('inactivemenu');
      $(this).removeClass('inactivemenu').addClass('activemenu');
     }
  
    if (id == 'more')
    {
      $('#moresubmenu').show();
      var menu_position = $("#more").position().left - 180;
      var menu_position_top = $("#more").position().top;
      $('#moresubmenu').css({ left: menu_position+"px", top: menu_position_top+'px'});
    }
    
  });
  
  $("div #menu li").mouseleave(function (e) {
    var id = this.id;   
  
    if (id == 'more')
    {
      var position = $('#moresubmenu').position();
      var right = $('#moresubmenu').width() + position.left;
      var bottom = $('#moresubmenu').height() + position.top;
     
      if (!(e.pageX <  right && e.pageX >  position.left && e.pageY > position.top && e.pageY < bottom))
      {  
        $('#moresubmenu').hide();
        $(this).removeClass("activemenu").addClass("inactivemenu");
        $("#menu ul li:nth-child("+parseInt(current_menu_item)+")").removeClass('inactivemenu').addClass("activemenu");
      }
    }
    else
    {
      $(this).removeClass("activemenu").addClass("inactivemenu");
      $("#menu ul li:nth-child("+parseInt(current_menu_item)+")").removeClass('inactivemenu').addClass("activemenu");
    }
  
  });
  
  $("#more").mouseenter(function () {
    //var id = this.id;
    //if ($(this).attr('class') == 'inactivemenu') {
    // $('.activemenu').removeClass('activemenu').addClass('inactivemenu');
     //$(this).removeClass('inactivemenu').addClass('activemenu');
    //}
   // $('#moresubmenu').show();
    //var menu_position = $("#more").position().left - 180;
    //var menu_position_top = $("#more").position().top;
    //$('#moresubmenu').css({ left: menu_position+"px", top: menu_position_top+'px'});
  });
  
  $("#more").mouseleave(function(e) {
    //$(this).removeClass("activemenu").addClass("inactivemenu");
    //$("#menu ul li:nth-child("+parseInt(current_menu_item)+")").removeClass('inactivemenu').addClass("activemenu");
  
      //var position = $('#moresubmenu').position();
      //var right = $('#moresubmenu').width() + position.left;
      //var bottom = $('#moresubmenu').height() + position.top;
     
      //if (!(e.pageX <  right && e.pageX >  position.left && e.pageY > position.top && e.pageY < bottom))
      //{  
        //$('#moresubmenu').hide();
      //}
  });  
  
  $('#moresubmenu').mouseenter(function() {
    $('.activemenu').removeClass('activemenu').addClass('inactivemenu'); 
    $("#more").removeClass('inactivemenu').addClass('activemenu');
  });
  
  $('#moresubmenu').mouseleave(function() {
     $('#moresubmenu').hide();
    $('.activemenu').removeClass('activemenu').addClass('inactivemenu');   
    $("#menu ul li:nth-child("+parseInt(current_menu_item)+")").removeClass('inactivemenu').addClass("activemenu");    
  });
  
  /* menu system logic and animation - end */  
 
 
  // Automatically get the user's location and display the appropriate weather
  geofill.find({
   callback: function(o) {
     var yql_query = "select * from weather.forecast where location in (select id from xml where url=\"http://xoap.weather.com/search/search?where="+o.city+","+o.country.replace(/ /, "+")+"\" and itemPath=\"search.loc\") and u = 'c'";
     $.YQL(yql_query, function(data) {
        if (parseInt(data.query.count) > 0) {
          if (parseInt(data.query.count) == 1) {
            var w = data.query.results.channel;
          }
          else {
            var w = data.query.results.channel[0];
          }
          $("span#autofill-weather-location").html(w.location.city.toLowerCase());
          $("span#weather").html(w.item.condition.temp+"&deg;");
        }
        else {
          $("span#autofill-weather-location").html(" ");
        }
     }); 
   }
  });
  
  
  // Share topics via twitter and facebook
  $("span.share").live("mouseenter", function() {
   var share_post_id = $(this).attr('postid');
   //var post_user = $('#post_user_'+share_post_id).html();
   var post_date = $('#post_date_'+share_post_id).html();
   var post_contents = $('#post_contents_'+share_post_id).html();
   $(this).addClass("current");
   //$(this).html("share via: <a href='http://twitter.com/?status=Check out the latest on @mobiYze! http://www.yworld.co.za/showbox?id=1123 #mobiyze' class='sharelink' target='_blank'>twitter</a> or <a href='http://www.facebook.com/sharer.php?u=http://beta.yfm.co.za/showbox?id=1123&src=sp' class='sharelink'>facebook</a>");    
  	var twitterhtml = "<a href='http://twitter.com/?status=Check out the following post on YFM! http://www.yworld.co.za/showbox?id="+share_post_id+"' class='sharelink' target='_blank'>twitter</a>";
	var facebookt = encodeURIComponent("Check out this post on YFM!");
	var facebooku = encodeURIComponent("www.yworld.co.za/showbox?id="+share_post_id);
	var facebookurl = "http://www.facebook.com/sharer.php?u="+facebooku+"&t="+facebookt;
	//alert(facebookurl);
	//var facebookurl = "http://www.facebook.com/sharer.php?u=www.yworld.co.za/showbox?id=7288&t=test";
	var facebookhtml = "<a href='"+facebookurl+"' class='sharelink' target='_blank'>facebook</a>";
	$(this).html("share via: "+twitterhtml+" or "+facebookhtml);    
  }).live("mouseleave", function() {
   $(this).removeClass("current");
   $(this).html("share");
  }); 
  
/**************** profile page  **************/
   $(".prof").live("click", function(e) {    
     e.preventDefault();   
     var divToShow = $(this).attr("bar");
     $("div.prof").css({ display: "none" });
     $("div#prof-"+divToShow).css({ display: "block" }); 
     
          
     });
     $(".fourty").live("click", function(e) {    
     e.preventDefault(); 
      
     var d = $(this).attr("bars");
     $("div.fourty").css({ display: "none" });
     $("div#fourty-"+d).css({ display: "block" });
      });
      
       $('.inactive').live('click',function(){
       	 var current = this.id;
     
     	 $('.tabs .active').removeClass('active').addClass('inactive');
     	 $('#'+current).removeClass('inactive').addClass('active');
     	 /* alert(current); */
   });
   
   $('.no').live('click',function(){
       	 var state = this.id;
     
     	 $('.tabs .yes').removeClass('yes').addClass('no');
     	 $('#'+state).removeClass('no').addClass('yes');
     	/*  alert(state); */
   });

  

  $(".ten").live("click", function(e) {    
     e.preventDefault(); 
      
     var d = $(this).attr("tens");
     $("div.ten").css({ display: "none" });
     $("div#ten-"+d).css({ display: "block" });
    /*
 $("td.ten").css({ background:"#1f1f1f"});      
     $(this).css({ background:"#be060d"}); 
*/
});   
   
   //blog 
   $("a.blogNavigation").live("click", function(e) {    
     e.preventDefault();   
     $("a.blogNavigation").css({ "color": "red" });     
     $(this).css({ "color": "blue" });
     var currentBlogPost = $(this).attr("toDisplay");     
     $('div.post').css({ display: "none" });
     $('div#post-'+currentBlogPost).css({ display: "block" });  
   });   
/*********************************************/  

/**************** news page  **************/
   $("span.btnsport").live("click", function() {    
     $(this).css({ background: "url('/wp-content/themes/immedia/images/btn-sport-over.png') no-repeat"});
     $(this).siblings().css({ background: "url('/wp-content/themes/immedia/images/btn-sport.png') no-repeat"});
     $("span.btnsport-long").css({ background: "url('/wp-content/themes/immedia/images/btn-sport-long.png') no-repeat"});

     var divToShow = $(this).attr("bar");
     $("div.news").css({ display: "none" });
     $("div#news-"+divToShow).css({ display: "block" });       
   });
   
  $("span.btnsport-long").live("click", function() {    
     $(this).css({ background: "url('/wp-content/themes/immedia/images/btn-sport-over-long.png') no-repeat"});
     $(this).siblings().css({ background: "url('/wp-content/themes/immedia/images/btn-sport-over-long.png') no-repeat"});
     $("span.btnsport").css({ background: "url('/wp-content/themes/immedia/images/btn-sport.png') no-repeat"});
      
     var divToShow = $(this).attr("bar");
     $("div.news").css({ display: "none" });
     $("div#news-"+divToShow).css({ display: "block" });       
   });  
   
        
/*
  $("input[name=vote]").live("click", function() {    
    alert("hello");
  });  
*/        
 
/*********************************************/  

/**************** profile page  **************/
  // Setup custom scroll bars for this page
  /* $('#prof-1').jScrollPane(); */  // come back to the this
/*********************************************/    

});

function change_dj(dj, show) {
  // Change the current now playing DJ
	jQuery.preLoadImages('/wp-content/themes/immedia/images/djpics/headerpics/'+dj);
	jQuery('#jockpic').fadeIn(2000).html('<img src="/wp-content/themes/immedia/images/djpics/headerpics/'+dj+'">');
	jQuery('.onairnowtitle').fadeOut(50).fadeIn(2000).html('on air now');
	
	jQuery('.onairnow').fadeOut(50).fadeIn(2000).html(show);
}






