// site specific javascript
/*
if(!jqN.browser.safari)
{
	jqN(document).ready(function(){
		behavior_binder();
	});
}
else
{
	jqN(window).load(function(){
		behavior_binder();
	});
}
*/

/*
Binds behaviors
** can be re-run whenever there are DOM changes
*/
/*
function behavior_binder(){

	jqN('.module_title span.main').drawFont();
	jqN('.module_title span.sub').drawFont();
	jqN('.module_title span.season_link a').drawFont();
	
}
*/

function refreshAds() {
	// insert iframes into both, load in new ad code.
	if (jqN && js_site && js_section) {

		// 728x90
		if (document.getElementById('global_header_ad')) {
			var container_728x90 = document.getElementById('global_header_ad');
			jqN(container_728x90).children().remove();
			jqN(container_728x90).append('<iframe width="728" height="90" scrolling="no" frameborder="0" src="/_utils/ads/controller.php?size=728x90&site=' + js_site + '&section=' + js_section + '"></iframe>');			
		}

		if (document.getElementById('ad300x250')) {
			var container_300x250 = document.getElementById('ad300x250');
			jqN(container_300x250).children().remove();
			jqN(container_300x250).append('<iframe width="300" height="250" scrolling="no" frameborder="0" src="/_utils/ads/controller.php?size=300x250&site=' + js_site + '&section=' + js_section + '"></iframe>');
		}		

	}

}


function loadVideoAutoplay(element) {
	if (DetectFlashVer(9,0,0)) {

		jqN(element).children('.video_full').addClass('autoplay');

		jqN(element).find('.video_full > .thumb').append('<iframe frameborder="0" scrolling="no" width="300" height="193" class="video_iframe"></iframe>');

		jqN(element).find('.video_iframe:first').attr('src', '/_utils/video/thP_video_controller.php?sect='+js_site+'&/video/small/autoplay/_vid'+autoplay_videoid); // does this fix the firefox bug w/ iframes? /_utils/video/thP_video_controller.php?_vid1375154

	}
}

function killVideoAutoplay(element) {
	jqN(element).find('.video_iframe').hide().attr('src', '').remove();
}

/* general fancy-pants classes */

/* 990x90 fancy extended transitions */
function banner990x90interval() {
	var self;
	self=this;
	this.currentImage = 0;
	this.images = jqN('#showsite_990x90_banner .logo');		
	jqN(this.images[0]).show();
	this.binterval = window.setInterval(function() { self.switchLogo(); }, '3000');
}

banner990x90interval.prototype.switchLogo = function() {
	var self;
	self=this;		
	var nextImage = '';		
	if (this.currentImage == (this.images.length-1)) {
		nextImage = 0;
	} else {
		nextImage = this.currentImage+1;
	}
	jqN(this.images[this.currentImage]).fadeOut();
	jqN(this.images[nextImage]).fadeIn();
	this.currentImage = nextImage;		
}

/* syfy feature rotator re-write! */
function syfyFeatureRotator(containerName, items, options) {
	var self;
	self=this;
	
	this.containerElement = jqN(document.getElementById(containerName));
	this.featureItems = items;
	this.currentIndex = null;

	if (options.totalItems) {
		this.totalItems = options.totalItems;
	} else {
		this.totalItems = this.featureItems.length;
	}
	
	if(options.trianglePosition) {
	   this.trianglePosition = options.trianglePosition;
	} else {
	   this.trianglePosition = 'top';
	}
	
	if(options.fadeThumbnail) {
	   this.fadeThumbnail = options.fadeThumbnail;
	} else {
	   this.fadeThumbnail = false;
	}
	
	if(options.imageWidth) {
	   this.imageWidth = options.imageWidth;
	} else {
	   this.imageWidth = 685;
	}
	
	if(options.imageHeight) {
	   this.imageHeight = options.imageHeight;
	} else {
	   this.imageHeight = 385;
	}
	
	if(options.homepage) {
	   this.homepage = options.homepage;
	} else {
	   this.homepage = false;
	}
	
	if(options.headText) {
	   this.headText = options.headText;
	} else {
	   this.headText = false;
	}

	this.rotateIntervalMS = 6000;
	this.rotateInterval = null;

	// build the items
	this.containerElement.append('<div id="main_feature" class="shadow"></div>');
	this.containerElement.append('<div id="feature_buttons"></div>');
	for (var i=0;i<this.totalItems;i++) {
		var item = this.featureItems[i];
		// barf, template this
		this.buildFeature(item,i);
		this.buildButton(item,i);
	}

	this.moveTo(0);

	jqN(document).ready(function() {
		self.rotateInterval = window.setInterval(function() { self.moveTo(self.nextIndex()); }, self.rotateIntervalMS);		
	});
	
}

