var allowAutoMove = true; //user interaction will set this to false.
var allowWidgetAction = true;

function doFeaturedMove(oldNum, newNum) {
	jQuery('#home-image-'+oldNum).hide();
	jQuery('#home-image-text-'+oldNum).hide();
	jQuery('#home-image-button-'+oldNum).removeClass('active');
	
	jQuery('#home-image-'+newNum).show();
	jQuery('#home-image-text-'+newNum).show();
	jQuery('#home-image-button-'+newNum).addClass('active');
}

function featuredMove() {
	if(allowAutoMove) {
		//get the currently selected item
		var selectedLi = jQuery('#homesubnav ul li a.active');
		var num = parseInt(selectedLi.attr('id').substr(18));
		
		var newNum = num + 1;
		if(newNum > 2) {
			newNum = 0;
		}
		doFeaturedMove(num, newNum);
		
		setTimeout(featuredMove, 5000);
	}
}

function findWidgetContainer(obj) { //be sure the obj is in a .widget, or i'm not sure what will happen :)
	while(jQuery(obj).attr('className') != 'widget') {
		obj = jQuery(obj).parent();
	}
	
	return jQuery(obj);
}

function saveUsersWidgets(reload) {
	allowWidgetAction = false;
	var options = jQuery("#homepage-widgets-form").serialize();
	jQuery.ajax({
			url: '/ajax_requests.htm?action=save-homepage',
			data: options,
			type: 'post',
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				alert('There was an error saving your personalized homepage.  Please try again later');
				allowWidgetAction = true;
			},
			success: function (data, textStatus) {
				if(parseInt(data) != 1) {
					alert('There was an error saving your personalized homepage.  Please try again later');
				}
				allowWidgetAction = true;
				if(reload) {
					window.location = '/';
				}
			}
	});
}
function saveHomeWidget(obj) {
	var wid = findWidgetContainer(obj);
	
	jQuery('#'+wid.attr('id')+' .inner:last-child').hide();
	jQuery('#'+wid.attr('id')+' .inner:first-child').show();
	saveUsersWidgets(true);
	
	//window.location = '/';
	//todo, ajax reload widget
}

function getWidgetCats(obj, loggedin) {
	if(!loggedin) {
		alert('To use this great functionality that lets true Urban Warriors customise their own Ajax Cape Town website you first need to register or login');
		return;
	}
	if(obj.value == 1) { //WIDGET_TICKETS
		window.location = "/ajax_requests.htm?action=add-new-widget&type=1";
	}else if(obj.value == 2) { //WIDGET_PROMOTION
		window.location = "/ajax_requests.htm?action=add-new-widget&type=2";
	}else if(obj.value == 3) { //WIDGET_TWITTER
		jQuery.ajax({
				url: '/ajax_requests.htm?action=show-widget-cats&type=3',
				type: 'get',
				error: function (XMLHttpRequest, textStatus, errorThrown) {
					alert('There was a problem trying to get the twitter users you can view.  Please try again in a few minutes');
				},
				success: function (data, textStatus) {
					jQuery('#widget-options').attr('innerHTML', data);
					jQuery('#widget-options').jqTransform(); //pretty the form 
				}
		});
	}else if(obj.value == 4) { //WIDGET_LATEST_NEWS
		jQuery.ajax({
				url: '/ajax_requests.htm?action=show-widget-cats&type=4',
				type: 'get',
				error: function (XMLHttpRequest, textStatus, errorThrown) {
					alert('There was a problem trying to get the categories you can view.  Please try again in a few minutes');
				},
				success: function (data, textStatus) {
					jQuery('#widget-options').attr('innerHTML', data);
					jQuery('#widget-options').jqTransform(); //pretty the form 
				}
		});
	}else if(obj.value == 5) { //WIDGET_EVENTS
		jQuery.ajax({
				url: '/ajax_requests.htm?action=show-widget-cats&type=5',
				type: 'get',
				error: function (XMLHttpRequest, textStatus, errorThrown) {
					alert('There was a problem trying to get the categories you can view.  Please try again in a few minutes');
				},
				success: function (data, textStatus) {
					jQuery('#widget-options').attr('innerHTML', data);
					jQuery('#widget-options').jqTransform(); //pretty the form 
				}
		});
	}else if(obj.value == 6) { //WIDGET_GALLERY
		jQuery.ajax({
				url: '/ajax_requests.htm?action=show-widget-cats&type=6',
				type: 'get',
				error: function (XMLHttpRequest, textStatus, errorThrown) {
					alert('There was a problem trying to get the categories you can view.  Please try again in a few minutes');
				},
				success: function (data, textStatus) {
					jQuery('#widget-options').attr('innerHTML', data);
					jQuery('#widget-options').jqTransform(); //pretty the form 
				}
		});
	}else if(obj.value == 7) { //WIDGET_LATEST_VIDEO
		window.location = "/ajax_requests.htm?action=add-new-widget&type=7";
	}else if(obj.value == 8) { //WIDGET_FAN
		window.location = "/ajax_requests.htm?action=add-new-widget&type=8";
	}else if(obj.value == 9) { //WIDGET_PLAYER
		jQuery.ajax({
				url: '/ajax_requests.htm?action=show-widget-cats&type=9',
				type: 'get',
				error: function (XMLHttpRequest, textStatus, errorThrown) {
					alert('There was a problem trying to get the categories you can view.  Please try again in a few minutes');
				},
				success: function (data, textStatus) {
					jQuery('#widget-options').attr('innerHTML', data);
					jQuery('#widget-options').jqTransform(); //pretty the form 
				}
		});		
	}
}

