/* This Javascript file is used for displaying Qcue information on client sites.  It includes 3rd party
 * Javascript (each denoted with its own header and copyright below).
 * 
 * BxSlider - 1.0 - jquery.bxSlider.js
 * Qcue code at bottom (since leverages third party code).
 */

/**
 * 
 * 
 * bxSlider: Content slider / fade / ticker using the jQuery javascript library.
 * 
 * Author: Steven Wanderski Email: wandoledzep@gmail.com URL:
 * http://bxslider.com
 * 
 * License: MIT
 * 
 * 
 */

jQuery.fn.bxSlider = function(options) {

	// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Declare variables and functions
	// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
	var defaults = {
		mode : 'slide',
		speed : 500,
		auto : false,
		auto_direction : 'left',
		pause : 2500,
		controls : true,
		prev_text : 'prev',
		next_text : 'next',
		width : $(this).children().width(),
		prev_img : '',
		next_img : '',
		ticker_direction : 'left',
		wrapper_class : 'container'
	};

	options = $.extend(defaults, options);

	if (options.mode == 'ticker') {
		options.auto = true;
	}

	var $this = $(this);

	var $parent_width = options.width;
	var current = 0;
	var is_working = false;
	var is_disabled = false; // QCUE - added to allow disablement
	var child_count = $this.children().size();
	var i = 0;
	var j = 0;
	var k = 0;

	function animate_next() {
		if (!is_disabled) { // QCUE - Added check to disable
			is_working = true;
			
			// QCUE - Changed the logic so that we put the first object after this one before moving
			$this.css( {
				'left' : 0 + 'px'
			}).children(':first').appendTo($this);
		
			$this.animate( {
				'left' : '-' + $parent_width + 'px' // QCUE - Then changed -1x instead of -2x
			}, options.speed, function() {

				// QCUE - Then deleted the move here since already done
		
				is_working = false;
		
			});
		}
	}

	function animate_prev() {
		if (!is_disabled) { // QCUE - Added check to disable
			is_working = true;
	
			$this.animate( {
				'left' : 0
			}, options.speed, function() {
	
				$this.css( {
					'left' : '-' + $parent_width + 'px'
				}).children(':last').insertBefore($this.children(':first'));
	
				is_working = false;
	
			});
		}
	}

	function fade(direction) {

		if (direction == 'next') {

			var last_before_switch = child_count - 1;
			var start_over = 0;
			var incr = k + 1;

		} else if (direction == 'prev') {

			var last_before_switch = 0;
			var start_over = child_count - 1;
			var incr = k - 1;

		}

		is_working = true;

		if (k == last_before_switch) {

			$this.children().eq(k).fadeTo(options.speed, 0);
			$this.children().eq(start_over).fadeTo(options.speed, 1,
					function() {

						is_working = false;

						k = start_over;

					});

		} else {

			$this.children().eq(k).fadeTo(options.speed, 0);
			$this.children().eq(incr).fadeTo(options.speed, 1, function() {

				is_working = false;

				k = incr;

			});

		}

	}

	function add_controls() {

		// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
		// Check if user selected images to use for next / prev
		// ///////////////////////////////////////////////////////////////////////////////////////////////////////////

		if (options.prev_img != '' || options.next_img != '') {

			$this
					.parent()
					.append(
							'<a class="slider_prev" href=""><img src="'
									+ options.prev_img
									+ '" alt=""/></a><a class="slider_next" href=""><img src="'
									+ options.next_img + '" alt="" /></a>');

		} else {

			$this.parent().append(
					'<a class="slider_prev" href="">' + options.prev_text
							+ '</a><a class="slider_next" href="">'
							+ options.next_text + '</a>');

		}

		$this.parent().find('.slider_prev').css( {
			'float' : 'left',
			'outline' : '0'
		});
		$this.parent().find('.slider_next').css( {
			'float' : 'right',
			'outline' : '0'
		});

		// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
		// Accomodate padding-top for controls when elements are absolutely
		// positioned (only in fade mode)
		// ///////////////////////////////////////////////////////////////////////////////////////////////////////////

		if (options.mode == 'fade') {

			$this.parent().find('.slider_prev').css( {
				'paddingTop' : $this.children().height()
			})
			$this.parent().find('.slider_next').css( {
				'paddingTop' : $this.children().height()
			})

		}

		// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
		// Actions when user clicks next / prev buttons
		// ///////////////////////////////////////////////////////////////////////////////////////////////////////////

		qcueEnableButtons(); // QCUE - Moved to separate function

	}

	function qcueEnableButtons() {
		$('.slider_next').click(function() { // QCUE - Doesn't have to be on
												// parent; was
												// $this.parent().find

					if (!is_working) {

						if (options.mode == 'slide') {

							animate_next();

							if (options.auto) {

								clearInterval($.t);

								$.t = setInterval(function() {
									animate_next();
								}, options.pause);

							}

						} else if (options.mode == 'fade') {

							fade('next');

							if (options.auto) {

								clearInterval($.t);

								$.t = setInterval(function() {
									fade('next');
								}, options.pause);

							}

						}

					}

					return false;

				});

		$('.slider_prev').click(function() { // QCUE - Doesn't have to be on
												// parent; was
												// $this.parent().find

					if (!is_working) {

						if (options.mode == 'slide') {

							animate_prev();

							if (options.auto) {

								clearInterval($.t);

								$.t = setInterval(function() {
									animate_prev();
								}, options.pause);

							}

						} else if (options.mode == 'fade') {

							fade('prev');

							if (options.auto) {

								clearInterval($.t);

								$.t = setInterval(function() {
									fade('prev');
								}, options.pause);

							}

						}

					}

					return false;

				});
	}

	function ticker() {

		if (options.ticker_direction == 'left') {

			$this.animate( {
				'left' : '-' + $parent_width * 2 + 'px'
			}, options.speed, 'linear', function() {

				$this.css( {
					'left' : '-' + $parent_width + 'px'
				}).children(':first').appendTo($this);

				ticker();

			});

		} else if (options.ticker_direction == 'right') {

			$this.animate( {
				'left' : 0
			}, options.speed, 'linear', function() {

				$this.css( {
					'left' : '-' + $parent_width + 'px'
				}).children(':last').insertBefore($this.children(':first'));

				ticker();

			});

		}

	}

	// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Create content wrapper and set CSS
	// ///////////////////////////////////////////////////////////////////////////////////////////////////////////

	$this.wrap('<div class="' + options.wrapper_class + '"></div>');

	// console.log($this.parent().css('paddingTop'));

	if (options.mode == 'slide' || options.mode == 'ticker') {

		$this.parent().css( {
			'overflow' : 'hidden',
			'position' : 'relative',
			'width' : options.width + 'px'
		});

		$this.css( {
			'width' : '999999px',
			'position' : 'relative',
			'left' : '-' + $parent_width + 'px'
		});

		$this.children().css( {
			'float' : 'left',
			'width' : $parent_width
		});

		$this.children(':last').insertBefore($this.children(':first'));

	} else if (options.mode == 'fade') {

		$this.parent().css( {
			'overflow' : 'hidden',
			'position' : 'relative',
			'width' : options.width + 'px'
		// 'height' : $this.children().height()
				});

		if (!options.controls) {
			$this.parent().css( {
				'height' : $this.children().height()
			});
		}

		$this.children().css( {
			'position' : 'absolute',
			'width' : $parent_width,
			'listStyle' : 'none',
			'opacity' : 0
		});

		$this.children(':first').css( {
			'opacity' : 1
		});

	}

	// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Check if user selected "auto"
	// ///////////////////////////////////////////////////////////////////////////////////////////////////////////

	if (!options.auto) {

		add_controls();

	} else {

		if (options.mode == 'ticker') {

			ticker();

		} else {

			// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
			// Set a timed interval
			// ///////////////////////////////////////////////////////////////////////////////////////////////////////////

			if (options.mode == 'slide') {

				if (options.auto_direction == 'left') {

					$.t = setInterval(function() {
						animate_next();
					}, options.pause);

				} else if (options.auto_direction == 'right') {

					$.t = setInterval(function() {
						animate_prev();
					}, options.pause);

				}

			} else if (options.mode == 'fade') {

				if (options.auto_direction == 'left') {

					$.t = setInterval(function() {
						fade('next');
					}, options.pause);

				} else if (options.auto_direction == 'right') {

					$.t = setInterval(function() {
						fade('prev');
					}, options.pause);

				}

			}

			if (options.controls) {

				add_controls();

			} else { // QCUE - Enable buttons regardless
				qcueEnableButtons();
			}

		}

		// QCUE - Added to disable auto on hover
		$this.parent().hover(
			function () {
				is_disabled = true;
			},
			function () {
				is_disabled = false;
			});

	}

}

