// JavaScript Document

var activeID = 0;
var activeFeaturette = 0;
var features = new Array();
var featuretteIDs = new Array();
var totalFeatures = 0;
var totalFeaturettes = 0;
window.onload = setFeatures;

/* Used to fix a problem with IE's DOM handler hanging onload event */
function pageInit(alertVar){ 	
	var inputId = setTimeout("classNaming();", 100);
	if(alertVar != '')
		alert(alertVar);
}
function classNaming(){			
	if(document.body.clientWidth > 950){
		if(document.getElementById('body_slim') != null)
			document.getElementById('body_slim').id = 'body';		
	} else {		
		if(document.getElementById('body') != null)
			document.getElementById('body').id = 'body_slim';	
	}	
	return false;
}

function setFeatures(){
	if(ifExists(document.getElementById('featurettes'))){
		var featurettes = document.getElementById('featurettes');
		var featurettesInputs = featurettes.getElementsByTagName("INPUT");
		var inputCount = featurettesInputs.length;
		for(var i=0; i<inputCount; i++){
			if(featurettesInputs[i].className == 'featID'){
				featuretteIDs[featuretteIDs.length] = featurettesInputs[i].value;
			}
		}		
		totalFeaturettes = featuretteIDs.length;
	}
	
	if(ifExists(document.getElementById('topthird'))){
		var topThird = document.getElementById('topthird');
		var topThirdInputs = topThird.getElementsByTagName("INPUT");
		inputCount = topThirdInputs.length;
		for(var i=0; i<inputCount; i++){
			if(topThirdInputs[i].className == 'idHolder'){
				features[features.length] = topThirdInputs[i].value;
			}
		}
		totalFeatures = features.length;
		setTimeout("rotateFeature();", 8000);
	}
}

function ifExists(domObject){
	if(typeof(domObject) != "undefined" && domObject != null)
		return true;
	else
		return false;
}

function rotateFeaturette(direction){
	if(totalFeaturettes > 1){
		direction = parseInt(direction);
		activeFeaturette += direction;
		
		if(activeFeaturette == totalFeaturettes) activeFeaturette = 0;
		else if(activeFeaturette < 0) activeFeaturette = (totalFeaturettes - 1);
		var videoInfo = document.getElementById('video_' + featuretteIDs[activeFeaturette]).value;
		
		document.getElementById('featuretteImg').src = document.getElementById('img_' + featuretteIDs[activeFeaturette]).value;
		document.getElementById('featuretteLink').href = document.getElementById('link_' + featuretteIDs[activeFeaturette]).value;
		document.getElementById('featuretteEventLink').href = document.getElementById('link_' + featuretteIDs[activeFeaturette]).value;
		document.getElementById('featuretteEventLink').innerHTML = document.getElementById('artist_' + featuretteIDs[activeFeaturette]).value;
		document.getElementById('featuretteVenue').innerHTML = document.getElementById('venue_' + featuretteIDs[activeFeaturette]).value;
		document.getElementById('featuretteText').innerHTML = stripslashes(document.getElementById('text_' + featuretteIDs[activeFeaturette]).value);
		if(videoInfo.length > 0){			
			swfobject.embedSWF("http://www.youtube.com/v/" + videoInfo + "&hl=en", flashholder, flashwidth, flashheight, flashversion, flashvars , params, attributes);
			    
			/*
			document.getElementById('featuretteVideo').innerHTML = "<object width=\"231\" height=\"175\"><param name=\"movie\" value=\"\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"http://www.youtube.com/v/" + videoInfo + "&hl=en\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"231\" height=\"175\"></embed></object>";
			*/
			document.getElementById('featuretteMedia').className = 'hidden';
			document.getElementById('featuretteVideoWrapper').className = '';
		} else {
			document.getElementById('featuretteMedia').className = '';
			document.getElementById('featuretteVideoWrapper').className = 'hidden';			
		}
	}
}

function rotateFeature(){
	if(totalFeatures > 1){
		activeID++;
		if(activeID == totalFeatures) activeID=0;
		
		document.getElementById('featureImageSrc').src = "/media/images/featured/" + features[activeID] + "_feature_onsale.jpg";
		document.getElementById('eventLink').href = "/event/" + features[activeID] + "/" + document.getElementById('slug_' + features[activeID]).value + "/"; 
		document.getElementById('purchaseTickets').href = document.getElementById('site_' + features[activeID]).value;
		
		/*
		document.getElementById('saveToList').setAttribute("onclick", "addEvent('" + features[activeID] + "', this);return false;");
		document.getElementById('sendInvite').setAttribute("onClick", "showInvite('" + features[activeID] + "', this);return false;");		
		*/
		document.getElementById('saveToList').onclick = function(){ addEvent(features[activeID], this); return false;}
		document.getElementById('sendInvite').onclick = function(){ showInvite(features[activeID], this);return false;}
	}
	setTimeout("rotateFeature();", 8000);
}

