function ObjectRef(ObjectName)  // return a DOM object reference
{
if(InternetExplorer)
    return document.getElementById(ObjectName); 
else 
    return ((document.layers) ? document.layers[ObjectName]:eval(ObjectName));

}


// Replace until there are no more instances of the substring
function ReplaceAll(S,SS,RS)
	{
	var oSS=new RegExp(SS);
	while(S.indexOf(SS)>=0)
		S=S.replace(oSS,RS);
	return S;
	}
	
function GetUrlParameter(sURL,sName)  // retrieve named url parameter
	{		
	var S=" "+sURL;
	var iPos=S.indexOf("?");	
	if(iPos<0)
	  return "";
	  
	S=S.substring(iPos+1);
	
	iPos=S.indexOf(sName+"=");
	if(iPos<0)
		return "";
		
	iPos+=(sName.length+1);
	var SR="",x;
	for(x=iPos;S.charAt(x)!="&" && x<S.length;x++)
		{
		SR+=S.charAt(x);
		}
	return SR;
	}
	




// My Tag Reader
function tag_reader(sX)
{
/* Programmer: Reginald Armond
   Purpose: My self contained simple xml reader
*/ 
this.sgml=sX;
this.ipos=0;
this.init=fn_init;
this.current_tag="";
this.current_attributes="";
this.current_tagtext="";
this.current_taghtml="";
this.bEof=(sX.length<1);

// Interface
this.tag=fn_tag;
this.next=fn_next;
this.eof=fn_eof;
this.property=fn_property;
this.tag_text=fn_tag_text;
this.tag_html=fn_tag_html;

function fn_init(SS)
  {
  this.sgml=SS;
  this.ipos=0;
  this.bEof=(SS.length<1);
  }
function fn_next()
  {

  var iPos=this.sgml.indexOf("<",this.ipos);

this.current_tag="";
this.current_attributes="";
this.current_tagtext="";
this.current_taghtml="";


  var iStart;
  if(iPos<0)
    {
     this.bEof=true;
     return false;
    }
  iStart=iPos;

  iPos++;
  var tagname="";
  var c=this.sgml.charAt(iPos++).toLowerCase();
  while(iPos<this.sgml.length && ((c>="a" && c<="z") || c=="/" || c=="?"))
    {
    tagname+=c;
    c=this.sgml.charAt(iPos++).toLowerCase();
    }
  iPos--;

  if(tagname.length<1)
    {
     this.bEof=true;
     return false;
    }
  var iPosEnd=iPos;
  var iQuotes=0;
  var sProperties="";

  c=this.sgml.charAt(iPosEnd++);

  // find end of tag
  while(iPosEnd<this.sgml.length && (c==">" && (iQuotes & 1)==0)==false)
    {
    sProperties+=c;
    if(c=='"')
      iQuotes++;
    c=this.sgml.charAt(iPosEnd++);
    }

  iPosEnd--;

  if(c==">")
    {
    this.current_tag=tagname;
    this.ipos=iPosEnd+1;

    if(tagname.charAt(0)=="/")
      return true;
    this.current_attributes=sProperties;

    var findtag="</"+tagname+">";
    var iFoundTag=this.sgml.indexOf(findtag,this.ipos);
    if(iFoundTag>=0)
      {
      this.current_tagtext=this.sgml.substring(iStart,iFoundTag);
      this.current_taghtml=this.sgml.substring(this.ipos,iFoundTag);//+findtag;
      }

    return true;
    }

  return false;
  }
function fn_tag()
  {
  return this.current_tag;
  }
function fn_eof()
  {
  return this.bEof;
  }
function fn_property(sKey)
  {
  return properties_get(this.current_attributes,sKey);
  }
function fn_tag_text()
  {
   return this.current_tagtext;
  }
function fn_tag_html()
  {
   return this.current_taghtml;
  }
}

