//Initiating jQuery to work with Prototype;
//var $ = jQuery.noConflict(); located inside jquery-1.2.min.js file

$(document).ready(function() { onDocumentLoad(); });
$(window).load(function() { onWindowLoad(); });

//Global Variables
var timeOut;
var delayTime = 10000;

/*
 * Standard page loading functions for prototype / jQuery
 */
function onDocumentLoad(){
	
}

//Stuff put into this function doesnt run until the entire document is loaded flash and all
function onWindowLoad(){

	start_slideshow(0, ($("div.showcase").size()-1), delayTime, "showcase");
	start_slideshow(-1, ($("div.homePageShowcase").size()-1), delayTime, "homePageShowcase");

	$('#leftSideLeftColumnBottom').flash({
		src: '/assets/templates/rockpaperinternet/flash/rockBackground.swf',
		width: 538,
		height: 329,
		expressInstall: true,
		wmode: "transparent"
	});

	$('a.external').attr('target','_blank');

	$('#leftSideRightColumnTopOpacity').fadeTo("fast", 0.6)
	$('#leftSideRightColumnTopOpacityHome').fadeTo("fast", 0.6)

	$('#leftSideRightColumnBottomOpacity').fadeTo("fast", 0.4)
	$('#leftSideRightColumnBottomOpacityHome').fadeTo("fast", 0.6)

	$('#rightSideLeft_TopColumnOpacity').fadeTo("fast", 0.4)
	$('#rightSideLeft_TopColumnOpacityHome').fadeTo("fast", 0.4)
	$('#rightSideRight_BottomColumnOpacity').fadeTo("fast", 0.4)
	$('#rightSideRight_BottomColumnOpacityHome').fadeTo("fast", 0.4)

	$('#contentScroll').jScrollPane({showArrows:true,scrollbarWidth:8,scrollbarMargin:0});
	
	$('#showCasePause').click(function(){
		if($(this).html()=='<img src="/assets/templates/rockpaperinternet/images/rpi-pause_button.gif" alt="Pause" />'){
			$(this).html('<img src="/assets/templates/rockpaperinternet/images/rpi-right_button.gif" alt="Play" />');
			pause_slideshow();
		} else {
			$(this).html('<img src="/assets/templates/rockpaperinternet/images/rpi-pause_button.gif" alt="Pause" />');
			restart_slideshow();
		}
	});
	
	$('#showCasePrevious').click(function(){
		previous_slideshow();
	});
	
	$('#showCaseNext').click(function(){
		next_slideshow();
	});

	/* ie6 hover fix for menu */
	$('.outerULRightMenu *').hover(function(){
		$(this).addClass("hover");
	}, function(){
		$(this).removeClass("hover");
	});

	/* Since wayfinder doesnt add current to weblinks right */
	$('.outerULRightMenu li a').each(function(){
		if(this.href==window.location.href){
			$(this).parent('li').addClass('active');
		}
	});
}

function start_slideshow(start_frame, end_frame, delay, id) {
	timeOut = setTimeout(switch_slides(start_frame,0,end_frame, delay, id), delay);
}

function pause_slideshow() {
	clearTimeout(timeOut);
}

function restart_slideshow(){
	if($('#homePageShowcaseShow').children(":visible").size()===0){
		var frame = -1;
	} else {
		var frame = parseInt($('#homePageShowcaseShow').children(":visible").attr("id").replace("homePageShowcase",""));
	}

	clearTimeout(timeOut);
	start_slideshow(frame, $(".homePageShowcase").size()-1, delayTime,  "homePageShowcase");
}

function next_slideshow(){
	if($('#homePageShowcaseShow').children(":visible").size()===0){
		var frame = -1;
	} else {
		var frame = parseInt($('#homePageShowcaseShow').children(":visible").attr("id").replace("homePageShowcase",""));
	}

	clearTimeout(timeOut);
	$('#showCasePause').html('<img src="/assets/templates/rockpaperinternet/images/rpi-right_button.gif" alt="Play" />');

	if(frame === -1){
		$('#mainContentShow').fadeOut(600,function(){
			frame = 0;
			$('#homePageShowcase' + frame).fadeIn(600);
		});
	} else {
		$('#homePageShowcase' + frame).fadeOut(600,function(){
			if(frame==($(".homePageShowcase").size()-1)){
				$('#mainContentShow').fadeIn(600);
			} else {
				frame++;
				$('#homePageShowcase' + frame).fadeIn(600);
			}
		});
	}
}

function previous_slideshow(){
	if($('#homePageShowcaseShow').children(":visible").size()===0){
		var frame = -1;
	} else {
		var frame = parseInt($('#homePageShowcaseShow').children(":visible").attr("id").replace("homePageShowcase",""));
	}

	clearTimeout(timeOut);

	if(frame === -1){
		$('#mainContentShow').fadeOut(600,function(){
			frame = ($(".homePageShowcase").size()-1);
			$('#homePageShowcase' + frame).fadeIn(600);
		});
	} else {
		$('#homePageShowcase' + frame).fadeOut(600,function(){
			if(frame==0){
				$('#mainContentShow').fadeIn(600);
			} else {
				frame--;
				$('#homePageShowcase' + frame).fadeIn(600);
			}
		});
	}
}

function switch_slides(frame, start_frame, end_frame, delay, id) {
    return (function() {
    	if(id=="homePageShowcase"&&frame==-1){
    		$('#mainContentShow').fadeOut(600);
    	}
    	$('#' + id + frame).fadeOut(600);

    	if(id=="homePageShowcase"&&frame==end_frame){
    		timeOut = setTimeout("$('#mainContentShow').fadeIn(600);", 600);
    		frame = -1;
    	} else {
    		if (frame == end_frame) { frame = start_frame; } else { frame = frame + 1; }
    		timeOut = setTimeout("$('#" + id + frame + "').fadeIn(600);", 600);
    	}
    	timeOut = setTimeout(switch_slides(frame, start_frame, end_frame, delay, id), delay + 600);
    })
}

/*sIfr doesnt like being loaded by jQuery in IE so must be loaded on own outside */

var officinaSans = {
	src: '/assets/templates/rockpaperinternet/flash/officinaSans.swf'
	,ratios: [8, 1.42, 9, 1.33, 12, 1.35, 21, 1.3, 23, 1.27, 30, 1.28, 44, 1.26, 59, 1.25, 62, 1.24, 63, 1.25, 66, 1.24, 67, 1.25, 101, 1.24, 102, 1.23, 106, 1.24, 107, 1.23, 108, 1.24, 111, 1.23, 114, 1.24, 115, 1.23, 117, 1.24, 120, 1.23, 121, 1.24, 124, 1.23, 1.24]
};

// sIFR.domains = ['novemberborn.net'] // Don't check for domains in this demo
sIFR.useStyleCheck = true;
sIFR.activate(officinaSans);

sIFR.replace(officinaSans, {
	selector: '#leftSideRightColumnTop h1, #leftSideRightColumnTopHome h1'
	,css: [
		'.sIFR-root { text-align: center; color: #000000; text-transform: uppercase; font-weight: normal; }'
	]
	,wmode: 'transparent'
});

sIFR.replace(officinaSans, {
	selector: '#rightSideRight_BottomColumnHome h2'
	,css: [
		'.sIFR-root { text-align: center; color: #FFFFFF; font-weight: normal; }'
	]
	,wmode: 'transparent'
});

sIFR.replace(officinaSans, {
	selector: '#leftSideRightColumnBottom h2'
	,css: [
		'.sIFR-root { text-align: center; color: #FFFFFF; font-weight: normal; }'
	]
	,wmode: 'transparent'
});