// 'Element Reveal'and 'Content Swap' and 'Marquee' script with features :- 
// pop-uss, rotating images, screen centering, random or ordered, images or messages.
// Switch_store temporary variables are :-
// [0]is name, [1] is number, [2] is delay or marquee speed, [3] is Aname [4] is options,  [5] is centre, [6] is max
// [7] is stop flag, [8] is timeout variable, [9] is image width or marquee width, [10] is image height or marquee height
// [14] marquee internal variable,  [15] marquee internal variable,  [16] marquee internal variable,  [17] marquee internal variable,  [18] marquee internal variable,  
function switch_it(Sname,Snumber,Sdelay,SAname,Swidth,Sheight) {
// sort input data into switch_store

if (typeof switch_store=="object"){}else {switch_store={}};
if (typeof switch_store[Sname]=="object"){}else {switch_store[Sname]={}};
if (typeof switch_list=="object"){}else {switch_list={}};
if(typeof switch_store["o_top"]=="object"){}else {switch_store["o_top"]={}};
if(typeof switch_store["o_left"]=="object"){}else {switch_store["o_left"]={}};
Sdata=switch_store[Sname];
Sdata[0]=Sname;
if(Sdelay==undefined||Sdelay!="looping"){
Sdata[1]=(typeof Snumber=="number")? Snumber:(Snumber==undefined)? 0:(typeof Sdata[1]=="number")? Sdata[1]:0;
Sdata[7]=(Snumber=="stop")? Sdata[4]:Sdata[7];
Sdata[2]=(Sdelay>0)? Sdelay:(Sdelay<0)? -Sdelay:(Sdata[2]>0)? Sdata[2]:1;
Sdata[3]=(typeof SAname=="string" && Snumber!="last")? SAname:(typeof Sdata[3]=="string")? Sdata[3]:0;
Sdata[4]=(Snumber==undefined)? "view":(typeof Snumber!="number")? Snumber:(Sdata[4]=="random"||Sdata[4]=="ordered")? Sdata[4]:"view";
Sdata[5]=(Sdelay<0||Sdelay=="C"||Sdelay=="c")? "C":(Sdelay>0||Sdelay=="P"||Sdelay=="p")? "P":Sdata[5];
if(Sdata[6]>0){}
else{if(typeof switch_list[Sdata[0]]==="object" && typeof switch_list[Sdata[0]][0]=="string"){Sdata[6]=switch_list[Sdata[0]].length-1}
else{for (switch_j=1; switch_j<=100; switch_j++){
if(document.getElementById(Sdata[0] + '_' + switch_j)==null){break;}else{Sdata[6]=switch_j};};};}
Sdata[9]=(typeof Swidth=="number")? Swidth:(Sdata[9]>0)? Sdata[9]:0;
Sdata[10]=(Sheight!=undefined && typeof Swidth=="number")? Sheight:(typeof Swidth=="number"&& Sheight==undefined)? 0:(Sdata[10]>0)? Sdata[10]:0;
Sdata[15]='<nobr>'
var kk=0;var mm=0;
if(Sdata[1]>Sdata[6]){Sdata[1]=0};}
//select mode of operation and update switch_store
clearTimeout(Sdata[8]);
switch(Sdata[4]){
case 'last':Sdata[1]=switch_store[SAname][1];switch_auxilary(Sname);break;
case 'next':Sdata[1]=(Sdata[1]<Sdata[6]&&Sdata[1]>0)? Sdata[1]+1:Sdata[1];switch_auxilary(Sname);break;
case 'previous':Sdata[1]=(Sdata[1]>1)? Sdata[1]-1:Sdata[1];switch_auxilary(Sname);break;
case 'ordered':Sdata[1]=Sdata[1]+1; if(Sdata[1]>Sdata[6]){Sdata[1]=1};switch_auxilary(Sname);Sdata[8] =setTimeout(function() {switch_it(switch_store[Sname][0],switch_store[Sname][1],'looping')},(1000*switch_store[Sname][2]));break;
case 'random':Sdata[1]=Math.floor(Sdata[6]*Math.random())+1;switch_auxilary(Sname);Sdata[8] =setTimeout(function() {switch_it(switch_store[Sname][0],switch_store[Sname][1],'looping')},(1000*switch_store[Sname][2]));break;
case 'any':Sdata[1]=Math.floor(Sdata[6]*Math.random())+1;switch_auxilary(Sname);break;
case 'continue':Sdata[4]=(Sdata[7]==undefined)? "ordered_1":Sdata[7];if (Sdata[4]=="random_1"||Sdata[4]=="random_2"||Sdata[4]=="random_3"||Sdata[4]=="random_4"||Sdata[4]=="random_5"||Sdata[4]=="ordered_1"||Sdata[4]=="ordered_2"||Sdata[4]=="ordered_3"||Sdata[4]=="ordered_4"||Sdata[4]=="ordered_5"){switch_marquee_move(Sname)}else{switch_auxilary(Sname);Sdata[8] =setTimeout(function() {switch_it(switch_store[Sname][0],switch_store[Sname][1],'looping')},(1000*switch_store[Sname][2]))};break;
case 'end':Sdata[1]=Sdata[6];switch_auxilary(Sname);break;
case 'start':Sdata[1]=1;switch_auxilary(Sname);break;
case 'view':switch_auxilary(Sname);break;
case 'stop' : break;
case 'random_1' :for (var zz=1;zz<=(switch_list[Sdata[0]].length-1);zz++){mm = 1;kk = Math.floor((switch_list[Sdata[0]].length-1)*Math.random())+1;switch_marquee_content(kk,mm)};switch_marquee();break;
case 'random_2' :var jim=0;for (var zz=1;zz<=(switch_list[Sdata[0]].length-1);zz++){jim=jim+1;if(jim>(switch_list[switch_list[Sdata[0]][0]].length-1)){jim=2};if(jim%2==1){mm=1}else{mm=jim};kk = Math.floor((switch_list[Sdata[0]].length-1)*Math.random())+1;switch_marquee_content(kk,mm)};switch_marquee();break;
case 'random_3' :var jim=1;for (var zz=1;zz<=(switch_list[Sdata[0]].length-1);zz++){jim=jim+0.5;if(jim>(switch_list[switch_list[Sdata[0]][0]].length-1)){jim=1.5};if(jim%1==0.5){mm=1}else{mm=jim};kk = Math.floor((switch_list[Sdata[0]].length-1)*Math.random())+1;switch_marquee_content(kk,mm)};switch_marquee();break;
case 'random_4' :for (var zz=1;zz<=(switch_list[Sdata[0]].length-1);zz++){mm = Math.floor((switch_list[switch_list[Sdata[0]][0]].length-1)*Math.random())+1;kk = Math.floor((switch_list[Sdata[0]].length-1)*Math.random())+1;switch_marquee_content(kk,mm)};switch_marquee();break;
case 'random_5' :for (var zz=1;zz<=(switch_list[Sdata[0]].length-1);zz++){mm = Math.floor((switch_list[switch_list[Sdata[0]][0]].length-2)*Math.random())+2;if(zz%2==1){mm=1};kk = Math.floor((switch_list[Sdata[0]].length-1)*Math.random())+1;switch_marquee_content(kk,mm)};switch_marquee();break;
case 'ordered_1' :for (var zz=1;zz<=(switch_list[Sdata[0]].length-1);zz++){mm = 1;kk=zz;switch_marquee_content(kk,mm)};switch_marquee();break;
case 'ordered_2' :var jim=0;for (var zz=1;zz<=(switch_list[Sdata[0]].length-1);zz++){jim=jim+1;if(jim>(switch_list[switch_list[Sdata[0]][0]].length-1)){jim=2};if(jim%2==1){mm=1}else{mm=jim};kk=zz;switch_marquee_content(kk,mm)};switch_marquee();break;
case 'ordered_3' :var jim=1;for (var zz=1;zz<=(switch_list[Sdata[0]].length-1);zz++){jim=jim+0.5;if(jim>(switch_list[switch_list[Sdata[0]][0]].length-1)){jim=1.5};if(jim%1==0.5){mm=1}else{mm=jim};kk=zz;switch_marquee_content(kk,mm)};switch_marquee();break;
case 'ordered_4' :for (var zz=1;zz<=(switch_list[Sdata[0]].length-1);zz++){mm = Math.floor((switch_list[switch_list[Sdata[0]][0]].length-1)*Math.random())+1;kk=zz;switch_marquee_content(kk,mm)};switch_marquee();break;
case 'ordered_5' :for (var zz=1;zz<=(switch_list[Sdata[0]].length-1);zz++){mm = Math.floor((switch_list[switch_list[Sdata[0]][0]].length-2)*Math.random())+2;if(zz%2==1){mm=1};kk=zz;switch_marquee_content(kk,mm)};switch_marquee();break;
}}

