﻿/* ------------------------SLIDESHOW---------------------------- */
//OPACITY MODIFIED FROM http://www.brainerror.net/scripts_js_blendtrans.php
//CLASSNAME MODIFIED FROM http://www.hotscripts.com/forums/javascript/47025-document-getelementsbyclassname.html
/* ----------------------------- */

function getElementsByClassName(classname,container) {
	var objTemp = document.getElementsByTagName("*");
	var arrStories = [];
	var blnSomething = false;
	for (var i in objTemp) {
		if (new RegExp("\\b"+classname+"\\b").test(objTemp[i].className)) {
			arrStories.push(objTemp[i]);
			blnSomething = true;
		}
	}
	
	if (!(blnSomething)) {
		arrStories[0] = -1;
	}
	
	return arrStories;
}

function setOpacity(id,opacity) {
	var object = document.getElementById(id).style;
	
	object.opacity = (opacity / 100);						//SAFARI,FIREFOX,CSS3
	object.MozOpacity = (opacity / 100);					//OLD MOZILLA
	object.KhtmlOpacity = (opacity / 100);					//OLD KONQUEROR AND OLD SAFARI-WEBKIT
	object.filter = "alpha(opacity=" + opacity + ")";		//IE
}

function setFade(id,opacStart,opacEnd,millisec,hide) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;
	var i;

	//determine the direction for the blending, if start and end are the same nothing happens
	if (opacStart > opacEnd) {
		document.getElementById(id).style.zIndex = '10';
		for (i = opacStart; i >= opacEnd; i--) {
			setTimeout("setOpacity('"+id+"',"+i+")",(timer * speed));
			timer++;
		}
	} else if (opacStart < opacEnd) {
		document.getElementById(id).style.zIndex = '1000';
		for(i = opacStart; i <= opacEnd; i++) {
			setTimeout("setOpacity('"+id+"',"+i+")",(timer * speed));
			timer++;
		}
	}
}

function playSlideshow() {
	if (blnChangeImage && !blnSlideshowPause) {
		switch (conTransition) {
			case 'fade':
				setFade(arrStories[current].id,100,0,750,false);
				setFade(arrStories[next].id,0,100,750,true);
				//highlightButton(next);
				break;
		}
	
		current = next;
		next++;
		if (next == arrStories.length) { next = 0; }
	}
}

function gotoSlide(next) {
	switch (conTransition) {
		case 'fade':
			setFade(arrStories[current].id,100,0,750,false);
			setFade(arrStories[next].id,0,100,750,true);
			//highlightButton(next);
			break;
	}
	
	current = next;
	clearInterval(timer);
}

function highlightButton(num) {
	//GET BUTTONS
	var arrButtons = getElementsByClassName("button","b2bSlideshowWebPart");

	//CLEAR ALL BUTTONS
	for (i = 0;i < arrButtons.length;i++) {
		arrButtons[i].setAttribute("class", "button buttonRegular");
		arrButtons[i].setAttribute("className", "button buttonRegular");
	}

	arrButtons[num].setAttribute("class", "button buttonActive");
	arrButtons[num].setAttribute("className", "button buttonActive");
}

//--------------------------
//   PROCEDURE
//--------------------------


//SET VARIABLES
var blnSlideshowPause = false;
var arrStories = getElementsByClassName('story');

//SETUP
if (arrStories[0] != -1) {
	//START BUTTONS
	var strButtons = "";
	
	//DETERMINE TYPE OF TRANSITION
	var conTransition = "fade";
	
	//SETUP EACH CLASS
	for (i = 0;i < arrStories.length;i++) {
		//--ASSIGN IDs SEQUENTIALLY TO OBJECTS
		arrStories[i].setAttribute("id","story"+i);
		
		//--HIDE ALL
		setOpacity(arrStories[i].id,0);
		
		//--MAKE BUTTONS
		//strButtons += '<a href="#" onclick="gotoSlide('+i+');return false;" class="button buttonRegular">'+(i + 1)+'</a>';
	}
	//SHOW FIRST
	var current = 0;
	var next = 1;
	document.getElementById(arrStories[current].id).style.zIndex = '1000';
	setOpacity(arrStories[current].id,100);

	//SHOW BUTTONS
	//document.getElementById('buttons').innerHTML = strButtons;
	//highlightButton(0);
	
	//FADE ON TIMER
	if (arrStories.length > 1) {
		var timer = setInterval("playSlideshow();",6000);
	}
}


