/////////////////// Hide js_disabled divs and reveal js_enabled divs \\\\\\\\\\\\\\\\\\\
var divs = document.getElementsByTagName('div');
for (var i=0;i<divs.length;i++)
{
	if(divs[i].className == "js_disabled")
	{
		divs[i].style.display='none';
		divs[i].innerHTML = '';
	}
	else if(divs[i].className == "js_enabled")
	{
		divs[i].style.display='block';
	}
}

$('window_background').setAttribute("onClick", "CloseWindow('parent')");
$('window_container').setAttribute("onMouseOver", "overwindow = true;");
$('window_container').setAttribute("onMouseOut", "overwindow = false;");

///////////////// Adjust the width of the site for the ad space \\\\\\\\\\\\\\
$('container').style.width = ($('test').offsetWidth - 160)+'px';
$('window_background').style.width = ($('test').offsetWidth - 160)+'px';
$('ad_space').style.left = ($('test').offsetWidth - 160)+'px';

///////////////// Adjust the heights and position of the border and footer
$('border').style.height = ($('test').offsetHeight - 65)+'px';
$('content').style.width = ($('border').offsetWidth - 5)+'px';
$('content').style.height = ($('border').offsetHeight - 10)+'px';
$('browse').style.left = ($('margin').offsetWidth + 3)+'px';
$('browse').style.width = ($('content').offsetWidth - ($('margin').offsetWidth + 8))+'px';
// $('restore_session').style.width = ($('test').offsetWidth - 160)+'px';
$('footer').style.width = ($('test').offsetWidth - 160)+'px';
$('tabs_for_picks_completed').style.width = ($('test').offsetWidth - 160)+'px';

///////////////// Reset the search input bar \\\\\\\\\\\\
$('input_style').value = 'Search for a tab';
$('input_style').className = 'black_input';
$('input_search_style').style.paddingLeft = '0px';
$('input_clear_style').style.display = 'none';

//////////////////// Remove any query strings from the url left over from a js disabled session \\\\\\\\\\\\\\\\\\\\\\\ 
var url = document.location.href;

var hashsplit = url.split("#");
// var jsdisabledsplit = hashsplit[0].split("http://www.zojam.com/?");
// var cesplit = hashsplit[0].split("http://www.zojam.com/?ce=");
// var tokensplit = hashsplit[0].split("http://www.zojam.com/?token=");
var jsdisabledsplit = hashsplit[0].split("http://localhost/?");
var cesplit = hashsplit[0].split("http://localhost/?ce=");
var tokensplit = hashsplit[0].split("http://localhost/?token=");
var querysplit = '';

// Prevent jsdisabled redirect unless the get variables are for account verification
if(jsdisabledsplit.length > 1 && cesplit.length <= 1 && tokensplit.length <= 1)
{
	alert('here');

	querysplit = jsdisabledsplit[1].split("&");

	var ctquery = "";
	var aquery = "";
	var squery = "";
	var tquery = "";
	var wquery = "";

	for(var i=0; i < querysplit.length; i++)
	{
		var variablesplit = querysplit[i].split("=");
		
		switch(variablesplit[0])
		{
			case "ct":
			{
				switch(variablesplit[1])
				{
					case "p":
					{
						ctquery = "popular";
						break;
					}
					case "r":
					{
						ctquery = "recent";
						break;
					}
					case "b":
					{
						ctquery = "browse";
						break;
					}
					case "s":
					{
						ctquery = "search";
						break;
					}
				}
				break;
			}
			case "a":
			{
				aquery = variablesplit[1];
				break;
			}
			case "s":
			{
				squery = variablesplit[1];
				break;
			}
			case "t":
			{
				tquery = variablesplit[1];
				break;
			}
			case "w":
			{
				// TODO: the sign up window text is different with js enabled and disabled
				wquery = (variablesplit[1] == "signup") ? "sign_up" : variablesplit[1];
				break;
			}
		}
	}

	var jsenabledhash = "";
	
	if(wquery != "")
	{
		jsenabledhash += "/window/"+wquery+"/";
	}
	else if(ctquery != "")
	{
		jsenabledhash += "/"+ctquery+"/";
		if(aquery != "")
		{
			jsenabledhash += aquery+"/";
			if(squery != "")
			{
				jsenabledhash += squery+"/";
				if(tquery != "")
				{
					jsenabledhash += tquery+"/";
				}
			}
		}
	}

	document.location.href = "http://www.zojam.com/#!"+jsenabledhash;
}

