$(document).ready(function() {
    
    $('.faq .toggle a').click(function() {
        if(this.title == "More") {
            this.title = "Close";
            this.innerHTML = "[Close]";
            this.parentNode.parentNode.className = "faqFull";
        }
        else {
            this.title = "More";
            this.innerHTML = "[More]";
            this.parentNode.parentNode.className = "faq";
        }
        return false;
    });
    
    if(document.getElementById('slideshowBank')) {
	    init_slideShow();
	}
	
	if(document.getElementById('ExpandableContainer')) {
	    expand_init();
	}
	
	if(document.getElementById('filmstrip')) {
		filmstrip_init();
	}
	
	if($.browser.msie && parseInt($.browser.version,10) < 8) {
	 
	    $('.ProjectItem:last img').hover(
	        function() {
	            this.className = "hoverlast";
	        },
	        function() {
	            this.className = "";
	        }
	    );  
	}
	
	if($.browser.msie && parseInt($.browser.version,10) < 7) {
	        $('.ProjectItem img').hover(
	            function() {
	                this.className = "hover";
	            },
	            function() {
	                this.className = "";
	            }
	        );
	        
	        $('.ProjectItem:last img').hover(
	            function() {
	                this.className = "hoverlast6";
	            },
	            function() {
	                this.className = "";
	            }
	        );
	    }
	
	else {
	        $('.ProjectItem:last img').addClass('hoverlast');
        }
	
	$('.ProjectItem:last').addClass('lastproject');
	
	if(document.getElementById('homeFeature')) {
	    homeSwap_init();
	}
	
	if(document.getElementById('projectPager')){
	    var projectPager = document.getElementById('projectPager');
	    if(projectPager.innerHTML == '' || projectPager.innerHTML == null){
	        projectPager.style.background = 'none';
	    } 
	    else{
	        projectPager.style.visibility = 'visible';
	    }
	}
	
});

function entity_hoverOn(entity) {
	document.getElementById('entityLabel_'+entity).style.display = "block";
}
function entity_hoverOff(entity) {
	document.getElementById('entityLabel_'+entity).style.display = "none";
}


var pm_currentID = "";
var pm_closeTime;

function pm_open(object) {
	clearTimeout(pm_closeTime);
	if(document.getElementById(pm_currentID)) {
		var oldObject = document.getElementById(pm_currentID);
		oldObject.getElementsByTagName('span')[0].className = "";
		if(oldObject.getElementsByTagName('ul').length > 0) {
			oldObject.getElementsByTagName('ul')[0].className = "";
		}
		pm_currentID = "";
	}
	
	object.getElementsByTagName('span')[0].className = "on";
	if(object.getElementsByTagName('ul').length > 0) {
		
		var ul = object.getElementsByTagName('ul')[0];
		ul.style.left = object.offsetLeft + 6 + "px";
		ul.style.top = object.offsetTop + 30 + "px";
		ul.className = "on";
		
	}
	pm_currentID = object.id;
}

function pm_close() {
	pm_closeTime = setTimeout('pm_closeNow()',150);
}

function pm_closeNow() {
	if(document.getElementById(pm_currentID)) {
		object = document.getElementById(pm_currentID);
		object.getElementsByTagName('span')[0].className = "";
		if(object.getElementsByTagName('ul').length > 0) {
			object.getElementsByTagName('ul')[0].className = "";
		}
	}
	
	pm_currentID = "";
}

function customValidationStyle(oElem, isValid) {
    if ("INPUT" == oElem.tagName || "SELECT" == oElem.tagName || "TEXTAREA" == oElem.tagName) {
        if (isValid) {
            oElem.style.background = "none";
        }
        else {
            oElem.style.background = "#EACCBA";
        }
    }
    else {
        design_validationStyle(oElem, isValid);
    }
}


var SlideShowSpeed = 3000;

var SScount = 1;
var SSmax = 0;
var SSinterval;
var SSopacity = 0;
var SSeffect;

function init_slideShow() {
	var bankImages = document.getElementById('slideshowBank').getElementsByTagName('img');
	SSmax = bankImages.length;
	if(SSmax > 1) {
		document.getElementById('slideshow').getElementsByTagName('img')[0].id = "ssM";
		for(var x = 0; x < SSmax; x++) {
			bankImages[x].id = "ssM" + (x+1);
		}
		
		var fadeImage = document.createElement('img');
		fadeImage.id = "fadeImage";
		document.getElementById('slideshow').appendChild(fadeImage);
	
		fadeImage.style.opacity = "0";
		fadeImage.style.filter = "alpha(Opacity=0)";
		
		fade_slideShow();
	}
}

function fade_slideShow() {
		
	var fadeImage = document.getElementById('fadeImage');
	var newImage = document.getElementById('ssM'+SScount);
	fadeImage.src = newImage.src;
	fadeImage.alt = newImage.alt;

	SSeffect = setInterval('fadeIn_slideShow()', 20);
}

function fadeIn_slideShow() {
	SSopacity = SSopacity + 2;
	object = document.getElementById('fadeImage');
	if(SSopacity < 100) {
		object.style.opacity = SSopacity * .01;
		object.style.filter = "alpha(Opacity="+SSopacity+")";
	}
	else {
		clearInterval(SSeffect);
		object.style.opacity = "1";
		object.style.filter = "alpha(Opacity=100)";
		
	    SScount++;
	    if (SScount > SSmax)
			SScount = 1;
		if (SScount < 1)
			SScount = SSmax;
			
		var image = document.getElementById('ssM');
		image.src = object.src;
		image.alt = object.alt;
		
		SSopacity = 0;
		object.style.opacity = "0";
		object.style.filter = "alpha(Opacity=0)";
		
		SSinterval = setTimeout('fade_slideShow()', SlideShowSpeed);
	}
}