/*Buzz Rotation*/
function buzzRotate(direction){
	var buzzStories = document.getElementById('buzzContent').getElementsByTagName("DIV");
	var started = false;
	var buzzCount = 0;	
	var buzzLength = buzzStories.length;
	var found = false;
	var firstBuzz = 0;
	var nextViewed = 0;
	var lastViewed = 0;
	var buzzEntryCount = 0;
	//find first hidden
	for(var i=0; i < buzzLength; i++){			
		if(buzzStories[i].className.indexOf('anchor') < 0){
			if (buzzStories[i].className.indexOf('buzzEntry') >= 0) { buzzEntryCount++; }
			if((found != true) && (buzzStories[i].className.indexOf('hidden') < 0)) {
				firstBuzz = buzzEntryCount;
				found = true;
			} else if (buzzStories[i].className.indexOf('hidden') >= 0) {
				buzzStories[i].className = buzzStories[i].className.replace(new RegExp(" hidden\\b"), "");
			}
			buzzStories[i].className += ' hidden';
		}
	}
	
	if(direction == 'forward'){
		nextViewed = (firstBuzz + 3);
		if(nextViewed > buzzEntryCount) nextViewed = 0;
	} else {
		nextViewed = (firstBuzz - 3);
		if(nextViewed < 0) nextViewed = ((Math.ceil((buzzEntryCount - 3) / 3) * 3) + 1);
	}
		
	if(nextViewed > buzzEntryCount) nextViewed = 0;
	lastViewed = nextViewed + 2;
	if(lastViewed > buzzEntryCount) lastViewed = buzzEntryCount;
	
	
	var selectedNum = Math.floor(nextViewed / 3);
		
	var buzzNumber = document.getElementById('slideNav').getElementsByTagName("SPAN");	
	for( var i=0; i < buzzNumber.length; i++){
		buzzNumber[i].className = buzzNumber[i].className.replace(new RegExp(" selPage\\b"), "");		
	}	
	document.getElementById('buzzNumber' + selectedNum).className += ' selPage';
	var count = lastViewed - nextViewed;	
	var counter = 0;
	var c = 0;

	for(var i=0; i < buzzLength; i++){			
		if(buzzStories[i].className.indexOf('anchor') < 0){
			
			if (buzzStories[i].className.indexOf('buzzEntry') >= 0) 
				counter++;
				
			if ((buzzStories[i].className.indexOf('hidden') >= 0) &&  (c <= count) && (counter >= nextViewed)){				
				buzzStories[i].className = buzzStories[i].className.replace(new RegExp(" hidden\\b"), "");
				c++;
			}			
		}
	}
	/*
	if(buzzStories.length > 0){
		while (buzzCount==0){
			for(var i=0; i<buzzStories.length; i++){	
				//alert(buzzStories[i].className + " - " + buzzStories[i].className.indexOf('hidden'));
				if(buzzStories[i].className.indexOf('anchor') < 0){
					if(buzzStories[i].className.indexOf('hidden') < 0){					
						started = true;
						buzzStories[i].className += ' hidden';									
					} else if((started == true) && (buzzCount < 3) && (buzzStories[i].className.indexOf('hidden') >= 0)) {
						buzzStories[i].className = buzzStories[i].className.replace(new RegExp(" hidden\\b"), "");		
						buzzCount++;					
					}
				}
			}			
		}
	}
	*/
}


