$(function(){
	slider(0);
	var k = $('div[class^=cases_slider_item]').length;
	$("#cases-scroller-content").css("width", k*290);
	$('a.lightbox').lightBox({fixedNavigation:true});
});


var t = 5000;
var timer = null;
function slideCase(nr){
	$("#cases-scroller-content").animate({ "marginLeft": '-'+(nr*290)+'px' }, 300);
}

function cases(nr){
	$.ajax({
		url: "/ajax.php?action=cases&nav="+nr, cache: true, success: function(html){
			$("#main_content").html(html);
		}
	});	
}

function loadTwitter(user, hash, object, txt_hours, txt_notweets){

	// Declare variables to hold twitter API url and user name
    var twitter_api_url = 'http://search.twitter.com/search.json';
    var twitter_user = user;
	var twitter_hash = hash;
	var count = 3;

	var full_url = twitter_api_url + '?callback=?&rpp='+count+'&q=from:'+twitter_user;
	if (twitter_hash!=''){
		full_url += '+'+twitter_hash;
	}

    // Enable caching
    $.ajaxSetup({ cache: true });

    // 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
    $.getJSON(
        full_url,
        function(data) {
			var found = false;
			$(object).html('');
			if (data.results){
				$.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);

					// Before we continue we check that we got data
					if(tweet.text !== undefined) {
						// 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 current tweet
						var tweet_html = '<div id="tweet">';
							tweet_html += '<div id="tweet_left"></div>';
							tweet_html += '<div id="tweet_top"></div>';
							tweet_html += '<div id="tweet_back">';
								tweet_html += '<div class="tweet_text">';
								tweet_html += tweet.text + '<\/div>';
								tweet_html += '<div class="tweet_hours">';
								tweet_html += '<a href="http://www.twitter.com/';
								tweet_html += twitter_user + '/status/' + tweet.id + '">';
								tweet_html += hours;
								tweet_html += ' '+txt_hours+'';
								tweet_html += '<\/a>';
								tweet_html += '<\/div>';
							tweet_html += '</div>';
							tweet_html += '<div id="tweet_bottom"></div>';
						tweet_html += '</div>';

						// Append html string to tweet_container div
						$(object).append(tweet_html);

						$(object).linkify();
						found = true;
					}
				});
			}
			if (!found) {
				$(object).html('<div id="tweet"><div class="tweet_text">'+txt_notweets+'</div></div>');
			}
        }
    );
}

/// JQuery make links (target=_blank)
// Call andy jquery object $('#....').linkify();
(function($){ 
   var url1 = /(^|&lt;|\s)(www\..+?\..+?)(\s|&gt;|$)/g, 
      url2 = /(^|&lt;|\s)(((https?|ftp):\/\/|mailto:).+?)(\s|&gt;|$)/g, 
 
      linkifyThis = function () { 
        var childNodes = this.childNodes, 
            i = childNodes.length; 
        while(i--) 
        { 
          var n = childNodes[i]; 
          if (n.nodeType == 3) { 
            var html = (n.nodeValue); 
            if (html) 
            { 
              html = html.replace(/&/g, '&amp;') 
                         .replace(/</g, '&lt;') 
                         .replace(/>/g, '&gt;') 
                         .replace(url1, '$1<a href="http://$2" target="_blank">$2</a>$3') 
                         .replace(url2, '$1<a href="$2" target="_blank">$2</a>$5'); 
              $(n).after(html).remove(); 
            } 
          } 
          else if (n.nodeType == 1  &&  !/^(a|button|textarea)$/i.test(n.tagName)) { 
            linkifyThis.call(n); 
          } 
        } 
      }; 
 
  $.fn.linkify = function () { 
    return this.each(linkifyThis); 
  }; 
})(jQuery); 

function slider(j){
	clearTimeout(timer);
	var k = $('div[class^=slider_item_home]').length;
	$("#scroller-content-home").css("width", k*942);
	$('div[class^=slider_item_home]').each(function(i){
		$(this).clearQueue();
		if(i>=j){
			$(this).delay((i-j)*t).queue(function(){
				$("#scroller-content-home").animate({ "marginLeft": '-'+(i*942)+'px' }, 300);		 
			});
		}
	});
	timer = setTimeout('slider(0)', t*(k-j));	
}

function slide_home(nr){
	var k = $('div[class^=slider_item_home]').length;
	$('div[class^=slider_item_home]').each(function(i){
		$(this).clearQueue();
	});
	$("#scroller-content-home").css("width", k*942);
	$("#scroller-content-home").animate({ "marginLeft": '-'+(nr*942)+'px' }, 300);	
	clearTimeout(timer);
	timer = setTimeout('slider('+(nr+1)+')', 3000);	
}

function showSub(id){
	var btn = document.getElementById('btn_'+id);
	btn.className = 'item_over';

	var sub = document.getElementById('sub_'+id);
	sub.style.display = 'block';

	if (id>1){
		var spacer_left = document.getElementById('spacer_'+(id-1));
		spacer_left.className='spacer_left_over';
	}
	var spacer_right = document.getElementById('spacer_'+id);
	spacer_right.className = 'spacer_right_over';
}

function hideSub(id){
	var btn = document.getElementById('btn_'+id);
	btn.className = 'item';

	var sub = document.getElementById('sub_'+id);
	sub.style.display = 'none';

	if (id>1){
		var spacer_left = document.getElementById('spacer_'+(id-1));
		spacer_left.className='spacer';
	}
	var spacer_right = document.getElementById('spacer_'+id);
	spacer_right.className = 'spacer';
}

function showInfo(id){
	document.getElementById("info_"+id).style.display = "block";	
}

function hideInfo(id){
	document.getElementById("info_"+id).style.display = "none";	
}


function loadLightboxImage(img){
	var myImg = document.createElement("a");
	myImg.href = img;
	showLightbox(myImg);
}