/*
 * Qcue code
 * 
 * Copyright (c) 2007 - 2009 Qcue, Inc.
 */

var qcueTickerUrl = qcueBaseUrl + "qcueWidget?promoterId=";

$(document).ready(function() {
	qcueLoadTickerPrices();
});

function qcueLoadTickerPrices() {
	var newScript = document.createElement('script');
	newScript.type = 'text/javascript';
	qcueTickerUrl = qcueTickerUrl + $('#qcueWidgetId').val();
	if ($('#qcueNumberOfGames').length)
		qcueTickerUrl = qcueTickerUrl + '&numberOfGames=' + $('#qcueNumberOfGames').val();
	newScript.src = qcueTickerUrl;
	document.getElementsByTagName("head")[0].appendChild(newScript);
}

function qcueDisplayWidget(response) {
	for (var x in response.Qcue) {
		var event = response.Qcue[x];
		var html = getBigHTML(event);
		if (html != null)
			$('#qcueReplace').append(html);
	}
	$('#qcueReplace').bxSlider( {
		mode : 'slide',
		auto : true,
		speed : 500,	
		pause : 3000,
		width : 253,
		controls : false,
		wrapper_class : 'qcueContainer'
	});
}

function getBigHTML(event) {
	var html =
		  '		<div>\n'
		+ '			<div class="qcueClearFix qcueFloatHolder" style="width:253px">\n'
		+ '				<div class="qcueHBoxLeft">\n'
		+ '					<div style="padding:7px">\n'
		+ '						<div class="qcueTitle1">\n' + event.OpponentName + '<br />\n'
		+ '							<div class="qcueTtl3">' + event.EventDate + '</div>\n'
		+ '						</div>\n'
		+ '					</div>\n'
		+ '				</div>\n'
		+ '				<div class="qcueHBoxRight">\n'
		+ '					<div style="padding:1px">\n'
		+ '						<img height="50" width="65" src="' + qcueBaseUrl + event.LogoURL + '" alt="" /></div></div>\n'
		+ '			</div>\n'
		+ '			<div class="qcueHBoxCenter">\n'
		+ '				<div style="padding-top:7px; padding-left:9px">\n'
		+ '					<div class="qcueTitle2">\n';
	if (event.FeaturedPrice != null) {
		html +=
			  '						<img src="' + qcueBaseUrl + 'siteimages/widget/featured.png" style="margin-top:4px" alt="Featured Ticket" /><br />\n'
			+ '						<div class="qcueAlign">\n'
			+ '							<a class="qcueBuyLink" href="' + event.BuyLink + '">\n'
			+ '								<div class="qcueBl">$' + event.FeaturedPrice + '</div>\n'
			+ '								<span class="qcueColor1">' + event.FeaturedSection + '</span>&nbsp;\n'
			+ '								<span style="color:white">BUY</span>\n'
			+ '							</a>\n'
			+ '						</div>\n'
	}
	html +=
			  '						<img src="' + qcueBaseUrl + 'siteimages/widget/prices.png" style="margin-top:7px" alt="Prices Starting At" /><br />\n'
			+ '						<div class="qcueAlign">\n'
			+ '							<a class="qcueBuyLink" href="' + event.BuyLink + '">\n'
			+ '								<div class="qcueBl">$' + event.MinPrice + '</div>\n'
			+ '								<span class="qcueColor1">' + event.MinPriceSection + '</span>&nbsp;\n'
			+ '								<span style="color:white">BUY</span>\n'
			+ '							</a>\n'
			+ '						</div>\n'
			+ '						<a class="qcueTitle2 qcueBuyLink" href="' + event.BuyLink + '"><br/>See all dynamic prices &nbsp;\n'
			+ '							<img src="' + qcueBaseUrl + 'siteimages/widget/arrow.png" alt="" />\n'
			+ '						</a>\n'
			+ '					</div>\n'
			+ '				</div>\n'
			+ '			</div>\n'
			+ '		</div>\n';
	return html;
}

function qcueRollOver(imgId, imgSrc)
{
	$('#' + imgId).attr("src", imgSrc);
}