syfyFeatureRotator.prototype.darkenColor = function(color, percent) {
	var rgb = { r: parseInt(color.substr(0,2), 16), g: parseInt(color.substr(2,2), 16), b: parseInt(color.substr(4,2), 16) }
	var newcolor = '';
	for (c in rgb) {
		var wonk = (rgb[c] - Math.round((rgb[c]/100)*percent)).toString(16);
		if (wonk.length < 2) { wonk = '0'+wonk; }
		newcolor += wonk;
	}
	return(newcolor);
}

syfyFeatureRotator.prototype.buildFeature = function(item,i) {
	// feature
	var htmlstring = '<a href="'+item.itemURL+'" id="largeimage_feature'+i+'" class="feature_image_container" style="display: none;">';
	htmlstring += '<img class="feature_image" src="'+item.featureImage+'" width="'+this.imageWidth+'" height="'+this.imageHeight+'" alt="'+item.featureTitle+'">';
	htmlstring += '<div class="topfeature_gradient"></div>';
	htmlstring += '<div class="topfeature_text shadow"';
	if(this.homepage) {
	   htmlstring += ' style="background: #'+item.buttonColor+'; background: -moz-linear-gradient(top, #'+item.buttonColor+' 0%, #'+this.darkenColor(item.buttonColor, 25)+' 100%); background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#'+item.buttonColor+'), color-stop(100%,#'+this.darkenColor(item.buttonColor, 25)+')); filter: progid:DXImageTransform.Microsoft.gradient( startColorStr=#FF'+item.buttonColor+', endColorStr=#FF'+this.darkenColor(item.buttonColor, 25)+',GradientType=0 );"';
	}
	
	htmlstring += '>';
	if (item.featureSub) { htmlstring += '<h2 class="drawfont">'+item.featureSub+'</h2>'; }
	if (this.homepage) { htmlstring += '<div class="topfeature-inner-text shadow">'; }
	if (item.featureTitle) { htmlstring += '<h1 class="drawfont">'+item.featureTitle+'</h1>'; }
	if (this.homepage && item.buttonSub) { htmlstring += '<h4 class="drawfont">'+item.buttonSub+'</h4>'; }
	if (item.featureDescription) { htmlstring += '<p class="drawfont">'+item.featureDescription+'</p>'; }
	if (this.homepage) { htmlstring += '</div>'; }
	htmlstring += '</div>';
	htmlstring += '</a>';
	
	if (item.featureCTA) { htmlstring += '<a class="drawfont feature-cta" href="'+item.featureURL+'"><span class="grey-arrow-right-small"></span>'+item.featureCTA+'</a>'; }

	var element = jqN(htmlstring);
	element.appendTo('#main_feature');
	this.featureItems[i].featureElement = element;
    
    Cufon.replace('.drawfont');
}

syfyFeatureRotator.prototype.buildButton = function(item,i) {
	var self;
	self=this;
	
	htmlstring = '';
	
	if(this.headText && i == 0) {
	   htmlstring += '<h3 class="drawfont shadow module_title">' + this.headText + '</h3>';
 	}
	
	// button
	htmlstring += '<a href="'+item.itemURL+'" id="feature'+i+'" class="feature_button">';

	htmlstring += '<div class="button_hover shadow" style="display: none; background: #'+item.buttonColor+'; background: -moz-linear-gradient(top, #'+item.buttonColor+' 0%, #'+this.darkenColor(item.buttonColor, 25)+' 100%); background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#'+item.buttonColor+'), color-stop(100%,#'+this.darkenColor(item.buttonColor, 25)+')); filter: progid:DXImageTransform.Microsoft.gradient( startColorStr=#FF'+item.buttonColor+', endColorStr=#FF'+this.darkenColor(item.buttonColor, 25)+',GradientType=0 );">';




	htmlstring += '<div class="triangle" style="display: inline-block; '+this.cssChangedBorderPosition()+': #'+this.getTriangleColor(item.buttonColor)+';"></div>';
	if (item.buttonTitle) { htmlstring += '<h3 class="drawfont">'+item.buttonTitle+'</h3>'; }
	if (item.buttonSub) { htmlstring += '<h4 class="drawfont">'+item.buttonSub+'</h4>'; }
	htmlstring += '</div>';

	htmlstring += '<div class="button">';
	if (item.buttonTitle) { htmlstring += '<h3 class="drawfont">'+item.buttonTitle+'</h3>'; } 
	if (item.buttonSub) { htmlstring += '<h4 class="drawfont">'+item.buttonSub+'</h4>'; }
	htmlstring += '</div>';
	
	htmlstring += '<div class="thumbnail shadow"><img src="'+item.buttonImage+'" width="64" height="64"></div>';
	
	htmlstring += '</a>';

	var element = jqN(htmlstring);
	element.appendTo('#feature_buttons');
	this.featureItems[i].buttonElement = element;

	// bind
	element.mouseover(function() {
		if (self.rotateInterval) {
			window.clearInterval(self.rotateInterval);
		}
		if (!jqN(this).hasClass('current')) {
			self.moveTo(i);
		}
	});

	element.mouseout(function() {
		self.rotateInterval = window.setInterval(function() { self.moveTo(self.nextIndex()); }, self.rotateIntervalMS);
	});

}