// a sub function for secondary loop when auxilary used
function switch_auxilary (Sname) {
// loop for name and auxilary name
for (switch_count=0;switch_count<=4;switch_count=switch_count+3){Sgroup=Sdata[switch_count];Slist=switch_list[Sgroup];
if (Sgroup!=0){
// select 'element reveal' or 'content swap'
if(typeof Slist=="object"){switch_content()}else{switch_id()};};};
}

// a sub function for 'content swap'
function switch_content(){
document.getElementById(Sgroup).style.display = 'block';
if (Slist[Sdata[1]].search(/.jpg/i)==Slist[Sdata[1]].length-4||Slist[Sdata[1]].search(/.gif/i)==Slist[Sdata[1]].length-4||Slist[Sdata[1]].search(/.png/i)==Slist[Sdata[1]].length-4){if(Sdata[5]=="C"){document.getElementById(Sgroup).style.zIndex=-100};document.getElementById(Sgroup).src =Slist[Sdata[1]];}
else{document.getElementById(Sgroup).innerHTML =Slist[Sdata[1]]}
if(Sdata[5]=="C"){showdeadcenterdiv(Sgroup);}else{if(switch_store["o_top"][Sgroup]!=undefined){document.getElementById(Sgroup).style.top=switch_store["o_top"][Sgroup];};if(switch_store["o_left"][Sgroup]!=undefined){document.getElementById(Sgroup).style.left=switch_store["o_left"][Sgroup];}}
if(Sdata[1]==0){document.getElementById(Sgroup).style.display='none'; };}

