/**
 * Scripts for [Space Shower TV Plus]
 * using jQuery JavaScript Framework
 *
 * @author     RaNa design associates, inc.
 * @copyright  2011 RaNa design associates, inc.
 * @Update:    2011-03-23 21:16:38
 */

(function($) {

// ui.rotationBnr //{{{
$.widget("ui.rotationBnr", {
	_create: function (bnrData) {
		var self = this,
			interval = 4000;
		this.$bnrCtrl = $("#topPriorityBnrCtrl a");
		this.rotationId = 0;
		this.link = [];
		this.bnrData = bnrData;

		$.each(bnrData, function(i) {
			var bnr = bnrData[i];
			self.preloadImg(bnr.imgsrc);

			self.link[i] = $("<a/>", {
				href: bnr.url,
				title: bnr.title
			}).hide();
			$("<img/>", {
				"src": bnr.imgsrc,
				"alt": bnr.title
				}).appendTo(self.link[i]);
			$("<span/>").appendTo(self.link[i]);

			self.link[i].click(function (e) {
				self.changeLocation(bnr.url, bnr.blank);
				e.preventDefault();
			}).hover(function () {
				clearInterval(self.timer);
			}, function () {
				self.timer = setTimer();
			});

			$(self.$bnrCtrl[i]).click(function (e) {
				self.changeLocation(bnr.url, bnr.blank);
				e.preventDefault();
			}).hover(function () {
				clearInterval(self.timer);
				self.rotation(i);
			}, function () {
				self.timer = setTimer();
			});
		});

		this.rotation(undefined, function () {
			self.timer = setTimer();
		});

		function setTimer() {
			return setInterval(function () {
				self.rotation();
			}, interval);
		}
	},
	//
	// Followings are prototype methods.
	//
	preloadImg: function(src) {
		(new Image()).src = src;
	},
	changeLocation: function (url, blankWindow) {
		if (blankWindow) {
			window.open(url);
		} else {
			location.href = url;
		}
	},
	updateIndex: function () {
		this.rotationId ++;
		if (this.rotationId >= this.bnrData.length) {
			this.rotationId = 0;
		}
	},
	rotation: function (index, callback) {
		var self = this,
			$box = $("#topPriorityBnrImg");
		$.each(this.link, function () {
			this.stop(true, true);
		});
		if (index != undefined) {
			// called from navigation hover event
			this.rotationId = index;
			this.link[this.rotationId]
				.appendTo($box)
				.show();
			this.updateIndex();
			this.$bnrCtrl.each(function (i) {
				(i === index) ?
					$(this).addClass("on") : $(this).removeClass("on");
			});
			if (callback) { callback(); }
		} else {
			// called from setInterval()
			this.link[this.rotationId]
				.hide()
				.appendTo($box)
				.fadeIn(250, function() {
					self.updateIndex();
					if (callback) { callback(); }
				});
			this.$bnrCtrl.each(function (i) {
				(i === self.rotationId) ?
					$(this).addClass("on") : $(this).removeClass("on");
			});
		}
	}
});
//}}}

$(function() {
// run ui.rotationBnr widget
$.ajax("/js/topPriority.json", {
	dataType: "json",
	cache: false,
	success: function (data) {
		var topPriority = new $.ui.rotationBnr();
		topPriority._create(data);
	}
});

// Adjust height of "2nd/3rd priority" culumns
if ($.ui.adjustHeight) {
	function adjustHeight($element) {
		var clmArr = [],
			tmpI = 0;
		$element.each(function (i) {
			if ((i % 2) != 0) {
				clmArr.push($element[i - 1]);
				clmArr.push($element[i]);
				var clm1 = new $.ui.adjustHeight();
				clm1._create($(clmArr));
				tmpI = 0;
			} else {
				clmArr = [];
				tmpI ++;
			}
		});
	}
	var $clm1 = $('.mod_2nd3rdPriority .section01>.sectionInner');
	var $clm2 = $('.mod_2nd3rdPriority .section02>.sectionInner');
	setTimeout(function() {
		adjustHeight($clm1);
		adjustHeight($clm2);
	}, 500)
}

});

})(jQuery);


if (!window.console) { window.console = { log: function(args) { for (var i=0,l=args.length; i<l ; i++) { alert(args[i]); } } } }