function stripslashes( str ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Ates Goral (http://magnetiq.com)
    // +      fixed by: Mick@el
    // +   improved by: marrtins
    // +   bugfixed by: Onno Marsman
    // +   improved by: rezna
    // *     example 1: stripslashes('Kevin\'s code');
    // *     returns 1: "Kevin's code"
    // *     example 2: stripslashes('Kevin\\\'s code');
    // *     returns 2: "Kevin\'s code"
 
    return (str+'').replace(/\0/g, '0').replace(/\\([\\'"])/g, '$1');
}

         ////////////////////////////////////////////////////////////////////////////////
         // Video window function, handles urls, sets up embeds, makes window visible/invisible
         ////////////////////////////////////////////////////////////////////////////////

         function playVid(vidId, noBlackout) {

            ////////////////////////////////////////////////////////////////////////////////
            // Test to see if our video and blackout divisions have been set up.
            // If no division was declared in the HTML then we'll go ahead and automatically
            // create and style the necessary division.
            ////////////////////////////////////////////////////////////////////////////////

            if (_vidPane==null) {
               ////////////////////////////////////////////////////////////////////////////////
               /// Video Pane wasn't set up so see if it's declared in the HTML
               ////////////////////////////////////////////////////////////////////////////////
               _vidPane=document.getElementById('vidPane');
               if (!_vidPane) {
                  // user didn't create the division so create it for him.
                  // also asume no styleSheet and set basic styles.
                  var tbody = document.getElementsByTagName("body")[0];
                  tnode = document.createElement('div');
                  tnode.id='vidPane';
                  tnode.className='vidFrame';
                  tbody.appendChild(tnode);
                  _vidPane=document.getElementById('vidPane');
                  _vidPane.style.position='absolute';
                  _vidPane.style.display='none';
                  _vidPane.style.backgroundColor='#FFFFFF';
                  _vidPane.style.border='1px solid #800000';
                  _vidPane.style.width='435px';
                  _vidPane.style.height='382px';
                  _vidPane.style.cursor='move';
                  _vidPane.style.fontFamily='verdana';
                  _vidPane.style.fontSize='9pt';
                  _vidPane.style.zIndex='100';
                  _vidPane.style.MozBorderRadius='10';
               }

               // If _vidPane wasn't defined then for sure _blackout wasn't
               // so see if it was defined in the HTML
               _blackout=document.getElementById('blackout');

               if (!_blackout) {
                  // user didn't create the division so create it for him.
                  // also asume no styleSheet and set basic styles.
                  tnode = document.createElement('div');
                  tnode.id='blackout';
                  tbody.appendChild(tnode);
                  _blackout=document.getElementById('blackout');
                  _blackout.style.position='absolute';
                  _blackout.style.display='none';
                  _blackout.style.left='0px';
                  _blackout.style.top='0px';
                  _blackout.style.backgroundColor='#555555';
                  _blackout.style.opacity='.9';
                  _blackout.style.filter='alpha(opacity=90)';
                  _blackout.style.zIndex='50';
               }
               // Initialize the starting location of the video.
               _vidPane.style.top='75px';    // Starting location horozontal
               _vidPane.style.left='75px';   // Starting location verticle
            }

            // Shows (or hides) the vidPane layer.   Accepts 2 parameteres.
            // vidId is null (close window) or an anchor object (contains HREF value)
            // vidId is mandatory example: <a href="someservice.com/somevideo.swf" onClick='return playVid(this);'></a>
            // noBlackout is optional. If you pass true, the background will not be "greyed out".

            if (vidId==null) { 
               // Null is passed by the "close" link, so we'll hide the layer.
               _vidPane.style.display='none';         // Hide the division.
               _vidPane.innerHTML='';                 // purge it's html (kill video)
               _blackout.style.display='none';        // Hide the blackout layer.
            } else {
               // Snag the url from the passed object
               vidId=vidId.href;

               // Next three lines make the blackout layer visible
               // and makes sure it covers the entire page.
               if (!noBlackout) {
                  _blackout.style.width='100%';
                  _blackout.style.height=(document.body.offsetHeight<screen.height) ? screen.height+'px' : document.body.offsetHeight+20+'px'; 
                  _blackout.style.display='block';
               } else {
                  _blackout.style.display='none';        // Hide the blackout layer.
               }
               // Break out the URL passed to this function (so vidInfo[0]=http, [1]=domain, etc
               var vidInfo = vidId.split('/');

               // We're building a temporary string called vidstring. 
               // vidstring will hold the HTML as we build it based on the service
               // being used.  The next few lines contains items which are common
               // to all the services, or at least ignored if not directly used.
               
               var vidstring ='&nbsp;<A HREF="'+vidId+'">LINK</A>';
               vidstring+='&nbsp;/&nbsp;<A HREF="#" onClick="return(playVid())">CLOSE</A><BR>';
               vidstring+='<center><embed style="margin-top: 5px;"';  
               vidstring+=' enableJavascript="false" allowScriptAccess="never"';
               vidstring+=' allownetworking="internal" type="application/x-shockwave-flash"';
               vidstring+=' wmode="transparent" pluginspage="http://www.macromedia.com/go/getflashplayer" ';
			   
			   /*<object width="425" height="355"><param name="movie" value="http://www.youtube.com/v/uHmhmS84HaM&rel=1"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/uHmhmS84HaM&rel=1" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"></embed></object>*/
			   

               if (vidInfo[2].indexOf('youtube.com')>=0) {
                  ////////////////////////////////////////////////////////////////////////////////
                  // YouTube (Use browser URL, autoplays)
                  ////////////////////////////////////////////////////////////////////////////////
                  vidInfo=vidId.match(/v=.+$/);
                  vidInfo=String(vidInfo).replace(/v=/g,'');
                  vidstring+=' src="http://www.youtube.com/v/'+vidInfo+'&autoplay=1" ';
                  vidstring+=' height="350" width="425"></embed></center>';
               } else if (vidInfo[2].indexOf('video.google.com')>=0) {
                  ////////////////////////////////////////////////////////////////////////////////
                  // Google (Use browser URL, autoplays)
                  ////////////////////////////////////////////////////////////////////////////////
                  vidInfo=vidId.match(/docid=.+$/);
                  vidInfo=String(vidInfo).replace(/docid=/g,'');
                  vidstring+='  src="http://video.google.com/googleplayer.swf?docId='+vidInfo+'&autoplay=1" ';
                  vidstring+=' height="350" width=425"></embed></center>';
               } else if (vidInfo[2].indexOf('metacafe.com')>0) {
                  ////////////////////////////////////////////////////////////////////////////////
                  // MetaCafe (Use browser URL, autoplays)
                  ////////////////////////////////////////////////////////////////////////////////
                  vidInfo=vidId.match(/watch.+$/);
                  vidInfo=String(vidInfo).replace(/watch./,'');
                  vidInfo=String(vidInfo).replace(/.$/,'');
                  vidstring+=' flashVars="playerVars=autoPlay=yes" ';
                  vidstring+=' src="http://www.metacafe.com/fplayer/'+vidInfo+'.swf" ';
                  vidstring+=' width="400" height="345">';  
                  vidstring+='</embed></center>';
               } else if (vidInfo[2].indexOf('ifilm.com')>=0) {
                  ////////////////////////////////////////////////////////////////////////////////
                  // iFilm (Use browser URL, autoplays)
                  ////////////////////////////////////////////////////////////////////////////////
                  vidInfo=vidId.match(/video.+$/);
                  vidInfo=String(vidInfo).replace(/video./,'');
                  vidstring+=' flashVars="flvbaseclip='+vidInfo+'&ip=true" ';
                  vidstring+=' src="http://ifilm.com/efp" quality="high" name="efp" align="middle" ';
                  vidstring+=' width="425" height="350">';  
                  vidstring+='</embed></center>';
               } else if (vidInfo[2].indexOf('dailymotion.com')>=0) {
                  ////////////////////////////////////////////////////////////////////////////////
                  // Daily Motion (Use EMBED URL, autoplays)
                  ////////////////////////////////////////////////////////////////////////////////
                  vidstring+=' src="'+vidId+'" flashVars="autoStart=1" ';
                  vidstring+=' width="425" height="334">';
                  vidstring+='</embed></center>';
               } else if (vidInfo[2].indexOf('break.com')>=0) {
                  ////////////////////////////////////////////////////////////////////////////////
                  // Break (use EMBED URL, autostarts)
                  ////////////////////////////////////////////////////////////////////////////////
                  vidstring+=' src="'+vidId+'&autoplay=1" ';
                  vidstring+=' width="425" height="350">';
                  vidstring+='</embed></center>';
               } else if (vidInfo[2].indexOf('shoutfile.com')>=0) {
                  ////////////////////////////////////////////////////////////////////////////////
                  // Shoutfile (use EMBED URL, does not autostart)
                  ////////////////////////////////////////////////////////////////////////////////
                  vidstring+=' src="'+vidId+'"';
                  vidstring+=' width="400" height="300">';
                  vidstring+='</embed></center>';
               } else if (vidInfo[2].indexOf('soapbox.msn.com')>=0) {
                  ////////////////////////////////////////////////////////////////////////////////
                  // MSN Soapbox (use the LINK, autostarts)
                  ////////////////////////////////////////////////////////////////////////////////
                  vidInfo=vidId.match(/vid=.+$/);
                  vidInfo=String(vidInfo).replace(/vid=/g,'');
                  vidstring+=' src="http://images.soapbox.msn.com/flash/soapbox1_1.swf" ';
                  vidstring+=' flashvars="c=v&ap=true&v='+vidInfo+'" ';
                  vidstring+=' height="360" width="412"></embed></center>';
               } else if (vidInfo[2].indexOf('atomfilms.com')>=0) {
                  ////////////////////////////////////////////////////////////////////////////////
                  // AtomFilms (use URL, does not autostart), pretty lame embed service IMHO
                  ////////////////////////////////////////////////////////////////////////////////
                  vidInfo=vidId.match(/film\/.+(\.jsp)/);
                  vidInfo=String(vidInfo[0]).replace(/film\//g,'');
                  vidInfo=String(vidInfo).replace(/\.jsp/g,'');
                  vidstring+=' src="http://www.atomfilms.com:80/a/autoplayer/shareEmbed.swf?keyword='+vidInfo+'" ';
                  vidstring+=' height="350" width="425" autostart="true"></embed></center>';
               } else {
                  ////////////////////////////////////////////////////////////////////////////////
                  // Failed.
                  ////////////////////////////////////////////////////////////////////////////////
                  vidstring += '></embed><BR><BR><BR>Unknown video service.</center>';
               } 
               // Insert our HTML and display the video window.
               _vidPane.innerHTML=vidstring;
               // Set the Y position of the video window so it's in the visible clip
               var scrollTop = 0;
               if (document.documentElement && document.documentElement.scrollTop)
	                scrollTop = document.documentElement.scrollTop;
               else if (document.body)
	                scrollTop = document.body.scrollTop
               _vidPane.style.top=scrollTop+50+'px';
               // Video window was hidden so we'll make it visible
               _vidPane.style.display='block'; 
            }
            // return false so the browser won't follow through with A HREF clicks.
            return(false);
         }

         ////////////////////////////////////////////////////////////////////////////////
         // Drag and Grab handlers
         ////////////////////////////////////////////////////////////////////////////////

         function moveHandler(e){
            // Called automatically whenever the mouse is moved after a drag event starts
            if (e == null) { e = window.event }  // Get event data, if it wasn't passed, get it IE style.
            if ( _dragOK ){                      // is our global var set to true? is it ok to move the object?
               _savedTarget.style.left=e.clientX-_dragXoffset+'px';  //OK to move, calculate the offset and move it
               _savedTarget.style.top=e.clientY-_dragYoffset+'px';   // calculate the y offset and move it.
               return false;                                         // return false so browser doesn't try to do anything else.
            }                                   // End _dragOK check
         }                                      // End moveHandler
      
         function cleanup(e) {
            // Called whenever user lets up off a mouse button after a drag event starts
            document.onmousemove=null;                     // Turn off the mousemove event (won't call moveHandler() now).
            document.onmouseup=null;                       // Turn off the mouseup event (won't call cleanup() now).                     
            _savedTarget.style.cursor=_orgCursor;          // Restore original mouse shape
            _dragOK=false;                                 // Turn off the global constant we look for before moving stuff.
         }
      
         function dragHandler(e){
            // Called automatically when user holds down the mouse button
            var cursorType='-moz-grabbing';                               // Set mouse type to grabbing hand
            if (e == null) { e = window.event; cursorType='move';}        // This is IE so get event info IE style
            var target = e.target != null ? e.target : e.srcElement;      // Save object of the event
            if (target.className=="vidFrame") {                           // Did mouse go down over our dragable object?
               _orgCursor=target.style.cursor;                            // Remember the current mouse shape
               _savedTarget=target;                                       // Remember the object we're working with
               target.style.cursor=cursorType;                            // change mouse to "grab" icon                             
               _dragOK=true;                                              // When true, movehandler will move the window
               _dragXoffset=e.clientX-parseInt(_savedTarget.style.left);  // Remember current X offset
               _dragYoffset=e.clientY-parseInt(_savedTarget.style.top);   // Remember current Y offset
               document.onmousemove=moveHandler;                          // Call moveHandler() when mouse moves
               document.onmouseup=cleanup;                                // Call cleanup() when user lets go of mouse btn
               return false;                                              // IMPORTANT return false so browser doesn't do anything else.
            }                                                             // End Click on classname = object check
         }                                                                // End function dragHandler

         // Start the event handler. When mouse is clicked, call dragHandler()	 
      	 document.onmousedown=dragHandler;

         // Initialize global variables.
         var _savedTarget=null;        // The target layer (effectively vidPane)
         var _orgCursor=null;          // The original Cursor (mouse) Style so we can restore it
         var _dragOK=false;            // True if we're allowed to move the element under mouse
         var _dragXoffset=0;           // How much we've moved the element on the horozontal
         var _dragYoffset=0;           // How much we've moved the element on the verticle
         var _vidPane = null;          // Video Layer -- won't be defined until a video is called
         var _blackout= null;          // blackout Layer. -- won't be defined until a video is called.
 