var accepts = new Array();

if(document.draggableform)
{
	for(var i = 0; i < document.draggableform.elements.length; i++)
	{
		accepts.push(document.draggableform.elements[i].value);
		
		new Draggable(''+document.draggableform.elements[i].value+'', {
			revert: true,
			superghosting: true,
			onDrag: function(draggable, event) {
				disableBrowsing();
				
				if(jQuery.browser.mozilla)
				{
					document.body.style.cursor = '-moz-grabbing';
				}
				else
				{
					document.body.style.cursor = "url(closedhand.cur), url('cursor/closedhand.cur'), default";
				}
				
				// if your not hovering over a droppable
				if(!droppablehover)
				{
					$('mouse_error').style.display = 'block';
					$('mouse_plus').style.display = 'none';
					
					var posx = 0;
					var posy = 0;
					if (event.pageX || event.pageY) 
					{
						posx = event.pageX;
						posy = event.pageY;
						
						//alert("pageX, pageY: "+posx+", "+posy);
					}
					else if (event.clientX || event.clientY) 	
					{
						posx = event.clientX + document.body.scrollLeft
							+ document.documentElement.scrollLeft;
						posy = event.clientY + document.body.scrollTop
							+ document.documentElement.scrollTop;
							
						//alert("clientX, clientY: "+posx+", "+posy);
					}
					$('mouse_error').style.left = posx+'px';
					$('mouse_error').style.top = (posy+10)+'px';
				}
				// if you are hovering over a droppable
				else
				{
					$('mouse_error').style.display = 'none';
					$('mouse_plus').style.display = 'block';
					
					var posx = 0;
					var posy = 0;
					if (event.pageX || event.pageY) 
					{
						posx = event.pageX;
						posy = event.pageY;
					}
					else if (event.clientX || event.clientY) 	
					{
						posx = event.clientX + document.body.scrollLeft
							+ document.documentElement.scrollLeft;
						posy = event.clientY + document.body.scrollTop
							+ document.documentElement.scrollTop;
					}

					$('mouse_plus').style.left = posx+'px';
					$('mouse_plus').style.top = (posy+10)+'px';
				}
			},
			onEnd: function(draggable) {
				enableBrowsing();
				
				document.body.style.cursor = 'default';
				
				$('mouse_error').style.display = 'none';
				$('mouse_plus').style.display = 'none';
			
				var divid = draggable.element.id;
				divid = divid.replace(/clone_,*\)*/g,"");
				
				var oddoreven = draggable.element.id;
				oddoreven = oddoreven.replace(/clone_draggable_,*\)*/g,"");
				
				if(oddoreven == 0 || ((oddoreven % 2) == false ))
				{
					$(divid).style.backgroundColor='#ffffff';
				}
				else
				{
					$(divid).style.backgroundColor='#dddddd';
				}
				
				//$('jamlist_add_'+oddoreven+'').style.visibility = 'hidden';
			}
		});
	}
}

var loopthrough = $('hidden_jamlist').value;
for(var i=0; i < loopthrough; i++)
{
	containerMap.insert('jamlist-'+$('droppable_'+i+'').className, new contentContainer('jamlist_content.php', '-', '-', '', '-', '', '', 'initial', 'initial', '', '', 0, 1));
	
	Droppables.add('droppable_'+i+'', {
		accept: accepts,
		hoverclass: 'hover',
		onHover: function (dragged, event) {
			// indicate that you are actively hovering then assume you are not after 50 milliseconds (because there is no function on exiting hovering)
			droppablehover=true;
			setTimeout("droppablehover=false", 50);
		},
		onDrop: function(dragged, dropped, event) {
			var droppedid = dropped.id;
			
			var draggedid = dragged.id;
			draggedid = draggedid.replace(/clone_,*\)*/g,"");
			
			$(''+droppedid+'').highlight();
			
			var resource = $(''+draggedid+'_resource').className;
			var jamlistid = $(''+droppedid+'_id').className;
			//var jamlistid = $(''+droppedid+'').className;
			
			//jamlistid = jamlistid.replace(/ hover,*\)*/g,"");
			
			//alert('jamlistid: '+jamlistid+', resource: '+resource);
			addToJamlist(''+jamlistid+'', ''+resource+'');
		}
	});
}