//a sub function for 'element reveal'
function switch_id () {
for (switch_i=1; switch_i<=Sdata[6];switch_i++){
var showDivName = Sgroup + '_' + switch_i;
var showObj = document.getElementById(showDivName);
if (showDivName == Sgroup + '_' + Sdata[1])
{showObj.style.display = 'block';
if(Sdata[5]=="C"){showdeadcenterdiv(showDivName)}else{if(switch_store["o_top"][showDivName]!=undefined){document.getElementById(showDivName).style.top=switch_store["o_top"][showDivName];};if(switch_store["o_left"][showDivName]!=undefined){document.getElementById(showDivName).style.left=switch_store["o_left"][showDivName];}}}
else
{showObj.style.display = 'none';};}}

// a sub function to centre on screen
function showdeadcenterdiv(switch_id) {
switch_r=document.getElementById(switch_id);
if(switch_store["o_top"][switch_id]==undefined){switch_store["o_top"][switch_id]=switch_r.style.top}
if(switch_store["o_left"][switch_id]==undefined){switch_store["o_left"][switch_id]=switch_r.style.left};
switch_r.style.position='absolute';
switch_r.style.display = "block";
// First, determine how much the visitor has scrolled 
if( self.pageYOffset ) { 
switch_scrolledX = self.pageXOffset; 
switch_scrolledY = self.pageYOffset; 
} else if( document.documentElement && document.documentElement.scrollTop ) { 
switch_scrolledX = document.documentElement.scrollLeft; 
switch_scrolledY = document.documentElement.scrollTop; 
} else if( document.body ) { 
switch_scrolledX = document.body.scrollLeft; 
switch_scrolledY = document.body.scrollTop; }
// Next, determine the coordinates of the center of browser's window 
if( self.innerHeight ) { 
switch_centerX = self.innerWidth; 
switch_centerY = self.innerHeight; 
} else if( document.documentElement && document.documentElement.clientHeight ) { 
switch_centerX = document.documentElement.clientWidth; 
switch_centerY = document.documentElement.clientHeight; 
} else if( document.body ) { 
switch_centerX = document.body.clientWidth; 
switch_centerY = document.body.clientHeight; } 
//find the nested offset
   obj=switch_r.offsetParent;
   switch_curleft = 0;
   switch_curtop = 0;
   if (obj.offsetParent) {
            do
                 { switch_curleft += obj.offsetLeft;
                  switch_curtop += obj.offsetTop;
            }while (obj = obj.offsetParent);
      }
// determine image width and height 
if (typeof Slist=="object" && typeof Slist[0]=="string" && Slist[0]=="image"){
if(switch_list[Sdata[switch_count]+"_height"][Sdata[1]]==undefined){image_height=1}else{image_height=switch_list[Sdata[switch_count]+"_height"][Sdata[1]]}
if(switch_list[Sdata[switch_count]+"_width"][Sdata[1]]==undefined){image_width=1}else{image_width=switch_list[Sdata[switch_count]+"_width"][Sdata[1]]}
if (image_width==1 && image_height>1){image_width=image_height};
if (image_width>1 && image_height==1){image_height=image_width};
if(Sdata[9]==0&&Sdata[10]==0 &&(image_width!=1||image_height!=1)){Xwidth=image_width;Yheight=image_height};
if(Sdata[9]>0&&Sdata[10]==0){Xwidth=Sdata[9];Yheight=Math.round(image_height* Sdata[9]/image_width)};
if(Sdata[9]==0&&Sdata[10]>0){Xwidth=Math.round(image_width* Sdata[10]/image_height);Yheight=Sdata[10]};
if(Sdata[9]>0&&Sdata[10]>0){Xwidth=Sdata[9];Yheight=Sdata[10]};
switch_r.style.width = Xwidth + 'px';
switch_r.style.height = Yheight + 'px';
}else{
var Xwidth=parseInt(switch_r.offsetWidth);
var Yheight=parseInt(switch_r.offsetHeight);
if(!Xwidth){Xwidth=0};
if(!Yheight){Yheight=0};
}
// define the position 
var leftOffset = switch_scrolledX + (switch_centerX/2 - Xwidth/2 - switch_curleft); 
var topOffset = switch_scrolledY + (switch_centerY/2 - Yheight/2 - switch_curtop); 
switch_r.style.top = topOffset + 'px'; 
switch_r.style.left = leftOffset + 'px';
switch_r.style.zIndex = 100;  }