/* ------------------------SLIDER---------------------------- */
//ADAPTED FROM http://www.leigeber.com/2008/11/drop-down-menu/
blnChangeImage = true;
var menu=function(){
	var t=15,z=50,s=6,a;
	function dd(n){this.n=n; this.h=[]; this.c=[]}
	dd.prototype.init=function(p,c){
		a=c; var w=document.getElementById(p), s=w.getElementsByTagName('ul'), l=s.length, i=0;
		for(i;i<l;i++){
			var h=s[i].parentNode; this.h[i]=h; this.c[i]=s[i];
			h.onmouseover=new Function(this.n+'.st('+i+',true)');
			h.onmouseout=new Function(this.n+'.st('+i+')');
		}
	}
	dd.prototype.st=function(x,f){
		var c=this.c[x], h=this.h[x], p=h.getElementsByTagName('a')[0];
		clearInterval(c.t); c.style.overflow='hidden';
		if(f){
			p.className+=' '+a;
			if(!c.mh){c.style.display='block'; c.style.height=''; c.mh=c.offsetHeight; c.style.height=0}
			if(c.mh==c.offsetHeight){c.style.overflow='visible'}
			else{c.style.zIndex=z; z++; c.t=setInterval(function(){sl(c,1)},t)}
		}else{p.className=p.className.replace(a,''); c.t=setInterval(function(){sl(c,-1)},t)}
		
		//TALK TO BUTTONS
		if (blnChangeImage) {
			blnChangeImage = false;
		} else {
			blnChangeImage = true;
		}
	}
	function sl(c,f){
		var h=c.offsetHeight;
		if((h<=0&&f!=1)||(h>=c.mh&&f==1)){
			if(f==1){c.style.filter=''; c.style.opacity=1; c.style.overflow='visible'}
			clearInterval(c.t); return
		}
		var d=(f==1)?Math.ceil((c.mh-h)/s):Math.ceil(h/s), o=h/c.mh;
		c.style.opacity=o; c.style.filter='alpha(opacity='+(o*100)+')';
		c.style.height=h+(d*f)+'px'
	}
	return{dd:dd}
}();



/* ------------------------HOMEPAGE : LARGE BUTTONS---------------------------- */
function enter(elementname) {
	document.getElementById(elementname).style.backgroundPosition="top";
}
function exit(elementname) {
	document.getElementById(elementname).style.backgroundPosition="bottom";
}



/* ------------------------JQUERY---------------------------- */
$(document).ready(function(){	
	//------------------------------<FONT> TAGS
	//http://stackoverflow.com/questions/278709/allowing-the-font-tag-to-override-css
	$('font[size]').each(function() {
        var fontSize = this.size;
        if (fontSize == 1) {
            $(this).css("font-size", 10);
        } else if (fontSize == 2) {
            $(this).css("font-size", 11);
        } else if (fontSize == 3) {
            $(this).css("font-size", 13);
        } else if (fontSize == 4) {
            $(this).css("font-size", 16);
        } else if (fontSize == 5) {
            $(this).css("font-size", 24);
        } else if (fontSize == 6) {
            $(this).css("font-size", 28);
        }
    });
    $('font[face]').each(function() {
        $(this).css('font-family', this.face);
    });
    $('font[color]').each(function() {
        $(this).css('color', this.color);
    });

});


/* ------------------------WEBPART DRAG FIX---------------------------- */
//http://neilmosafi.blogspot.com/2007/11/sharepoint-dragging-webparts-causes.html
function MSOLayout_GetRealOffset(StartingObject, OffsetType, EndParent)
{
    var realValue=0;
    if (!EndParent) EndParent=document.body;
    for (var currentObject=StartingObject; currentObject && currentObject !=EndParent && currentObject != document.body; currentObject=currentObject.offsetParent)
    {
        var offset = eval('currentObject.offset'+OffsetType);
        if (offset) realValue+=offset;
    }
    return realValue;
}