//  DragDropExtra Scriptaculous Enhancement, version 0.5
//  (c) 2007-2008 Christopher Williams, Iterative Designs
//
// v0.5 release
//      - Fixed bug where 2nd drag on an element in IE would result in funny placement of the
//        element. [shammond42]
// v0.4 release
//		- Fixed issue with dragging and dropping in IE7 due to an exception being thrown and not properly reseting in FinishDrag.
// v0.3 release
//	  - Fixed bug found by Phillip Sauerbeck psauerbeck@gmail. Tests added based on Phillip's efforts.
// v0.2 release
//		- Minor bug fix for the releasing of objects after they have been dropped, prevents memory leak.
// v0.1 release
//		- initial release for the super ghosting capability
//		- Drags from one scrolling list to the other (overflow:auto)
//		- Retains the original object so that it can remain present despite being dragged
// 
// dragdropextra.js is freely distributable under the terms of an MIT-style license.
// For details, see the Iterative Designs web site: http://www.iterativedesigns.com/
// Parts of this code have been taken from the original dragdrop.js library which is 
// copyrighted by (c) 2005-2007 Thomas Fuchs (http://script.aculo.us, 
// http://mir.aculo.us) and (c) 2005-2007 Sammi Williams 
// (http://www.oriontransfer.co.nz, sammi@oriontransfer.co.nz) and available under 
// a MIT-style license.

Draggable.prototype.startDrag = function(event) {
  this.dragging = true;
  if(!this.delta)
	this.delta = this.currentDelta();
  
  if(this.options.zindex) {
	this.originalZ = parseInt(Element.getStyle(this.element,'z-index') || 0);
	this.element.style.zIndex = this.options.zindex;
  }
  
  if(this.options.ghosting) {
	this._clone = this.element.cloneNode(true);
	this.element._originallyAbsolute = (this.element.getStyle('position') == 'absolute');
	if (!this.element._originallyAbsolute)
	  Position.absolutize(this.element);
	this.element.parentNode.insertBefore(this._clone, this.element);
  }
  
	if(this.options.superghosting) {
		Position.prepare();
		var pointer = [Event.pointerX(event), Event.pointerY(event)];
		body = document.getElementsByTagName("body")[0];
		me = this.element;
		this._clone = me.cloneNode(true);
		if (Prototype.Browser.IE) {
			// Clear event handing from the clone
			// Solves the second drag issue in IE
			this._clone.clearAttributes();
			this._clone.mergeAttributes(me.cloneNode(false));
		}
		me.parentNode.insertBefore(this._clone, me);
		me.id = "clone_"+me.id;
		me.hide();

		Position.absolutize(me);
		me.parentNode.removeChild(me);
		body.appendChild(me);
		//Retain height and width of object only if it has been nulled out.  -v0.3 Fix
		if (me.style.width == "0px" || me.style.height == "0px")	{
		me.style.width=Element.getWidth(this._clone)+"px";
		me.style.height=Element.getHeight(this._clone)+"px";
		}

		//overloading in order to reduce repeated code weight.
		this.originalScrollTop = (Element.getHeight(this._clone)/2);

		this.draw(pointer);
		me.show();
	}

  if(this.options.scroll) {
	if (this.options.scroll == window) {
	  var where = this._getWindowScroll(this.options.scroll);
	  this.originalScrollLeft = where.left;
	  this.originalScrollTop = where.top;
	} else {
	  this.originalScrollLeft = this.options.scroll.scrollLeft;
	  this.originalScrollTop = this.options.scroll.scrollTop;
	}
  }
  
  Draggables.notify('onStart', this, event);
	  
  if(this.options.starteffect) this.options.starteffect(this.element);
}




