/*!
 * JQUERY.calCrawl The Calendar Crawling plugin!
 * Copyright (c) 2010 Mike Newell
 * http://m-ln.com
 * Licensed under MIT
 * http://www.opensource.org/licenses/mit-license.php
 * Launch  : June 2010
 * Version : 0.0.1
 * Released: Tuesday 7 June, 2010 - 00:00
 */
function crawlCal() {
(function ($) {	
	function calSetup() {
		setUpNext();
		var dest = '';
		var linkCount = $('.calActive a');
		// make the corresponding html
		
		
		for(i = 0, n = linkCount.length; i < n; i++) {
			var day = $(linkCount[i]).text();
			$(linkCount[i]).addClass('calLink'+day);
			// setup the inital html for the tooltips
		}
		var qtipID = -1;
		$('.calActive a').hover(function () {
										 	dest = "http://targheemusic.com/"+$(this).attr('href');
											var className = $(this).attr('class');
											//alert(className);
											var isLoaded = $(this).hasClass('loaded');
											if(isLoaded) {
												// do nothing
											} else {
												qtipID = qtipID + 1;
												//alert(qtipID);
												$("."+className).qtip({
																	  	content: "<p>Loading...</p>",
																		position: {
																			corner: {
																				tooltip: 'bottomRight',
																				target: 'topLeft'
																				}
																			},
																		show: {
																			ready: true,
																			when: 'mouseover',
																			delay: 0
																			},
																			hide: {
																				fixed: true,
																				delay: 400
																				},
																				style: {
																					border: {
																						width: 7,
																						color: '#cccccc'
																						},
																					width: 200,
																					background: '#333333',
																					color: '#ffffff',
																					padding: 15,
																					textAlign: 'center',
																					tip: true,
																					name: 'dark'
																					}
												});
												
												makeTheCall(dest, day, className);
												// add a hide right here to fade the popup out
											}
											// to load it once so I dont make ajax call everytime someone hovers over it
											$(this).addClass("loaded");
											$(this).addClass("qtipID-"+qtipID);
										 },
										 function (e) {
											 
											 });
		$('#calTActive a').hover(function () {
										 	dest = "http://targheemusic.com/"+$(this).attr('href');
											var className = $(this).attr('class');
											//alert(className);
											var isLoaded = $(this).hasClass('loaded');
											if(isLoaded) {
												// do nothing
											} else {
												qtipID = qtipID + 1;
												//alert(qtipID);
												$("."+className).qtip({
																	  	content: "<p>Loading...</p>",
																		position: {
																			corner: {
																				tooltip: 'bottomRight',
																				target: 'topLeft'
																				}
																			},
																		show: {
																			ready: true,
																			when: 'mouseover',
																			delay: 0
																			},
																			hide: {
																				fixed: true,
																				delay: 400
																				},
																				style: {
																					border: {
																						width: 7,
																						color: '#cccccc'
																						},
																					width: 200,
																					background: '#333333',
																					color: '#ffffff',
																					padding: 15,
																					textAlign: 'center',
																					tip: true,
																					name: 'dark'
																					}
												});
												
												makeTheCall(dest, day, className);
												// add a hide right here to fade the popup out
											}
											// to load it once so I dont make ajax call everytime someone hovers over it
											$(this).addClass("loaded");
											$(this).addClass("qtipID-"+qtipID);
										 },
										 function (e) {
											 
											 });
	}
	
	function makeTheCall (dest, day, className) {
		$.ajax({
			   	type:"get",
				url:dest,
				cache:false,
				success: function (data) {
					var $resp = $(data);
					// grab the content blocks
					var contentBlocks = new Array();
					contentBlocks = $resp.find('.innerPost');
					
					// foreach content block, grab specific items and output them in the template
					var arr = new Array();
					var title='';
					var time='';
					var location='';
					var hyperLink='';
					for(i = 0, n = contentBlocks.length; i < n; i++) {
						var block = $(contentBlocks[i]);
						title = "<h2>"+$(block).find('.innerPostTitle').text()+"</h2>";
						time  = "<p>"+$(block).find('.innerPostTime').text()+"</p>";
						location  = "<p>"+$(block).find('.innerPostLocation').text()+"</p>";
						hyperLink  = "<a href='"+$(block).find('.innerPostLink').attr('href')+"'>"+title+"</a>";
						
						arr.push(hyperLink, location, time);
						// at the very end we push all the data in order to the array, then loop again
						
					}
					var api = $("."+className).qtip("api");
					api.updateContent("<div class='calEventBlock'>"+arr.join("</div><div class='calEventBlock'>")+"</div>");
					
					
				}
		});
	}
	
	function setUpNext() {
		var nextLink = $("a#calNext").attr("href");
		var prevLink = $("a#calPrev").attr("href");
		$("a#calNext").click(function(event	) {
			event.preventDefault();
			$("div#calTable").fadeOut();
			$("div#cal").height(222);
			$("div#cal").load("http://targheemusic.com/index.php"+nextLink+" #cal", function () {
				calSetup();
				
				/*$("a#calPrev").click(function(event	) {
					event.preventDefault();
					$("div#cal").load("http://targheemusic.com/index.php"+prevLink+" #cal", function () {
						calSetup();
					});
				});*/
				
				setUpNext();
				
				$("div#cal").fadeIn();
			});
			
		});
		
	}
	
	calSetup();

})(jQuery);
}