function properties_get(sProps,sKey)
{
         if (sProps=="")
           return "";
		
		sProperties=sProps;
		sProperties=ReplaceAll(sProperties,"\"\"","\"?\"");		
		
         var iPos=sProperties.indexOf(sKey);
         if(iPos<0)
           return "";
         iPos+=sKey.length;

         var c=sProperties.charAt(iPos++);
         while(iPos<sProperties.length && c==" " && c!="=")
             {
             c=sProperties.charAt(iPos++);
             }
         if(c!="=")
             return "";

         iPos++;
         c=sProperties.charAt(iPos++);

         while(iPos<sProperties.length && c==" ")
             {
             c=sProperties.charAt(iPos++);
             }
         var value="";
         if(c=='"')
           {           
           c=sProperties.charAt(iPos++);
           }
          while(iPos<sProperties.length && c!='"')
             {
             value+=c;
             c=sProperties.charAt(iPos++);
             }
			if(value=="?")
				value="";
          return value;

}

function properties_put(sProperties,sKey,sValue)
{
        if (sProperties=="")
           {
           sProperties=sKey+"=";
           sProperties+='"';
           sProperties+=sValue;
           sProperties+='"';
           return sProperties;
           }
         var iPos=sProperties.indexOf(sKey);
         if(iPos<0)
            {
           sProperties+=" ";
           sProperties+=sKey+"=";
           sProperties+='"';
           sProperties+=sValue;
           sProperties+='"';
           return sProperties;
           }
         iPos+=sKey.length;
         var c=sProperties.charAt(iPos);
         while(iPos<sProperties.length && c==" " && c!="=")
             {
             c=sProperties.charAt(iPos); iPos++;
             }
         if(c!="=")
             return undefined;
         iPos++;
         c=sProperties.charAt(iPos);

         while(iPos<sProperties.length && c==" ")
             {
             c=sProperties.charAt(iPos); iPos++;
             }
         if(c!='"')
           return undefined;
         var iPosEnd=sProperties.indexOf('"',iPos+1);
         if (iPosEnd<0)
          return undefined;
         var sLeft=sProperties.substring(0,iPos+1);
         var sRight=sProperties.substring(iPosEnd);
         sProperties=sLeft;
         sProperties+=sValue;
         sProperties+=sRight;
         return sProperties;
}


// 
// Constructor for thumbnail item object
function thumbnail()
{
this.id=0;
this.image_ref="";
this.width=1;
this.height=1;
this.title="title";
this.description="description";
this.heading="heading";
this.stage="";
this.image_orientation="vertical";
this.Assign=fn_set_attributes;
this.GetItem=fn_item_instance_render;

function fn_set_attributes(s_id,s_image,s_width,s_height,s_title,s_description,s_stagedir,s_heading,s_image_orientation)
{
this.id=s_id;
this.image_ref=s_image;
this.width=s_width;
this.height=s_height;
this.title=s_title;
this.stage=s_stagedir;
this.description=s_description;
this.image_orientation=s_image_orientation;
this.heading=s_heading;
}

function fn_item_instance_render()
	{
	var S="";	
	var WO; // Window opener
	// S='<img src="{image_ref}" width="{width}px" height="{height}px">';
	S="<table class=compVideoEnclosure cellSpacing=0 cellPadding=0 border=0><tr><td>";	
	S+='<TABLE class=compVideo cellSpacing=0 cellPadding=0 width=365 border=0>';	
	S+="<TBODY><TR vAlign=top><TD class=videoTitle colSpan=2>{title}</TD></TR><TR vAlign=top><TD rowSpan=2>";		
	
	WO="window.open('http://movies.compulsion.tv/flash/splash2/{orientation}_stills/{stage}/indexStage.html',745,610);";	
	S+='<A href="#" onclick="'+WO+'">';	
	S+='<IMG height="{height}px" src="{image_ref}" width="{width}px" border=0/></A>';
	S+="</TD>";		
    S+='<TD style="PADDING-RIGHT: 5px; PADDING-LEFT: 5px; FONT-SIZE: 12px; PADDING-BOTTOM: 5px; PADDING-TOP: 5px; FONT-FAMILY: arial, Helvetica, sans-serif">{description}</TD>';
	
	S+="</TR><TR vAlign=bottom><TD>";
	S+='<A href="#" onclick="'+WO+'">';	
	S+='<IMG height=25 src="img/click_to_see.gif" width=163 border=0 /></A></TD></TR>';	
	S+='</TBODY></TABLE>';
	S+='</td></tr></table>';

	S=ReplaceAll(S,"{description}",""+this.description);
	S=ReplaceAll(S,"{heading}",""+this.heading);
	S=ReplaceAll(S,"{image_ref}",this.image_ref);
	S=ReplaceAll(S,"{width}",""+this.width);
	S=ReplaceAll(S,"{height}",""+this.height);
	S=ReplaceAll(S,"{title}",""+this.title);
	S=ReplaceAll(S,"{stage}",""+this.stage);		
	S=ReplaceAll(S,"{orientation}",""+this.image_orientation);		
	
	return S;
	}
}