Draggable.prototype.draw = function(point) {
	  var pos = Position.cumulativeOffset(this.element);
	  if(this.options.ghosting) {
		var r   = Position.realOffset(this.element);
		pos[0] += r[0] - Position.deltaX; 
		pos[1] += r[1] - Position.deltaY;
	  }
  
	  var d = this.currentDelta();
	  pos[0] -= d[0]; 
	  pos[1] -= d[1];
  
	  if(this.options.scroll && (this.options.scroll != window && this._isScrollChild)) {
		pos[0] -= this.options.scroll.scrollLeft-this.originalScrollLeft;
		pos[1] -= this.options.scroll.scrollTop-this.originalScrollTop;
	  }
  
	  var p = [0,1].map(function(i){ 
		return (point[i]-pos[i]-this.offset[i]) 
	  }.bind(this));

		if(this.options.snap) {
		  if(Object.isFunction(this.options.snap)) {
			p = this.options.snap(p[0],p[1],this);
		  } else {
		  if(Object.isArray(this.options.snap)) {
			p = p.map( function(v, i) {
			  return (v/this.options.snap[i]).round()*this.options.snap[i] }.bind(this))
		  } else {
			p = p.map( function(v) {
			  return (v/this.options.snap).round()*this.options.snap }.bind(this))
		  }
		}}

	if (this.options.superghosting)	{	
		p[1] = point[1] - this.originalScrollTop;
	}



	var style = this.element.style;
	if((!this.options.constraint) || (this.options.constraint=='horizontal'))
	  style.left = p[0] + "px";
	if((!this.options.constraint) || (this.options.constraint=='vertical'))
	  style.top  = p[1] + "px";
	
	if(style.visibility=="hidden") style.visibility = ""; // fix gecko rendering
}

Draggable.prototype.initDrag = function(event) {
  if(!Object.isUndefined(Draggable._dragging[this.element]) &&
	Draggable._dragging[this.element]) return;
  if(Event.isLeftClick(event)) {    
	// abort on form elements, fixes a Firefox issue
	var src = Event.element(event);
	if((tag_name = src.tagName.toUpperCase()) && (
	  tag_name=='INPUT' ||
	  tag_name=='SELECT' ||
	  tag_name=='OPTION' ||
	  tag_name=='BUTTON' ||
	  tag_name=='TEXTAREA')) return;
	  
	var pointer = [Event.pointerX(event), Event.pointerY(event)];
	var pos     = Position.cumulativeOffset(this.element);
	this.offset = [0,1].map( function(i) { return (pointer[i] - pos[i]) });
	
	Draggables.activate(this);
	Event.stop(event);
  }
}

Droppables.isAffected = function(point, element, drop) {
	Position.prepare();
	positioned_within = Position.withinIncludingScrolloffsets(drop.element, point[0], point[1])
	return (
	  (drop.element!=element) &&
	  ((!drop._containers) ||
		this.isContained(element, drop)) &&
	  ((!drop.accept) ||
		(Element.classNames(element).detect( 
		  function(v) { return drop.accept.include(v) } ) )) && positioned_within );


}

Draggable.prototype.finishDrag = function(event, success) {
  this.dragging = false;
  
  if(this.options.quiet){
	Position.prepare();
	var pointer = [Event.pointerX(event), Event.pointerY(event)];
	Droppables.show(pointer, this.element);
  }

  if(this.options.ghosting) {
	if (!this.element._originallyAbsolute)
	  Position.relativize(this.element);
	delete this.element._originallyAbsolute;
	Element.remove(this._clone);
	this._clone = null;
  }

  var dropped = false; 
  if(success) { 
	dropped = Droppables.fire(event, this.element); 
	if (!dropped) dropped = false; 
  }
  if(dropped && this.options.onDropped) this.options.onDropped(this.element);
  Draggables.notify('onEnd', this, event);

  var revert = this.options.revert;
  if(revert && Object.isFunction(revert)) revert = revert(this.element);
  
  var d = this.currentDelta();
  if(revert && this.options.reverteffect) {
	if (dropped == 0 || revert != 'failure')
	  this.options.reverteffect(this.element,
		d[1]-this.delta[1], d[0]-this.delta[0]);
  } else {
	this.delta = d;
  }

  if(this.options.zindex)
	this.element.style.zIndex = this.originalZ;

  if(this.options.endeffect) 
	this.options.endeffect(this.element);
	

	if(this.options.superghosting) {
		body = document.getElementsByTagName("body")[0];
	  Element.remove(this.element);
		new Draggable(this._clone, this.options);
	}


  Draggables.deactivate(this);
  Droppables.reset();
}