function addHomePageWidget(type, obj) {
	if(type == 3) { //WIDGET_TWITTER
		window.location = "/ajax_requests.htm?action=add-new-widget&type=3&opt="+obj.value;
	}else if(type == 4) {	//WIDGET_LATEST_NEWS
		window.location = "/ajax_requests.htm?action=add-new-widget&type=4&opt="+obj.value;
	}else if(type == 5) {	//WIDGET_EVENTS
		window.location = "/ajax_requests.htm?action=add-new-widget&type=5&opt="+obj.value;
	}else if(type == 6) {	//WIDGET_GALLERY
		window.location = "/ajax_requests.htm?action=add-new-widget&type=6&opt="+obj.value;
	}else if(type == 9) {	//WIDGET_PLAYER
		window.location = "/ajax_requests.htm?action=add-new-widget&type=9&opt="+obj.value;		
	}
}

jQuery(function() {
		//wait 5 seconds, then move to the next featured scroller
		setTimeout(featuredMove, 5000);
		
		jQuery('a.home-image-button').click(function(e) {
				allowAutoMove = false; //stop the auto moving of the featured scroller
				
				//get the currently selected item
				var selectedLi = jQuery('#homesubnav ul li a.active');
				var num = parseInt(selectedLi.attr('id').substr(18));
				var newNum = parseInt(this.id.substr(18));
				doFeaturedMove(num, newNum);
				
				e.preventDefault();
				return false;
		});
		
		//sortable home page
		jQuery("#homelisting ul, #homelisting li").disableSelection();
		jQuery("#homelisting").sortable({
				revert: true,
				handle: 'h3.widget-title',
				update: function() {
					saveUsersWidgets();
				}
		});
		
		jQuery('h3.widget-title a.edit').click(function(e) {
				if(allowWidgetAction) {				
					var wid = findWidgetContainer(this);
					jQuery('#'+wid.attr('id')+' .inner:last-child').show();
					jQuery('#'+wid.attr('id')+' .inner:first-child').hide();
				}
				e.preventDefault();
				return false;
		});
		
		jQuery('h3.widget-title a.close').click(function(e) {
				if(allowWidgetAction) {
					if(confirm('Are you sure you want to remove this widget?  You can always re-add it later using the options at the bottom of the page.')) {
						var wid = findWidgetContainer(this);
						wid.parent().attr('id', 'temp-id');
						
						var cnt = 0;
						jQuery('#homelisting').children().each(function() {
								if(this.id == 'temp-id') {
									window.location = '/ajax_requests.htm?action=remove-widget&num='+cnt;
									return false;
								}
								cnt++;
						});
					}
				}
				e.preventDefault();
				return false;
		});

});
