/*!
 * jQuery symSponsorFlip Helper Scripts v1.00
 * http://symfonip.com
 * 13-Aug-2010
 * Copyright 2010, Ronan Cashell
 */
 
/*!
 * jQuery symSponsorFlip Helper Scripts v1.00
 * http://symfonip.com
 * 13-Aug-2010
 * Copyright 2010, Ronan Cashell
 */
 
 // This function allows symFlowplayer to be played standalone on top of each poster image.
 (function($) {
 	$.fn.extend({
		symSponsorFlip: function(options) {
		
							var pauseOver = false;
							var settings = { autoFlip: false,
											 autoFlipDuration: 5,
											 randomFlip: false,
											 autoRevert: true,
											 uniqueID: "page0",
											 color: "#999999",
											 flipSpeed: 0.350,
											 pauseOnHover: false};

							if(typeof options == "object") {
								$.extend(settings, options);
							}
							var counter = $(this).length;
							var curFlip = null;
							var randFlip = null;
							var flipID = null;
							var intID = null;
							
							if(settings.autoFlip) {
								if(settings.randomFlip) {
									intID = setInterval(function() { 
												if(!settings.pauseOnHover || (settings.pauseOnHover && !pauseOver)) {
													if(settings.autoRevert) {
														if(curFlip != null) {
															// Only flip back if we are showing text.
															if(curFlip.data("flipped")) {
																curFlip.click();
															} else {
																curFlip = null;
															}
														}
													}
													flipID = Math.floor(Math.random()*counter);
													// Must make sure we never flip the same entry
													if(curFlip != null) {
														// If there is only 1 item then we could go into an infinite loop
														// Use count to break out nicely.
														var count = 0;
														while(count < 5 && ("symSponsorFlip" + settings.uniqueID + flipID) == curFlip.attr("id")) {
															flipID = (flipID + Math.floor(Math.random()*counter)) % counter;
															count = count + 1;
														}
													}
													curFlip = $("#symSponsorFlip" + settings.uniqueID + flipID);
													curFlip.click(); 
												}
											}, settings.autoFlipDuration*1000);
								} else {
									intID = setInterval(function() { 
												if(settings.autoRevert) {
													if(curFlip != null) {
														// Only flip back if we are showing text.
														if(curFlip.data("flipped")) {
															curFlip.click();
														} else {
															curFlip = null;
														}
													}
												}
												if(curFlip != null) {
													flipID = (flipID + 1)%counter;
												} else {
													flipID = 0;
												}
												curFlip = $("#symSponsorFlip" + settings.uniqueID + flipID);
												curFlip.data("auto", true);
												curFlip.click(); 
											}, settings.autoFlipDuration*1000);
								}
							}
							
							$(this).hover(function() {
											if(settings.pauseOnHover) {
												pauseOver = true;
											}
										},
										function() {
											if(pauseOver) {
												pauseOver = false;
											}
										});
										
							// Keeps an array of flips that we are dealing with.
							var allFlips = $(this);
							
							return this.each(function() {
								$(this).bind("click", function() {
									// we now bind to the click event
									var elem = $(this);
								
									// Then we go through each of these and flip them back.
									if(!settings.autoFlip && settings.autoRevert) {
										allFlips.each(function() {
											if($(this).data("flipped")) {
												$(this).revertFlip();
												$(this).data("flipped", false);
											}
										});
									}
									// data('flipped') is a flag we set when we flip the element:
									
									if(elem.data('flipped')) {
										// If the element has already been flipped, use the revertFlip method
										// defined by the plug-in to revert to the default state automatically:
										//elem.html(content);
										elem.revertFlip();
									
										// Unsetting the flag:
										elem.data('flipped',false)
									} else {
										// Using the flip method defined by the plugin:
									
										elem.flip({
											direction:'lr',
											speed: settings.flipSpeed*1000,
											color: settings.color,
											onBefore: function(){
												// Insert the contents of the .sponsorData div (hidden from view with display:none)
												// into the clicked .sponsorFlip div before the flipping animation starts:
												elem.html(elem.siblings('.sponsorData').html());
											},
											onEnd: function() {}
										});
									
										// Setting the flag:
										elem.data('flipped',true);
									}
								});
							});
						}
	});
})(jQuery);

jQuery.noConflict();
jQuery(document).ready(function($){
	/* The following code is executed once the DOM is loaded */
	
	// I do not recommend setting flipDuration less than 2.
					   $('#symSponsorFlippage4 .sponsorFlip').symSponsorFlip({uniqueID: "page4", autoFlip: true, randomFlip: true, autoFlipDuration: 4, flipSpeed: 0.35, color:"#003a74", autoRevert:true, pauseOnHover:false});	
});