function expand_init() {
    $('#ExpandableContainer .ExpandButton').click(function() {
        if(this.parentNode.className == "Closed") {
            this.parentNode.className = "Open";
            this.innerHTML = "[Close]";
        }
        else {
            this.parentNode.className = "Closed";
            this.innerHTML = "[More]";
        }
    });
}

var filmstrip_num = 1;
var filmstrip_count = 0;
var filmstrip_src = "";

function filmstrip_init() {
	document.getElementById('filmstrip').scrollTop = 0;
	var frames = $('#film .frame');
	filmstrip_count = frames.length;
	
	if(filmstrip_count > 3) {
		$('#filmstrip_up').addClass('disabled');
	}
	else {
		$('#filmstrip_up').addClass('disabled');
		$('#filmstrip_down').addClass('disabled');
	}
	
	for(var x=0;x<filmstrip_count;x++) {
		frames[x].id = 'film-frame'+(x+1);
	}
	
    filmstrip_src = document.getElementById('primaryImage').src;
	$('#film .frame').hover(
	    function() {
	        document.getElementById('primaryImage').src = this.getElementsByTagName('img')[0].src;
	    },
	    function() {
	        document.getElementById('primaryImage').src = filmstrip_src;
	    }
	);
}

function filmstrip_down() {
	filmstrip_num++;
	if(filmstrip_num > filmstrip_count - 2) {
		filmstrip_num = filmstrip_count - 2;
	}
	else {
		filmstrip_move(filmstrip_num);
	}
}
function filmstrip_up() {
	filmstrip_num--;
	if(filmstrip_num < 1) {
		filmstrip_num = 1;
	}
	else {
		filmstrip_move(filmstrip_num);
	}
}

function filmstrip_buttons() {
	if(filmstrip_num >= filmstrip_count - 2) {
		$('#filmstrip_down').addClass('disabled');
		$('#filmstrip_up').removeClass('disabled');
	}
	else if(filmstrip_num <= 1) {
		$('#filmstrip_up').addClass('disabled');
		$('#filmstrip_down').removeClass('disabled');
	}
	else {
		$('#filmstrip_up').removeClass('disabled');
		$('#filmstrip_down').removeClass('disabled');
	}
}

function filmstrip_move(num) {

	filmstrip_buttons();
	var theScroll = document.getElementById('filmstrip');
	var position = findElementPos(document.getElementById("film-frame" + num));
	var offsetPos = findElementPos(document.getElementById('film-frame1'));
	position[1] = position[1] - offsetPos[1];
	filmstrip_start(theScroll, theScroll.scrollTop, position[1]);
}

var filmstrip_anim = {time:0, begin:0, change:0.0, duration:0.0, element:null, timer:null};

function filmstrip_start(elem, start, end)
{

	if (filmstrip_anim.timer != null) {
		clearInterval(filmstrip_anim.timer);
		filmstrip_anim.timer = null;
	}
	filmstrip_anim.time = 0;
	filmstrip_anim.begin = start;
	filmstrip_anim.change = end - start;
	filmstrip_anim.duration = 50;
	filmstrip_anim.element = elem;
	
	filmstrip_anim.timer = setInterval("filmstrip_scroll();", 15);

}
function filmstrip_scroll()
{
	if (filmstrip_anim.time > filmstrip_anim.duration) {
		clearInterval(filmstrip_anim.timer);
		filmstrip_anim.timer = null;
	}
	else {
		move = sineInOut(filmstrip_anim.time, filmstrip_anim.begin, filmstrip_anim.change, filmstrip_anim.duration);
		filmstrip_anim.element.scrollTop = move;
		filmstrip_anim.time++;
	}
}

function findElementPos(elemFind)
{
	var elemX = 0;
	var elemY = 0;
	do {
		elemX += elemFind.offsetLeft;
		elemY += elemFind.offsetTop;
	} while ( elemFind = elemFind.offsetParent )
	return Array(elemX, elemY);
}

function sineInOut(t, b, c, d)
{
	return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
}

window.onload = function() {
 if(document.getElementById('banner')) {
  home_init();
 }
}

function home_init() {
    document.getElementById('banner').innerHTML = renderFlashHome('/rb_FlashHome.ashx');
    // IMPORTANT .NET FIX FOR FORM TAGS IN IE6
    //window['FlashHome'] = document.forms[0]['FlashDivision'];
}

var homeSwap = "";
function homeSwap_init() {
    homeSwap = $('#homeFeature .image img:first')[0].src;
    $('#homeFeature .thumb img').hover(
        function() {
            $('#homeFeature .image img:first')[0].src = this.src;
        },
        function() {
            $('#homeFeature .image img:first')[0].src = homeSwap;
        }
    );
}

function emailWindow(location) {
    var newWindow;
    if(location) {
        newWindow = window.open('/rb_emailPopup.aspx?url='+location, 'popForm', 'toolbar=0,sizable=0,width=455,height=660');
    }
    else {
        newWindow = window.open('/rb_emailPopup.aspx', 'popForm', 'toolbar=0,sizable=0,width=455,height=660');
    }
    newWindow.focus();
}