
//Rotate Promotions on the O'Reilly School of Technology Homepage
$(document).ready(function () {
	var numPromos = 0;
	var curPromo = 0;
	var promoMoving = false;


	var itemsToLoad = new Array();
	var priorityArray = new Array();
	var itemList = "/promo-rotate-january-2011.php";
	
	//Load the speakers
	$.ajax({
		type: "GET",
		url: itemList,
		dataType: "xml",
		success: loadItems
	});
				
	
	function loadItems(xml) {
		$(xml).find('item').each(function(i) {
			itemsToLoad[i] = new Array();
			itemsToLoad[i][0] = $(this);
			itemsToLoad[i][1] = $(this).attr('priority');
		});
		
		//Put the priority speakers in a separate array
		for (var i = 0; i < itemsToLoad.length; i++) {
			if (itemsToLoad[i][1] == 'true') {
				priorityArray = priorityArray.concat(itemsToLoad.splice(i, 1));
				i--;
			}
		}
		priorityArray.sort(function randOrd(){return (Math.round(Math.random())-0.5); }); //randomize the priority array
		itemsToLoad.sort(function randOrd(){return (Math.round(Math.random())-0.5); }); //randomize the items array
		itemsToLoad = priorityArray.concat(itemsToLoad); //add the two arrays back together
		

		//Create and display the containers for each item
		for (var i = 0; i < itemsToLoad.length; i++) {
			if (itemsToLoad[i][0].children().length == 2) {
				var listItem = $('<li class="two-column"></li>');
				
				itemsToLoad[i][0].children().each(function(e) {
					if (e == 0) {
						var block = $('<span class="left"></span>');

						$(this).children().each(function() {
							var aLink;
							if ($(this).attr('onclick') != null && $(this).attr('onclick') != 'undefined') {
								aLink = $('<a href="' + $(this).attr('href') + '" onclick="' + $(this).attr('onclick') + '"></a>');
							}
							else {
								aLink = $('<a href="' + $(this).attr('href') + '"></a>');
							}

							$(this).children().each(function() {
								var img = $('<img />').attr('src', $(this).attr('src')).attr('width', $(this).attr('width')).attr('height', $(this).attr('height')).attr('alt', $(this).attr('alt')).attr('border', $(this).attr('border'));

								aLink.append(img);
							});

							block.append(aLink);
						});

						listItem.append(block);
					}
					else if (e == 1) {
						var block = $('<span class="right"></span>');

						$(this).children().each(function(d) {
							var span = $('<span></span>');

							if ($(this).attr('class')) {
								span.attr('class', $(this).attr('class'));
							}

							$(this).children().each(function() {
								var p = $('<p></p>');
								if ($(this).attr('class')) {
									p.attr('class', $(this).attr('class'));
								}

								$(this).contents().each(function() {
									if ($(this)[0].nodeType == 1) {
										if ($(this)[0].nodeName == 'br') {
											p.append('<br />');
										}
										else if ($(this)[0].nodeName == 'a') {
											var aLink;
											if ($(this).attr('onclick') != null && $(this).attr('onclick') != 'undefined') {
												aLink = $('<a href="' + $(this).attr('href') + '" onclick="' + $(this).attr('onclick') + '"></a>');
											}
											else {
												aLink = $('<a href="' + $(this).attr('href') + '"></a>');
											}

											$(this).contents().each(function() {
												if ($(this)[0].nodeType == 3) {
													if ($(this)[0].nodeValue != null) {
														aLink.append($(this)[0].nodeValue);
													}
												}
											});
								
											p.append(aLink);
										}
										else if ($(this)[0].nodeName == 'span') {
											var pSpan = $('<span></span>');

											if ($(this).attr('class')) {
												pSpan.attr('class', $(this).attr('class'));
											}

											$(this).contents().each(function() {
												if ($(this)[0].nodeType == 3) {
													if ($(this)[0].nodeValue != null) {
														pSpan.append($(this)[0].nodeValue);
													}
												}
											});

											p.append(pSpan);
										}
									}
							
									if ($(this)[0].nodeType == 3) {
										if ($(this)[0].nodeValue != null) {
											p.append($(this)[0].nodeValue);
										}
									}
								});

								span.append(p);
							});

							block.append(span);

						});

						listItem.append(block);
					}					
				});

				$('#promo-component > .promo-content').append(listItem);
			}
			else {
				var listItem = $('<li class="one-column"></li>');
				itemsToLoad[i][0].children().each(function() {
					var block = $('<span></span>');

					$(this).children().each(function() {
						var aLink;
						if ($(this).attr('onclick') != null && $(this).attr('onclick') != 'undefined') {
							aLink = $('<a href="' + $(this).attr('href') + '" onclick="' + $(this).attr('onclick') + '"></a>');
						}
						else {
							aLink = $('<a href="' + $(this).attr('href') + '"></a>');
						}

						$(this).children().each(function() {
							var img = $('<img />');
							img.attr('src', $(this).attr('src')).attr('width', $(this).attr('width')).attr('height', $(this).attr('height')).attr('alt', $(this).attr('alt')).attr('border', $(this).attr('border'));

							aLink.append(img);
						});

						block.append(aLink);
					});

					listItem.append(block);
				});

				$('#promo-component > .promo-content').append(listItem);
			}
		}


		//Move all but the first item 600px to the right.
		//######################################################################
		$('#promo-component > .promo-content').children().each(function(e) {
			if (e == 0) {
				$(this).css('left', '0');
			}
			else {
				$(this).css('left', '600px');
				numPromos ++;
			}
		});
		//######################################################################
	
	
		//If there are multiple promotions, show and activate the arrows.
		//######################################################################
		if (numPromos > 0) {
			$('#promo-component > .left-arrow').fadeIn(500).click(function(e) {
				e.preventDefault();
				MoveRight();
			});
	
			$('#promo-component > .right-arrow').fadeIn(500).click(function(e) {
				e.preventDefault();
				MoveLeft();
			});
		}
		//######################################################################
	}

	var promoInterval =	setInterval(function () { MoveLeft(); }, 10000);

	$('#promo-component').mouseover(function() {
		clearInterval(promoInterval);
	}).mouseout(function() {
		promoInterval = setInterval(function () { MoveLeft(); }, 10000);
	});

	function MoveLeft() {
		if (!promoMoving) {
			promoMoving = true;

			var nextPromo = 0;
			if (curPromo < numPromos) {
				nextPromo = curPromo + 1;
			}
			else {
				nextPromo = 0;
			}
			
			$('#promo-component > .promo-content').children().each(function(e) {
				if (e == curPromo) {
					
					$(this).animate({left:'-600px'}, 1000, function() {
						promoMoving = false;
					});
				}
				else if (e == nextPromo) {
					$(this).css('left', '600px').animate({left:'0px'}, 1000);
				}
			});

			if (curPromo == numPromos) {
				curPromo = 0;
			}
			else {
				curPromo ++;
			}
		}
	}


	function MoveRight() {
		if (!promoMoving) {
			promoMoving = true;

			var nextPromo = 0;
			if (curPromo > 0) {
				nextPromo = curPromo - 1;
			}
			else {
				nextPromo = numPromos;
			}
			
			$('#promo-component > .promo-content').children().each(function(e) {
				if (e == curPromo) {
					
					$(this).animate({left:'600px'}, 1000, function() {
						promoMoving = false;
					});
				}
				else if (e == nextPromo) {
					$(this).css('left', '-600px').animate({left:'0px'}, 1000);
				}
			});

			if (curPromo == 0) {
				curPromo = numPromos;
			}
			else {
				curPromo --;
			}
		}
	}

});

