//send photo to a friend function
var send_to_friend = "http://www.cstv.com/cgi-bin/sprintsendtofriend.cgi?link=";

ajax={};
ajax.x=function(o)
{ 
	var _obj=false;
	try{_obj={'o':o,'ax':new ActiveXObject("Msxml2.XMLHTTP")};return _obj;}
	catch(e)
	{	
		try{_obj={'o':o,'ax':new ActiveXObject("Microsoft.XMLHTTP")};return _obj;}
		catch(e){_obj=false;}
	} 
	if(typeof XMLHttpRequest!="undefined")
	{
		try{_obj={'o':o,'ax':new XMLHttpRequest()};/*_obj.overrideMimeType("text/xml")*/;return _obj;}
		catch(e){_obj=false;}
	}
	if(_obj==false){alert("We do not support your browser ... just yet ...");}  
	return _obj; 
}
ajax.serialize=function(f){var g=function(n){return f.getElementsByTagName(n)};var nv=function(e){if(e.name)return encodeURIComponent(e.name)+'='+encodeURIComponent(e.value);else return ''};var i=collect(g('input'),function(i){if((i.type!='radio'&&i.type!='checkbox')||i.checked)return nv(i)});var s=collect(g('select'),nv);var t=collect(g('textarea'),nv);return i.concat(s).concat(t).join('&');};
ajax.send=function(u,f,m,a,o){var x=ajax.x(o);x.ax.open(m,u,true);x.ax.onreadystatechange=function(){if(x.ax.readyState==4)f(x.ax.responseText,x.o)};if(m=='POST')x.ax.setRequestHeader('Content-type','application/x-www-form-urlencoded');x.ax.send(a)};
ajax.get=function(url,func,o){ ajax.send(url,func,'GET',null,o)};
ajax.gets=function(url,o){var x=ajax.x(o);x.ax.open('GET',url,false);x.ax.send(null);return x.ax.responseText};
ajax.post=function(url,func,args,o){ajax.send(url,func,'POST',args,o)};
ajax.update=function(url,elm){var e=$(elm);var f=function(r){e.innerHTML=r};ajax.get(url,f)};
ajax.submit=function(url,elm,frm){var e=$(elm);var f=function(r){e.innerHTML=r};ajax.post(url,f,ajax.serialize(frm))};

loadjs=function(jsurl,id)
{
	var js=document.createElement('script');
	js.src=jsurl;
	js.tyle="text/javascript";
	if(typeof(id)!="undefined") 
	{
		try{document.getElementsByTagName('head').item(0).removeChild(document.getElementById(id))} catch(e){};
		js.id=id;
	}
	try{ document.getElementsByTagName('head').item(0).appendChild(js); }catch(e){alert('loadjs error')}
}
loadcss=function(cssurl)
{
	var css=document.createElement('link');
	css.href=cssurl;
	css.tyle="text/css";
	css.rel="stylesheet"
	document.getElementsByTagName('head').item(0).appendChild(css);
}

