jQuery(document).ready(function(){
  
    jQuery("a#PrintinPopup").click(function(event) {
     event.preventDefault();
     jQuery('div#shopping-list-print-container').printElement({ printMode: 'popup',  pageTitle: 'Shopping list generated by www.simplybeefandlamb..co.uk' });
  });

  
  	if( jQuery('.home-customise').length > 0 ) {
  		var	options = jQuery('.home-customise');
  		jQuery('.footer').prepend(options);
  		jQuery('.home-customise').wrap('<div class="wrapper customise-wrap" />')
  	}
  	
	if ( jQuery('ul.secondary-menu:visible').length > 0 ) {
	
		jQuery('body').addClass('secondary-menu-on');
	
	}

	if( jQuery('ul.main-menu li.menu-327').hasClass('active-trail') ){
		jQuery('ul.secondary-menu').attr('style','display:block;');
	}
	
	jQuery('footer form input#edit-mail').attr('placeholder', 'Email Address');
  
  
  
    jQuery('.menu-776,.menu-312,.menu-313').hover(function() {
		//alert('TEST');
		jQuery('ul.secondary-menu li.active-trail').css('background','url("/sites/all/themes/beefandlamb/images/nav-left2-1.png")');
		jQuery('ul.secondary-menu li.active-trail a.active-trail').css('background','url("/sites/all/themes/beefandlamb/images/nav-right2-1.png") no-repeat scroll right top transparent');
	},
	function() {
		jQuery('ul.secondary-menu li.active-trail').css('background','url("/sites/all/themes/beefandlamb/images/nav-left2.png")');
		jQuery('ul.secondary-menu li.active-trail a').css('background','url("/sites/all/themes/beefandlamb/images/nav-right2.png") no-repeat scroll right top transparent');
	});
  
  	
  	jQuery('.oldie header.header .search .form-submit, .oldie .search-recipes .views-exposed-form .views-exposed-widget .form-submit, .oldie footer .form-submit, .oldie #recipe-search-summary .form-submit').attr('value', '');
  	jQuery('.ie7 .home-customise input#edit-save').attr('value', 'Save');
    jQuery('#recipe-landing-list .recipe:last-child').addClass('last-one');
  
  
  var myhtml=''; 
  var i=0; 
  // interogate the homebox buttons
  jQuery('div#homebox-add li').each(function() {
	if( !jQuery(this).find('a').hasClass('restore') ){
	  var hclass = ''; 
	  var checked = '';
	  var xtra = ''; 
	  // set the value to the link to add unsaved blocks
	  var value = jQuery(this).find('a').attr('href');
	  if( jQuery(this).find('a').hasClass('used') ){
	    checked = " checked='checked' ";
	  }
	  else {
		 hclass = hclass + 'use '; 	
	  }
	  
	  var title = jQuery(this).find('a').html(); 	

      var id = ''; 
	  // find the corresponding homebox id - based on title
	  // there must be a better way to do this??? 
	  jQuery('div#homebox span.portlet-title').each(function() {
        if( jQuery(this).html()==title ){
		  // build a checkbox id with the prefix 'cb_'
		  id = "cb_" + jQuery(this).parent().parent().parent().attr('id');  
		  return;  
		}
			
		// if block is not there then set the id to the same as the value 	
		if(!id){
		  id = value; 
		}
			
	  })
		
	  // some manipulation for 'first' list item
	  
	  if( i===0 ){
	    hclass = hclass + "first "; 
	  }
		 
	  if( hclass ){
		 xtra = " class='" + hclass + "'"; 
	  }
	  // build the list item html - which includes the check box
	  myhtml = myhtml 
	  + "<li" + xtra + "><input type='checkbox' id='" + id + "' value='" + value + "' "+ checked + " name='testing'/><label for='" + id + "'>" + title + "</label>"
	  + "</li>";
	
  	  i++;
    }

  });
	
  // build the full html list including the restore link...	
  myhtml = "<div class='item-list'>"
  + "<ul class='clear-block'>"
  + myhtml 
  + "<li class='first'>"
  + "<a class='restore' href='/homebox/dashboard/restore'>Restore to defaults</a>"
  + "</li>"
  + "</ul>"
  + "</div>"; 

  // add the list html to the page 
  jQuery('div#homebox-add').html(myhtml); 

  // on checkbox click event
  jQuery('div#homebox-add input:checkbox').click(function() {
    // get the homebox block id by extracting the 'cb_' prefix
	var hbid = jQuery(this).attr('id').slice(3);  
		
	if ( jQuery(this).is(':checked') ) {
	  // if checked then add the block 
	  jQuery(this).parent().removeClass('use'); 
	  if ( jQuery(this).attr('value')==jQuery(this).attr('id') ) {
	    // if block is not renedered in the html then use the value link to load it in...
		// url hard-coded here but need to change this...
	    var host = document.location.host;
		//window.location.href="http://potato.9web.co.uk" + jQuery(this).attr('value');
		window.location.href="http://" + host + jQuery(this).attr('value');
	  }
	  else {
	    // the block is only hidden so 'show' it	  
		jQuery('div#homebox').find('div#' + hbid ).show(); 
		// call homebox functions to handle columns resize and saving options
		Drupal.homebox.equalizeColumnsHeights();
        Drupal.homebox.pageChanged();
   	  }
	}
	else {
	  // hide the homebox block
	  jQuery('div#homebox').find('div#' + hbid ).hide(); 
   	  Drupal.homebox.equalizeColumnsHeights();
      Drupal.homebox.pageChanged();
	  jQuery(this).parent().addClass('use'); 
	}
  });

  // additional functionality for closing block on homebox.. need to change the checkbox status to reflect this
  jQuery('div#homebox a.portlet-close').click(function() {
    // get the block id and add the 'cb_' prefix to reference the checkbox
	var cbval = "cb_" + jQuery(this).parent().parent().parent().attr('id'); 
	jQuery("div#homebox-add input:checkbox#"+ cbval).attr('checked', false); 
	jQuery("div#homebox-add input:checkbox#"+ cbval).parent().addClass('use'); 
  });
  
/*
if( jQuery('form#switchtheme-switch-form select#edit-theme option:selected').val() ){ 
     var themeval = jQuery('form#switchtheme-switch-form select#edit-theme option:selected').val();
  	 if( themeval == 'potato' ){ 
		jQuery('div.lowBandwidth').html("<p id='potatolow'>Site loading slowly? Switch to our <a href='#' title='this link convert this site to low bandwidth'>Low Bandwidth</a> version</p>");
	 }
	 else {	
		jQuery('div.lowBandwidth').html("<p id='potato'>Want to see more? Switch to our <a href='#' title='this link convert this site to low bandwidth'>High Bandwidth</a> version</p>");
	}
  }
*/

  // tweet handling  
   var username='simplybeeflamb'; // set user name
   var format='json'; // set format, you really don't have an option on this one
   
   var url='http://api.twitter.com/1/statuses/user_timeline/'+username+'.'+format+'?callback=?&rpp=2'; // make the url
	jQuery.getJSON(url,function(tweet){ // get the tweets
	  jQuery("div.twitter-feed").html(tweet[0].text); // get the first tweet in the response and place it inside the footer
	});
	
	// get #dadsxmas tweets for fathers christmas section. 
	
	 // Declare variables to hold twitter API url
	  var twitter_api_url = 'http://search.twitter.com/search.json';

		// Send JSON request
		// The returned JSON object will have a property called "results" where we find
		// a list of the tweets matching our request query
		jQuery.getJSON(
    	twitter_api_url + '?q=from:'+username+'+AND+%23dadsxmas&callback=?&rpp=2',
    	function(data) {
  			jQuery.each(data.results, function(i, tweet) {
				// Uncomment line below to show tweet data in Fire Bug console
				// Very helpful to find out what is available in the tweet objects
				//console.log(tweet);
				// Calculate how many hours ago was the tweet posted
				  var date_tweet = new Date(tweet.created_at);
				  var date_now   = new Date();
				  var date_diff  = date_now - date_tweet;
				  var hours      = Math.round(date_diff/(1000*60*60));
				  
				// Build the html string for the tweets
				var tweet_html	= '<p class="tweet_text">' + tweet.text + '<\/p><p class="tweet_hours">' + hours + ' hours ago<\/p>';
				// Turn hashtags and usernames into links
				tweet_html = tweet_html.replace(/(^|[^a-z0-9_])@([a-z0-9_]+)/i, '$1@<a href="http://www.twitter.com/$2">$2</a>');
				tweet_html = tweet_html.replace(/(^|\s)#(\w+)/g, '$1#<a href="http://search.twitter.com/search?q=%23$2">$2</a>');

				// put most recent two in right hand nav on What Youre Saying page
				jQuery('div.fc-twitter-feed').append('<div class="fc-tweet-text">'+ tweet_html +'<\/div>');
				
				//create a new variable to hold tweet for the christmas home page
				var fc_tweet_html	= '<div class="tweet_text">'+ tweet.text +'<\/div><div class="tweet_hours">' + hours + ' hours ago<\/div><div class="tweet-what"><a href="/whats-new/fathers-christmas/what-youre-saying">&nbsp;</a><\/div>';
				
				// Turn hashtags and usernames into links
				fc_tweet_html = fc_tweet_html.replace(/(^|[^a-z0-9_])@([a-z0-9_]+)/i, '$1@<a href="http://www.twitter.com/$2">$2</a>');
				fc_tweet_html = fc_tweet_html.replace(/(^|\s)#(\w+)/g, '$1#<a href="http://search.twitter.com/search?q=%23$2">$2</a>');
				
				// put most recent tweet in Fathers Christmas home page
				jQuery('div.xmas-twitter').html(fc_tweet_html);				
			});
		});
});