// Function to add 1 thumbnail per call
function add_thumbnail(sDivID,tn_item) // accepts a thumbnail item object
{
var OBJ=ObjectRef(sDivID);
if(OBJ==null)
  return;

OBJ.innerHTML+=tn_item.GetItem();
}

// function to add all thumbnails
function Update_Thumbnails(sDivID,sXML)
{
var OBJ=ObjectRef(sDivID);

if(OBJ==null)
  return;
OBJ.innerHTML="";

// Parse XML and add thumbnail instances
var xr=new tag_reader(sXML);
var s_title,s_description,s_width,s_height,s_id,s_image,s_stage,s_heading,s_image_orientation;
var image_count=0,total_height=200,height_max=0,height=0;


xr.next();

while(xr.eof()==false)
	{
	if(xr.tag()=="hypermovie")
		{
		s_id=xr.property("id");
		while(xr.eof()==false && xr.tag()!="/hypermovie") // read tags within hypermovie
			{
			if(xr.tag()=="image")
				{
				s_image=xr.property("src");
				s_width=xr.property("width");
				s_height=xr.property("height");	
				height=parseInt(s_height,10);
				width=parseInt(s_width,10);
				height_max=(height>height_max) ? height:height_max;
				image_count++;				
				}

			if(xr.tag()=="description")
				{
				s_description=xr.tag_html();
				}
			if(xr.tag()=="title")
				{
				s_title=xr.tag_html();				
				}
			if(xr.tag()=="stage")
				{
				s_stage=xr.tag_html();
				s_image_orientation=(height>=width) ? "vertical":"horizontal";
				}
			if(xr.tag()=="heading")
				{
				s_heading=xr.tag_html();
				}
				
			xr.next();
			}
			
		var oItem=new thumbnail();
		/*
		alert("id: "+s_id+"\nimage: "+s_image+"\nwidth: "+s_width+"\nheight: "+s_height+"\ntitle: "+s_title+"\ndescription: "+s_description+"\nstage: "+s_stage);
		*/
		oItem.Assign(s_id,s_image,s_width,s_height,s_title,s_description,s_stage,s_heading,s_image_orientation);		
		add_thumbnail(sDivID,oItem);
		if((image_count & 1)!=0)
			{
			total_height+=(height_max+10);
			OBJ.style.height=total_height;
			height_max=0;
			}
		}
	
	xr.next();
	}

}