// a sub function to attach a different link to each image
function switch_link() {
window.open(switch_list[Sdata[0]+"_link"][Sdata[1]],"Switch_Link");}

//sub functions for the marquee
function switch_marquee(){
Sdata[15]=Sdata[15]+'</nobr>';
Sdata[18]=Sdata[2];
Sdata[17]=0; Sdata[16]=0; 
var switch_font_size = Sdata[10] - 4;
if (document.getElementById) {Sdata[14] = '<div style="position:relative;width:'+Sdata[9]+'px;height:'+Sdata[10]+'px;overflow:hidden;'+switch_list[switch_list[Sdata[0]][0]][0]+'"';
Sdata[14] +=' onmouseover="switch_store[parentNode.id][18]=0" onmouseout="switch_store[parentNode.id][18]=switch_store[parentNode.id][2]"'; Sdata[14] +='><div id="'+Sdata[0]+'_inside" style="position:absolute;left:0px;top:0px;font-family:arial,sans-serif;font-size:'+switch_font_size+'px;line-height:'+Sdata[10]+'px;white-space:nowrap;"><\/div><\/div>'; 
document.getElementById(Sdata[0]).innerHTML = Sdata[14]; 
Sdata[16] = document.getElementById(Sdata[0]+"_inside"); 
Sdata[16].style.left=Sdata[9]+10+"px"; 
Sdata[16].innerHTML='<span id="'+Sdata[0]+'_tx">'+Sdata[15]+'<\/span>'; 
Sdata[17] = document.getElementById(Sdata[0]+"_tx").offsetWidth;
switch_marquee_move(Sdata[0]);}} 

//sub function for marquee content construction
function switch_marquee_content(kk,mm){
if(switch_list[Sdata[0]][kk].search(/.jpg/i)>0||switch_list[Sdata[0]][kk].search(/.gif/i)>0||switch_list[Sdata[0]][kk].search(/.png/i)>0){var switch_mod="<img style=\"height:"+Sdata[10]+"px\" src=\""+switch_list[Sdata[0]][kk]+"\">"}else{switch_mod=switch_list[Sdata[0]][kk]};
Sdata[15] =Sdata[15]+'<span style=\"'+switch_list[switch_list[Sdata[0]][0]][mm]+'\">' + switch_mod +'</span>'}

//sub function for marquee loop
function switch_marquee_move(Mname){
Mdata=switch_store[Mname];
Mdata[16].style.left = (parseInt(Mdata[16].style.left)>(-10 - Mdata[17])) ?parseInt(Mdata[16].style.left)-Mdata[18]+"px" : Mdata[9]+10+"px"; 
Mdata[8] =setTimeout(function() {switch_marquee_move(Mname)},50); }
