/************************************************************\
| Javascript functions for sugarcide.com       
| by Christine Ozuna
| www.sugarcide.com
|
| CODE SECTIONS:
| 1- Global declarations 
| 2- Design Section Nav functions
| 3- Misc. functions
|
\ ***********************************************************/
/************************************************************
* 1- Global declarations
*************************************************************/
var newLoc,downCounter,upCounter;
var	queryArgs = new Object();
var	cookieArgs = new Object();
var	linkCounter = 0;
var	linkArray = new Array("colorvision","epost-it","urbs-index","martinis","egarage","petroleum","honk-kong","3dflatxp","sonicPyramid","warehouse","ei-index","ei", "newlife", "newproto4" , "datafactory");
var refSrc = null; // value for referring file to uncut section
var url = null;
var homeLoad = 0;
var pflLoad = 0;
var dsgnLoad = 0;
/***************************************************************
* 2- Design section specific functions
****************************************************************/
//function in sug.js improved
function setRefSrc(){//onload call from index.htm to store + keep track of ref. file value
	parseQueryStr();
	refSrc = queryArgs.ref;
}
function loadFrames(thisLoc){// dynamically builds the frameset to load correct content for 'frames[1]' 
	var urlStr, urlStrLength, thisUrl, strStart, strEnd, strDyn,thisParam;	
	// HERE ADD CONDITIONAL IF ARG[0] THEN PASS IT ELSE NOT
	parseQueryStr(thisLoc);
	thisUrl = queryArgs.url;
	refSrc = queryArgs.ref;
	newUrl = "'" + thisUrl + ".htm'";
	strStart = "<html><frameset rows='20,380,20'  framespacing=0 border=0 frameborder=0 resize=no><frame src='uncut_top.htm' scrolling=no resize=no framespacing=0 border=0 frameborder=0>";
	strEnd = "<frame src='uncut_bot.htm' scrolling=no resize=no framespacing=0 border=0 frameborder=0></frameset></html>";
	strDyn = "<frame src=" + newUrl + " scrolling=auto resize=no>";
	//alert(strStart + strDyn + strEnd);
	thisWin.document.write(strStart + strDyn + strEnd);	//fixed target (thisWin), should add code to allow writing to any target
	for (i = 0; i < linkArray.length; i++){                
  		if (linkArray[i] == thisUrl){
			linkCounter = i;
		}
	}
}
function parseQueryStr(winLoc){//generic function to parse the url query string, argument required [window location]
	var queryStr,pQueryStr,queryPairs,pairName,pairValue;	
	if(arguments.length > 0){// required argument to allow parsing querystring from any location (re: framesets, popups)
		queryStr = eval(winLoc + ".location.search");
	}
	else{alert("Missing argument for parseQueryStr()!");}
	queryStr = unescape(queryStr);
	queryStrLength = queryStr.length;	
	pQueryStr = queryStr.substr(1,queryStrLength);
	if (queryStr.length > 0){// if querystring is empty returns false
		if(pQueryStr.indexOf("&")>-1){ // if query string more than one pair, stores all pairs in returned object
			var queryPairs = pQueryStr.split("&");
			for(i = 0; i < queryPairs.length; i++){
				pairIndex = queryPairs[i].indexOf("=");
				if (pairIndex == -1 )continue;
				pairName = queryPairs[i].substring(0,pairIndex);
				pairValue = queryPairs[i].substring(pairIndex+1);
				queryArgs[pairName] = pairValue;					
			}
			return queryArgs;
		}
		else// if querystring contains only one pair, returns pair
		{
			singlePairIndex= pQueryStr.substring(pQueryStr.indexOf("=")+1,pQueryStr.length);
			return singlePairIndex;
		}
	}
	else{return false;}		
}
function loadUrl(){// necessary to synchronize the navigation between the index list and the bottom navigation buttons
	var urlStr;
	if(arguments.length > 0){
		urlStr = arguments[0] + "&ref=" + refSrc;
		thisWin.location.href = urlStr;
	}
}
function goBack(){// goes back to original referring file as either works.htm or portfolio section
	window.top.location.href = eval('"' + refSrc + '.htm"');
}
function loadIndex(){// load index file for UNCUT section + carries ref.file value over
	if (top.frames[1].uncutIndexFlag == true){
	}
	else
	{
		indexUrl = 'index.htm?' + "&ref=" + refSrc;
		thisWin.location.href= indexUrl;
	}
}
function move2Previous(){// navigates array of links backward, also carries url (needed for reload) + ref. file values over
	if (thisWin.top.frames[1].uncutIndexFlag != true){//starting point
		downCounter = linkCounter + 1;
		newLoc = linkArray[downCounter];
		newLoc = '?url=' + newLoc + "&ref=" + refSrc;
		if(downCounter >= 1 && downCounter <= eval(linkArray.length - 1)){
			thisWin.location.href= "uncut-f.htm" + newLoc;
			linkCounter = downCounter;
		}
		else{
			loadIndex();	
		}
	}
}
function move2Next(){// navigates array of links upward, also carries url (needed for reload) + ref. file values over
	if (thisWin.top.frames[1].uncutIndexFlag == true){//starting point
		loadUrl('uncut-f.htm?url=datafactory');
	}
	else
	{
		upCounter = linkCounter - 1;
		newLoc = linkArray[upCounter];
		newLoc = '?url=' + newLoc + "&ref=" + refSrc;
		if(upCounter >= 0 ){
			thisWin.location.href= "uncut-f.htm" + newLoc;
			linkCounter = upCounter;
		}
		else{
			loadIndex();
		}
	}
}

