jQuery(document).ready(function(){

	var toggleContent = function(e)
	{
		var id = jQuery(this.parentNode.parentNode).attr('id');
		var targetContent = jQuery('div.itemContent', this.parentNode.parentNode);
		var toggleState;

		if (targetContent.css('display') == 'none') {
			targetContent.slideToggle(300);
			jQuery(this).html('<img class="toggleIcon" src="' + bloginfoTemplateUrl + '/images/collapseIcon.gif" alt="" />');
			toggleState = 'expanded';
		} else {
			targetContent.slideUp(300);
			jQuery(this).html('<img class="toggleIcon" src="' + bloginfoTemplateUrl + '/images/expandIcon.gif" alt="" />');
			toggleState = 'collapsed';
		}
		jQuery.cookie(id + 'ToggleState', toggleState, { expires: 7 });

		return false;
	};

	function serialize(sortID) {

		serial = jQuery('div#' + sortID).sortable("serialize", { key: sortID, expression: '(.+)' });
		//alert (serial);
		return serial;
	};

	var getBoxCode = function(boxUrl, boxInternalName) {

		jQuery.ajax({
			url: boxUrl,
			datatype: 'html',
			async: true,
			success: function(html)
			{
				jQuery('#' + boxInternalName).append(html);
				//jQuery('div.groupWrapperNarrow').Sortable('html');

				var boxToggleState = jQuery.cookie(boxInternalName + 'ToggleState');
				
				if (boxToggleState != null && boxToggleState == 'collapsed')
				{
					jQuery('div.itemHeader', jQuery('#' + boxInternalName)).children('a.closeEl').html('<img class="toggleIcon" src="' + bloginfoTemplateUrl + '/images/expandIcon.gif" alt="" />');
					jQuery('div.itemContent', jQuery('#' + boxInternalName)).css('display', 'none');
				}
				jQuery('div.itemHeader', jQuery('#' + boxInternalName)).children('a.closeEl').bind('click', toggleContent);
			}
		});
	};

	function buildBoxes(boxOrder, colWidth) {
	
		var boxOrderArray = boxOrder.split('&');
		var i = 0;
		var boxInternalName;
		
		for (i = 0; i < boxOrderArray.length; i++)
		{
			boxInternalName = boxOrderArray[i].split("=")[1]

			if (boxInternalName != null)
			{
				jQuery('div.groupWrapper' + colWidth).append('<div id="' + boxInternalName + '" class="groupItem' + colWidth + ' sortable"></div>');
				getBoxCode('/wp-content/themes/ssradio/boxes/' + boxInternalName + '.php', boxInternalName);
				//jQuery('div.groupWrapper' + colWidth).sortable("refresh");
			}
		}
	}

	var boxOrderWide = jQuery.cookie('ssrBoxOrderWide');
	var boxOrderNarrow = jQuery.cookie('ssrBoxOrderNarrow');
	var defaultBoxOrderArray;

	if (boxOrderWide == null)
	{
		defaultBoxOrderArray = new Array('recentlyArchivedShows', 'latestNewsAndEvents'/*, 'latestDiscussions'*/);
	
		boxOrderWide = 'sortNarrow[]=' + defaultBoxOrderArray.join('&sortNarrow[]=');
	}

	//buildBoxes(boxOrderWide, 'Wide');

	if (boxOrderNarrow == null)
	{
		defaultBoxOrderArray = new Array('connections', 'shoutbox', 'mailingList', 'scheduleSummary', 'thisWeeksTopShows', 'memberStats', 'latestReleases', 'supportTheCause');
	
		boxOrderNarrow = 'sortNarrow[]=' + defaultBoxOrderArray.join('&sortNarrow[]=');
	}

	//buildBoxes(boxOrderNarrow, 'Narrow');

	jQuery('a').click(function(){
		if (this.blur())
			this.blur();
	});

	function nowAndNext_itemLoadCallback(carousel, state)
	{
		if (state != 'init')
			return;

	
		jQuery.get(
			'/wp-content/themes/ssradio/nowAndNext.php',
			{ first: carousel.first, last: carousel.last },
			function(xml) {
				nowAndNext_itemAddCallback(carousel, carousel.first, carousel.last, xml);
			},
			'xml'
		);
	};
	
	function nowAndNext_itemAddCallback(carousel, first, last, xml)
	{
		var image, time, info, name;
	
		// Set the size of the carousel
		carousel.size(parseInt(jQuery('total', xml).text()));
	
		jQuery('showData', xml).each(function(i) {

			image = jQuery('image', jQuery(this)).text();
			time = jQuery('time', jQuery(this)).text();
			name = jQuery('name', jQuery(this)).text();
			categorySlug = jQuery('categorySlug', jQuery(this)).text();
			info = jQuery('info', jQuery(this)).text();

			carousel.add(first + i, nowAndNext_getItemHTML(image, time, name, categorySlug, info));
		});

		//overlay on hover
		jQuery(".jcarousel-list li").hover(
			function () {
				jQuery(this).children('div.overlay').animate({top: "1px", height: "104px"});
			}, 
			function () {
				jQuery(this).children('div.overlay').animate({height: "16px", top: "94px"});
			}
		);
	};

	function nowAndNext_getItemHTML(url, timeString, name, categorySlug, info)
	{
		return '<div class="overlay"><p class="timeString">' + timeString + '</p><p><a href="' + bloginfoUrl + '/category/shows/' + categorySlug + '/">' + name + '</a></p><p>' + info + '</p></div><img src="' + url + '" width="213" height="116" alt="" />';
	};

	jQuery('#nowAndNext').jcarousel({
		scroll: 4,
		itemLoadCallback: nowAndNext_itemLoadCallback
	});

	jQuery('div.groupWrapperWide').sortable({

		items: '> div.groupItemWide.sortable',
		placeholder: 'sortHelper',
		handle: 'div.itemHeader',
		tolerance: 'guess',
		scroll: true,
		revert: true,
		change: function(e, ui){
			jQuery.cookie('ssrBoxOrderWide', serialize('sortWide'), { expires: 7 });
		},
		start: function(){
			//jQuery.iAutoscroller.start(this, document.getElementsByTagName('body'));
		},
		stop: function(){
			//jQuery.iAutoscroller.stop();
		}
	});

	jQuery('div.groupWrapperNarrow').sortable({

		axis: 'y', 
		items: '> div.groupItemNarrow.sortable',
		placeholder: 'sortHelper',
		handle: 'div.itemHeader',
		tolerance: 'guess',
		scroll: true,
		revert: true,
		refreshPositions: true,
		change: function(e, ui){
			jQuery.cookie('ssrBoxOrderNarrow', serialize('sortNarrow'), { expires: 7 });
		},
		start: function(){
			//jQuery.iAutoscroller.start(this, document.getElementsByTagName('body'));
		},
		stop: function(){
			//jQuery.iAutoscroller.stop();
		}
	});
});