PhGalleryClass=function(IDprefix,thisID)
{
	this.IDprefix=IDprefix;
	this.thisID=thisID;
	this.gsources={};
	this.g=0; // gallery iterator
	this.n=0; // photo iterator
	this.loadgallery=function(g)
	{
//		if(!this.gsources.src[g].match(/^http:\/\/www\.cstv\.com\//)) alert("Sorry, only galleries on cstv.com are allowed !\n"+this.gsources[g])
		try
		{
			s=ajax.gets(this.gsources.collection[g].src+"?"+Math.random().toString());			
			if(!s)
			{
				try{
					loadjs(this.gsources.collection[g].src+"?"+Math.random().toString(),"loadjsobj")
				}
				catch(e){alert("loadjs error\n"+this.gsources.collection[g].src+"\n"+e)
				}
			}	
		}catch(e){
			try{
				loadjs(this.gsources.collection[g].src+"?"+Math.random().toString(),"loadjsobj")
			}
			catch(e){
				alert("loadjs error\n"+this.gsources.collection[g].src+"\n"+e)
			}
			return false;
		}
		try{eval("this.imgs="+s);}catch(e){alert(e)}
		try{if(this.gsources.collection[g].title&&!this.gsources.collection[g].title.match(/^\s$/))document.getElementById(this.IDprefix+'gallerytitle').innerHTML=this.gsources.collection[g].title;}catch(e){alert("phgc title error\n"+e)}
		
//		this.populateThumb();
//		this.saveMessage();
//		this.showStandings();
		return true;
	}
	this.saveMessage = function(){
		this.message = document.getElementById('g1_imgdet').innerHTML;
	}	
	this.populateThumb = function(){
		//pull first few items from this.imgs.imgs array and populates the thumbnails
		for(pop = 0; pop < this.imgs.imgs.length; pop++){
			var target_thumb = document.getElementById(this.IDprefix+'img'+(pop+1))			
			var target_header = document.getElementById(this.IDprefix+'hd'+(pop+1))
			if(target_thumb == undefined){
				break;
			}
			var thumb_image = this.imgs.base+this.imgs.imgs[pop].src;				
			target_header.innerHTML = this.imgs.imgs[pop].title.split(",")[0];
			target_thumb.src = thumb_image;
			
		}
	}
	this.sendPhoto=function()
	{
		var sg=document.getElementById(this.IDprefix+'select');
		var pglink=document.getElementById(this.IDprefix+'form_link');
		var pgform=document.getElementById(this.IDprefix+'pgform');
		if(!sg || !pglink || !pgform) {return false;}
		var gallery=sg.selectedIndex;
		var pgname=this.gsources.collection[gallery].title;
		var pgsrc=this.gsources.collection[gallery].src;
		pglink.value=window.location.protocol+"//"+window.location.host+window.location.pathname+"?"+escape("collection="+this.thisID+"&gallery="+gallery+"&photo="+this.n+"&src="+escape(pgsrc));
		pgform.submit();	
	}
	this.loadImage=function()
	{
		n=(typeof(params['photo'])!="undefined")?params['photo']:0;
		this.showImage();
	}
	this.updateGallery=function()
	{
		var sg=document.getElementById(this.IDprefix+'select');
		if(this.loadgallery(sg.selectedIndex)){
			//setTimeout(this.thisID+'.firstImage()',500);	
			this.n=-1;
		}
			
		
	}
	this.firstImage=function(){	this.n=0; this.showImage();	}
	this.adcounter=0;
	this.adperiod=6;
	this.nextImage=function()
	{
		this.adcounter++;
		if(this.adcounter==this.adperiod)
		{
			this.adcounter=0;
			this.showAd();
		}
		else
		{
			this.n++; 
			if(this.n>=this.imgs.imgs.length) { this.n=0;} 
			this.showImage();
		}
	}
	this.prevImage=function() 
	{
		this.adcounter++;
		if(this.adcounter==this.adperiod){	this.adcounter=0;	this.showAd();	}
		else	{	this.n--; if(this.n < 0) { this.n=this.imgs.imgs.length - 1; };	this.showImage(); }
	}
	this.showImage=function() 
	{
		if(!this.imgs) return;
		
//		if (this.standing == undefined){
//			this.standing = false;
//			this.n = -1;
//			return;
//		}
		
		try{document.getElementById(this.IDprefix+"adiv").style.display="none";}catch(e){alert("1:"+e)};
		try{document.getElementById(this.IDprefix+'sendtoafriend').disabled=false;}catch(e){alert("1:"+e)};
		var	img = '<img src="' + this.imgs.base+this.imgs.imgs[this.n].src + '" width="' + this.imgs.imgs[this.n].w + '" height="' + this.imgs.imgs[this.n].h + '" border="0" />';
		var imgcredit= '<span class="credit">' + this.imgs.imgs[this.n].credit + '</span>';
		var h = '<div class="caption">' + this.imgs.imgs[this.n].caption + '</div>';

		try{ document.getElementById(this.IDprefix+'imgtitle').innerHTML = this.imgs.imgs[this.n].title; }catch(e){};
		try{ document.getElementById(this.IDprefix+'imgdiv').innerHTML = img; }catch(e){};
		try{ document.getElementById(this.IDprefix+'imgdet').innerHTML = h; }catch(e){};
		try{ document.getElementById(this.IDprefix+'imgcredit').innerHTML = imgcredit; }catch(e){};
		try{ document.getElementById(this.IDprefix+'imgdiv').style.display  = "block"; }catch(e){};
		try{ document.getElementById(this.IDprefix+'standings').style.display  = "none"; }catch(e){};
		
		var pNum = n + 1 //photo number
		var nTotal = this.imgs.imgs.length; //photo total

		var hcopy ='';
		hcopy += pNum + ' / ' + nTotal; 
	
		try{document.getElementById(this.IDprefix+'num_count').innerHTML = hcopy;}catch(e){};
	}
	this.showStandings = function(){
		container = this.IDprefix+'standings';
		current_week = document.getElementById(this.IDprefix+'select').value;
		current_week = current_week.match(/\d+$/);
		var url =  "inc/standings_week"+current_week+".html";
		
		var pars = '';
		
		var myAjax = new Ajax.Updater( 
			container, 
			url, 
			{
				method: 'get', 
				parameters: pars
			});
		
		try{ document.getElementById('g1_imgdet').innerHTML = this.message; }catch(e){};
		try{ document.getElementById(this.IDprefix+'imgtitle').innerHTML = ""; }catch(e){};
		try{ document.getElementById(this.IDprefix+'standings').style.display  = "block"; }catch(e){};
		try{ document.getElementById(this.IDprefix+'imgdiv').style.display  = "none"; }catch(e){};
	}
	this.showAd=function() 
	{
		try{document.getElementById(this.IDprefix+'sendtoafriend').disabled=true;}catch(e){alert(e)};
		try{document.getElementById(this.IDprefix+"adiv").style.display="block";}catch(e){alert(e)};
 	}
	this.thsImage= function(image_num){
		this.n= image_num - 1; //subtract it from one so showImage reads it in the correct number & easier to update on html side
		this.showImage();
	}
	this.highlightThumb = function(box){
		fade_div = box.getElementsByTagName("DIV")[0];			
		fade_div.className = "bar_on";
	}
	this.unHighlightThumb = function(box){
		fade_div = box.getElementsByTagName("DIV")[0];		
		fade_div.className = "bar";
	}
};


//for ad
var ad = new Array();
function ad(filename,x,y,credit,caption) {
	var obj = new Object();

	obj.src = sect + "/" + filename;
	obj.width = x;
	obj.height = y;
	obj.credit = credit;
	obj.caption = caption;

	if (! ads[sect]) ads[sect] = new Array();
	ads[sect][ads[sect].length] = obj;
}


var n = 0;  // global counter for current image number
var adcounter = 0;
var adcount = 6; // ad call 
var adcall = 6; //number to call in ad

//right ad//
var sponsor_link = 'http://www.replayphotos.com';
var sponsor = '<img src="http://graphics.collegesports.com/graphics/heisman/ad_replay107x151.png" width="107" height="151" border="0">';

var adperiod=6;


// grab variables via GET
function getQueryVariable(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	}
}