/************************************************************
* 1- Sugarcide global functions
*************************************************************/
function test(s){
	alert(s);
}
thisWin = "";
function WinPos(name,x,y){
	this.name = name;
	this.x = x;
	this.y = y;
}
webWinXY = new WinPos("","350","150");
icoWinXY =  new WinPos("","350","150");
icoWinTbXY =  new WinPos("","350","150");
uiWinXY = new WinPos("","350","150");
dsgnWinXY =  new WinPos("","350","130");
resWinXY = new WinPos("","350","150");
animWinXY = new WinPos("","150","240");
demoLgXY = new WinPos("","0","0");

browserType=navigator.appName.charAt(0)+navigator.appVersion.charAt(0);
function launchWindow(url,name,w,h,x,y,menu,scroll,status,resize) {
	var op=navigator.userAgent.toLowerCase().indexOf('opera')!=-1;
	thisWinXY = eval(name + "XY");
	x = thisWinXY.x;
	y = thisWinXY.y;
	if (url != "none") {
	locX = (screen.width-w)/2;
	locY = (screen.height-h)/2;
	if (op){
		locX = (screen.width-w)/2;
		locY = (window.innerHeight-h)/2;
	}	
	//RECALCULATE CENTERED POSITIONING WITH X AND Y 10feb03
		thisWin =
window.open(url,name,'width='+w+',height='+h+',left='+locX+',top='+locY+',screenX='+locX+',screenY='+locY+',menubar='+menu+',scrollbars='+scroll+',status='+status+',resizable='+resize+'');
		//thisWin.moveTo(locX,locY); 
		setTimeout("thisWin.focus();",200);
	} 
}
var x = 0;
var dest = 0;
var distance = 0;
var step = 0;
var target = 0;
function scrollWin(target){
	step = 1;
	dest = target;
	if (x < dest){
		while (x < dest){
			step += (step / 50);
			x += step;
			window.top.frames[1].frames[1].scroll(x,0);		
		} 
		window.top.frames[1].frames[1].scroll(dest,0);
		x = dest;
	}
    if (x > dest){
		while (x > dest){
			step += (step / 50);
            if(x >= (0+step)){
				x -= step; 
				top.frames[1].frames[1].scroll(x,0);
            }
			else{
				break;
			}
		} 
		if(dest >= 0){	
			top.frames[1].frames[1].scroll(dest,0);
		}
		x = dest;
	}
	if (x<1) {x=1}
	if (x>2000) {x=2000}	
}
function checkFrame(thisFrame,thisFlag,thisFile,frameIndex){
	if (eval("window.top.frames[" + frameIndex + "]." + thisFlag + "Flag") == true){
		window.top.frames[frameIndex].location.href="index.htm";
	}
}
function loadAbout(){
	window.top.location.href ="indexf2.htm";
	window.top.frames[0].location.href = "about.htm";
	window.top.frames[1].location.href = "blank3.htm";
}
function MM_findObj(n, d) { //v3.0 Macromedia Fireworks function
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}
function MM_swapImage() { // v3.0 Macromedia Fireworks function - modified by cfo so function can be called from any frameset
//alert(arguments[0]);
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i],a[i+1]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
var imgDown = null;
function MM_swapImgRestore() { //v3.0 Macromedia Fireworks function - modified by cfo so down img does not swap/restore
	//alert(imgDown);
  var i,x,a=document.MM_sr;for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++)if(x.name != imgDown){x.src=x.oSrc;}
}
function preload() {//Note: arguments[0] must be the path (prefix), remaining arguments are images name 
  if (!document.images) return;
  var ar = new Array();
  path = arguments[0];
  for (var i = 1; i < arguments.length; i++) {
    ar[i] = new Image();
    ar[i].src = path  + arguments[i];
	window.status = "Loading " + arguments[i]; 
 }
 window.status = "";
}
var topBg = "images/bg-top_1.jpg";
function setBt(){
	var bg = topBg;
	var bgVal = bg.substring(bg.length-5,bg.length-5+1);
	bt = "bt" + bgVal;
	btx = "'" + "images/pfl_bgp" + bgVal + "_h.gif" + "'";
	MM_swapImage(bt,window.top.frames[1].frames[0].document,eval(btx),1);
	fixImg(bt);
}
function loadBg(st){window.top.topBg = st;saveCookie('sgBgImg',topBg,360);window.top.frames[1].frames[0].location.href = "pfl_top.htm";}
function fixImg(thisImg){imgDown = thisImg;}
function wMax(){window.top.moveTo(0,0);window.top.resizeTo(screen.width,screen.height-10);}
function wMin(x,y,w,h){window.top.moveTo(x,y);window.top.resizeTo(w,h);}
function gKey(){
	key=event.keyCode;	
	/*ctrlK = eval(eOrig+'event.ctrlKey');
	shiftK = eval(eOrig+'event.shiftKey');
	altK = eval(eOrig+'event.altKey');*/
	switch(key)	{
		case 119: alert(key); break; // f8
		case 118: break; // f7
		case 35: break; // end
		case 40: break; // arrow down
		case 36: break; // home
		case 38: break; // arrow up
		case 37: break;  // arrow left
		case 34: break; // page up
		case 39: break;// arrow right
		case 33: break;  // page down
		default: return false;
	}
}
///////////////////////////////////////////////////////////////////////////////
function saveCookie(name,value,days){
	if (days){
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000))
		var expires = "; expires="+date.toGMTString()
	}
	else expires = ""
	document.cookie = name+"="+value+expires+"; path=/"
}
function getCookie(name){// just want to return value for pair name given
	var cPairName, cPairValue, cookiePairs, cPairIndex;
	var curVal = "";
	var thisCookie = document.cookie;
	if (thisCookie.indexOf(name)==-1){
		return false;
	}
	if (thisCookie.indexOf(";")>-1){
		cookieStr = thisCookie.substr(0,thisCookie.length);
		var cookiePairs = cookieStr.split(";");
		for(i = 0; i < cookiePairs.length; i++){
			cPairIndex = cookiePairs[i].indexOf("=");
			cPairName = cookiePairs[i].substr(0,cPairIndex);
			while (cPairName.substr(0,1) == ' '){
				cPairName = cPairName.substr(1);
			}
			if (cPairName == name){
				cPairValue = cookiePairs[i].substr(cPairIndex+1);
				curVal = cPairValue; 
				return curVal;
			}
			cPairName = null;							
		}
	}
	else{
		char1 = thisCookie.indexOf(name)+name.length+1;
		curVal = thisCookie.substr(char1,thisCookie.length);		
	}	
	return curVal;
}
function deleteCookie(name){saveCookie(name,"",-1);}
///////////////////////////////////////////////////////////////////////////////
//                              SCROLL SECTION                               //
/////////// ///////////////////////////////////////////////////////////////////
function scrollDiv(e,endx,endy,inc,speed,fn){
	this.x = document.all(e).offsetLeft
	this.y = document.all(e).offsetTop
	if (endx == null) endx = this.x
	if (endy == null) endy = this.y
	var distx = endx-this.x
	var disty = endy-this.y
	scrollStart(e,endx,endy,distx,disty,inc,speed,fn)
}
scrollOn=true;
function scrollStart(e,endx,endy,distx,disty,inc,speed,fn){
	if (scrollOn) return;
	if (!inc) inc = 10;
	if (!speed) speed = 5;
	var num = Math.sqrt(Math.pow(distx,2) + Math.pow(disty,2))/inc;
	if (num == 0) return;
	var dx = distx/num;
	var dy = disty/num;
	if (!fn) fn = null;
	scrollOn = true;
	thisScroll(e,dx,dy,endx,endy,num,1,speed,fn)
}
function thisScroll(e,dx,dy,endx,endy,num,i,speed,fn){
	if (!scrollOn) return;
	if (i++ < num){
		moveBy(thisDiv,dx,dy); 
		if (scrollOn)	setTimeout("thisScroll(\""+e+"\","+dx+","+dy+","+endx+","+endy+","+num+","+i+","+speed+",\""+fn+"\")",40)
	}
	else{
		scrollOn = false;
		moveTo(e,endx,endy);
	}
}
function moveTo(e,x,y){	
	//alert('moveby is: ' + thisDiv);
	if (x!=null){
		this.x = 0;
		document.all(e).style.pixelLeft = this.x;
	}
	if (y!=null){
		this.y = y;
		document.all(e).style.pixelTop = this.y;
	}
}
function moveBy(e,x,y){
		moveTo(e,this.x+x,this.y+y);
}
function scrollStop(){
	scrollOn = false;
}
//onmousedown="scrollDiv('layoutsDiv',null,0,8,null); return false;