syfyFeatureRotator.prototype.moveTo = function(index) {

	if (this.currentIndex != null) {
		var currentItem = this.featureItems[this.currentIndex];
		currentItem.buttonElement.removeClass('current');
		currentItem.featureElement.stop(true, true).fadeOut();
		currentItem.buttonElement.children('.button_hover').stop(true, true).fadeOut();
		currentItem.buttonElement.children('.button').stop(true, true).fadeIn();
		currentItem.buttonElement.children('.thumbnail').addClass('shadow');
		if(this.fadeThumbnail) {
	        currentItem.buttonElement.children('.button').animate({'paddingLeft': '64px', 'paddingRight': '0px'});
	        currentItem.buttonElement.children('.button_hover').animate({'paddingLeft': '64px', 'paddingRight': '0px'});
	        currentItem.buttonElement.children('.thumbnail').fadeIn();
	    }		
	}

	var targetItem = this.featureItems[index];
	targetItem.buttonElement.addClass('current');
	targetItem.featureElement.stop(true, true).fadeIn();
	targetItem.buttonElement.children('.button_hover').stop(true, true).fadeIn();
	if (this.currentIndex == null) { targetItem.buttonElement.children('.button').hide(); } // WHAT THE FUCK?
	targetItem.buttonElement.children('.button').fadeOut(); /* why isn't this firing initially */
	targetItem.buttonElement.children('.thumbnail').removeClass('shadow');
	
	if(this.fadeThumbnail) {
	   targetItem.buttonElement.children('.thumbnail').fadeOut();
	   targetItem.buttonElement.children('.button').animate({'paddingLeft': '0px', 'paddingRight': '64px'});
	   targetItem.buttonElement.children('.button_hover').animate({'paddingLeft': '0px', 'paddingRight': '64px'});
	}
	
	/*if(this.homepage) {
	   targetItem.featureElement.children('.topfeature_text').css()
	}*/

	this.currentIndex = index;

}

syfyFeatureRotator.prototype.nextIndex = function() {
	if (this.currentIndex == (this.totalItems-1)) {
		return(0);
	} else {
		return(this.currentIndex+1);
	}
}

syfyFeatureRotator.prototype.cssChangedBorderPosition = function() {
    switch(this.trianglePosition) {
        case 'top':     return 'border-bottom-color';
        case 'right':   return 'border-left-color';
        case 'bottom':  return 'border-top-color';
        case 'left':    return 'border-right-color';
    }
}
syfyFeatureRotator.prototype.getTriangleColor = function (buttonColor) {
    if(this.homepage)
        return this.darkenColor(buttonColor, 13);
    else
        return buttonColor;
}


/* preliminary takeover code */
function syfyTakeover(element, target) {
	var self=this;
	this.element = jqN(element);
	this.targetURL = target;
	this.resize();
	jqN(window).resize(function() { self.resize(); });
	jqN(document).ready(function() {
		self.element.append('<iframe id="takeover_iframe" src="'+self.targetURL+'" frameborder="0" border="0"></iframe>');
	});
}

syfyTakeover.prototype.close = function() {
	this.element.attr('src', null);
	this.element.hide();
	jqN('#syfy_site_wrapper').show();
}

syfyTakeover.prototype.resize = function() {
	this.element.css('width', jqN(window).width()+'px');
	this.element.css('height', jqN(window).height()+'px');		
}

syfyTakeover.prototype.openLink = function(link) {
	window.location = link;
}