// AJAX OBJECT
function SendRequest()
    {
    // SendRequest object constructor

    this.oDocument=null;
    this.oRemotingDiv=null;
    this.oXHR=null;
    this.sGetDoc="";
    this.cbfn_Document=null;
    this.beIE = document.all?true:false;
    this.OpenXMLHttpRequest=fn_open_xml_http_request;
    this.Open=fn_create; // iframe method
    this.Send=fn_send; // iframe
    this.CallServer=fn_callserver;
    this.UpdatePage=fn_update_page;
    this.GetDocument=fn_get_document;
    g_oSendRequest=this;
    function fn_get_document(sURL,cb)
        {
        var sCMD=sURL;
        this.sGetDoc=sURL;
        this.cbfn_Document=cb;
        this.oXHR.open("GET",sURL,true);
        // also HEAD for .getAllResponseHeaders() || .getResponseHeader("LastModified");
        
        this.oXHR.onreadystatechange=this.UpdatePage;

        this.oXHR.send(null);
        
        }
    function fn_update_page()
        {
        // also .status==404notExist || .status==200Exist
        
        var dis=null;
        var i;
        dis=g_oSendRequest;
        if(dis.oXHR.readyState==4)
            {
            var res=dis.oXHR.responseText;
            if(dis.cbfn_Document!=null)
                {
                dis.cbfn_Document(res);
                dis.cbfn_Document=null;                
                return;
                }
            var afields=res.split(";");
            var n=0;
            for(n=0;n<afields.length;n++)
                {
                var values=afields[n].split("=");
                try 
                    {
                    dis.oDocument.getElementById(values[0]).value=unencode(values[1]);
                    }
                catch(e)
                    {
                    }
                }
            }
        }
    function fn_callserver(sURL,sFormFields)
        {
        var sCMD=sURL+"?";
        var afields=sFormFields.split(";");
        var n,sValue;
        for(n=0;n<afields.length;n++)
            {
            try
                {
                sValue=escape(this.oDocument.getElementById(afields[n]).value);
                }
            catch(e)
                {
                sValue="";
                }
            sCMD+=((n!=0) ?"&":"")+afields[n]+"="+sValue;
            }
        this.oXHR.open("GET",sURL,true);
        // also HEAD for .getAllResponseHeaders() || .getResponseHeader("LastModified");
        this.oXHR.onreadystatechange=this.UpdatePage;
        this.oXHR.send(null);
        }
    function fn_open_xml_http_request(oDocument)
        {
        this.oDocument=oDocument;
        
        if(this.beIE)
            {
            try
                {
                this.oXHR=new ActiveXObject("Msxml2.XMLHTTP");                
                }
            catch(e)
                {
                try
                    {
                    this.oXHR=new ActiveXObject("Microsoft.XMLHTTP");
                    }
                catch(e2)
                    {
                    this.oXHR=null;
                    }
                }
            }
        else
            {
            try
                {
                this.oXHR=new XMLHttpRequest();                
                }
            catch(e)
                {
                this.oXHR=null;
                }
            }

        }
    function fn_create(oDocument,sID,sURL)
        {
        this.oDocument=oDocument;
        var oDiv=this.oDocument.createElement('div');
        oDiv.id='remotingDiv';
        var style='border:0;width:0;height:0;';
        oDiv.innerHTML="<iframe name='"+sID+"' id='"+sID+"' style='"+style+"'></iframe>";
        this.oDocument.body.appendChild(oDiv);
        oDiv.iframe=this.oDocument.getElementById(sID);
        
        oDiv.form=this.oDocument.createElement('form');
        oDiv.form.setAttribute('id',sID+'RemotingForm');
        oDiv.form.setAttribute('action',sURL);
        oDiv.form.setAttribute('target',sID);
        oDiv.form.target=sID;
        oDiv.form.setAttribute('method','post');
        oDiv.form.innerHTML='<input type="hidden" name="data" id="'+sID+'Data">';
        oDiv.appendChild(oDiv.form);
        oDiv.data=this.oDocument.getElementById(sID+'Data');
        this.oRemotingDiv=oDiv;        
        }
    function fn_send(sURL,sData,fn_callback)
        {
        // Send request call
        if(this.oRemotingDiv==null)
            this.oRemotingDiv=this.Open('remotingFrame','blank.html');
        this.oRemotingDiv.form.action=sURL;
        this.oRemotingDiv.data.value=sData;
        this.oRemotingDiv.callback=fn_callback;
        this.oRemotingDiv.form.submit();
        }
    
    }
	

