//Initiating jQuery to work with Prototype;
//var $j = jQuery.noConflict(); located inside jquery-1.2.min.js file

$j(document).ready(function() { onDocumentLoad(); });
$j(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, ($j("div.showcase").size()-1), delayTime, "showcase");
	start_slideshow(-1, ($j("div.homePageShowcase").size()-1), delayTime, "homePageShowcase");

    $j('#leftSideLeftColumnBottom').flash({
        src: 'assets/templates/rockpaperinternet/flash/rockBackground.swf',
        width: 538,
    	height: 329,
    	expressInstall: true,
    	wmode: "transparent"
    });

	$j('#leftSideRightColumnTopOpacity').css("opacity",".6");
	$j('#leftSideRightColumnTopOpacityHome').css("opacity",".6");

	$j('#leftSideRightColumnBottomOpacity').css("opacity",".4");
	$j('#leftSideRightColumnBottomOpacityHome').css("opacity",".6");

	$j('#rightSideLeft_TopColumnOpacity').css("opacity",".4");
	$j('#rightSideLeft_TopColumnOpacityHome').css("opacity",".4");
	$j('#rightSideRight_BottomColumnOpacity').css("opacity",".4");
	$j('#rightSideRight_BottomColumnOpacityHome').css("opacity",".4");

	$j('#contentScroll').jScrollPane({showArrows:true,scrollbarWidth:8,scrollbarMargin:0});
	
	$j('#showCasePause').click(function(){
		if($j(this).html()=='<img src="assets/templates/rockpaperinternet/images/rpi-pause_button.gif" alt="Pause" />'){
			$j(this).html('<img src="assets/templates/rockpaperinternet/images/rpi-right_button.gif" alt="Play" />');
			pause_slideshow();
		} else {
			$j(this).html('<img src="assets/templates/rockpaperinternet/images/rpi-pause_button.gif" alt="Pause" />');
			restart_slideshow();
		}
	});
	
	$j('#showCasePrevious').click(function(){
		previous_slideshow();
	});
	
	$j('#showCaseNext').click(function(){
		next_slideshow();
	});

	/* ie6 hover fix for menu */
	$j('.outerULRightMenu *').hover(function(){
		$j(this).addClass("hover");
	}, function(){
		$j(this).removeClass("hover");
	});

	/* Since wayfinder doesnt add current to weblinks right */
	$j('.outerULRightMenu li a').each(function(){
		if(this.href==window.location.href){
			$j(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($j('#homePageShowcaseShow').children(":visible").size()===0){
		var frame = -1;
	} else {
		var frame = parseInt($j('#homePageShowcaseShow').children(":visible").attr("id").replace("homePageShowcase",""));
	}

	clearTimeout(timeOut);
	start_slideshow(frame, $j(".homePageShowcase").size()-1, delayTime,  "homePageShowcase");
}

function next_slideshow(){
	if($j('#homePageShowcaseShow').children(":visible").size()===0){
		var frame = -1;
	} else {
		var frame = parseInt($j('#homePageShowcaseShow').children(":visible").attr("id").replace("homePageShowcase",""));
	}

	clearTimeout(timeOut);
	$j('#showCasePause').html('<img src="assets/templates/rockpaperinternet/images/rpi-right_button.gif" alt="Play" />');

	if(frame === -1){
		$j('#mainContentShow').fadeOut(600,function(){
			frame = 0;
			$j('#homePageShowcase' + frame).fadeIn(600);
		});
	} else {
		$j('#homePageShowcase' + frame).fadeOut(600,function(){
			if(frame==($j(".homePageShowcase").size()-1)){
				$j('#mainContentShow').fadeIn(600);
			} else {
				frame++;
				$j('#homePageShowcase' + frame).fadeIn(600);
			}
		});
	}
}

function previous_slideshow(){
	if($j('#homePageShowcaseShow').children(":visible").size()===0){
		var frame = -1;
	} else {
		var frame = parseInt($j('#homePageShowcaseShow').children(":visible").attr("id").replace("homePageShowcase",""));
	}

	clearTimeout(timeOut);

	if(frame === -1){
		$j('#mainContentShow').fadeOut(600,function(){
			frame = ($j(".homePageShowcase").size()-1);
			$j('#homePageShowcase' + frame).fadeIn(600);
		});
	} else {
		$j('#homePageShowcase' + frame).fadeOut(600,function(){
			if(frame==0){
				$j('#mainContentShow').fadeIn(600);
			} else {
				frame--;
				$j('#homePageShowcase' + frame).fadeIn(600);
			}
		});
	}
}

function switch_slides(frame, start_frame, end_frame, delay, id) {
    return (function() {
    	if(id=="homePageShowcase"&&frame==-1){
    		$j('#mainContentShow').fadeOut(600);
    	}
    	$j('#' + id + frame).fadeOut(600);

    	if(id=="homePageShowcase"&&frame==end_frame){
    		timeOut = setTimeout("$j('#mainContentShow').fadeIn(600);", 600);
    		frame = -1;
    	} else {
    		if (frame == end_frame) { frame = start_frame; } else { frame = frame + 1; }
    		timeOut = setTimeout("$j('#" + 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'
});