var tagsArray;

var lastPopulatedBox = 0;
var numLinkColumns = 3;
var uiLinkObj = new uiLinks();

function linkColumns( )
{
	//this.col = new Array( numLinkColumns );
	
	this.col1.tagId = null;
	this.col2.tagId = null;
	this.col3.tagId = null;
	
	this.getColToPop = function( ) {
		if( this.col1.tagId == null ) return 1;
		if( this.col2.tagId == null ) return 2;
		if( this.col3.tagId == null ) return 3;
	}
	
}

function uiLinks( )
{
	this.col1Popped = false;
	this.col2Popped = false;
	this.col3Popped = false;
	this.lastPopped = null;
	this.targetCol = function( ) {
		//c( 'targetCol' );
		//c( 'lastPopped : ' + this.lastPopped );
		var nextTarget = null;
		if( this.col3Popped == false ) nextTarget = 3;
		if( this.col2Popped == false ) nextTarget = 2;
		if( this.col1Popped == false ) nextTarget = 1;
		//var nextTarget = this.lastPopped + 1;
		
		if( nextTarget == null ) {
			nextTarget = this.lastPopped + 1;
		}
		if( nextTarget > numLinkColumns ) nextTarget = 1;
		this.lastPopped = nextTarget;
		eval( 'this.col' + nextTarget + 'Popped = true' );
		//c( 'nextTarget : ' + nextTarget );
		return nextTarget;
	}
	this.resetCol = function( col ) {
		//c( 'resetCol( ' + col + ' )' );
		var ecUrl = 'http://fav.premo.biz/fav_manager.php';
		var paramString = 'function=emptyColumn&col=' + col;
		new Ajax.Request( ecUrl,{
			method: 'post',
			parameters: paramString,
			onSuccess: function( response ) {
				
				$( 'fav_link_ul_' + col ).hide( { 
					afterFinish: function() { $( 'fav_link_ul_' + col ).remove() }
				} );
				
				/*
				c( 'poo 1' );
				//$( 'fav_link_ul_' + col ).setStyle( { visibility:'hidden' } );
				c( 'poo 2' );
				//$( 'fav_link_ul_' + col ).remove();
				$( 'fav_link_ul_' + col ).innerHTML = '';
				$( 'fav_link_ul_' + col ).setStyle( { visibility:'hidden' } );
				
				c( 'poo 3' );
				*/
			}
		} );
	}
	this.displayLinks = function( tagId,colOverride ) {
		//var tagLoc = linkColObj.getColToPop();
		var dlUrl = 'http://fav.premo.biz/fav_manager.php';
		var tgtCol;
		if( ( colOverride == undefined) || ( colOverride == 'null' ) || ( colOverride == null ) ) {
			tgtCol = this.targetCol();
		} else {
			tgtCol = colOverride;
			eval( 'this.col' + colOverride + 'Popped = true' );
		}
		
		var paramString = 'function=getLinksByTag';
		paramString += '&col=' + tgtCol;
		if( tagId != 'all' ) {
			paramString += '&tagid=' + tagId;
		}
		
		tagsArray.each( function( tagObj ) {
			if( tagObj.tagLoc == tgtCol ) {
				tagObj.takeOffDisplay( true );
			}
		} );
		
		
		var tgtUl  = 'fav_link_ul_' + tgtCol;
		var tgtBox = 'fav_link_box_' + tgtCol;
		if( $( tgtUl ) == null ) $( tgtBox ).insert( '<ul id="' + tgtUl + '" class="fav_link_ul" style="display:none;" ></ul>' );
		var maxLinkLen = 1024;
		new Ajax.Request( dlUrl,{
			method: 'post',
			parameters: paramString,
			onSuccess: function( response ) {
				//c( 'tgtCol (a) : ' + tgtCol );
				$( tgtUl ).innerHTML = '';
				response.responseJSON.each( function( link ) {
					tagsArray.each( function( tagObj ) {
						if( 1 ) { }
					} );
					
					
					var newLi;
					newLi  = '';
					newLi += '';
					//newLi += '<li class="link_' + link.id + ' box_' + tgtCol + '" >';
					//newLi += '<li class="link_' + link.id + ' box_' + tgtCol + '" >';
					newLi += '<li id="link_' + link.id + '"  >';
					newLi += '<div class="link_text_container" >';
					newLi += '<a class="link_text" href="' + link.url + '" target="_blank" >';
					var tempDesc = link.desc;
					if( tempDesc.length > maxLinkLen ) {
						tempDesc = tempDesc.substr( 0,( maxLinkLen - 3 ) ) + '...';
					}
					newLi += tempDesc;
					newLi += '</a>';
					newLi += '</div>';
					/*
					newLi += '<a href="javascript:editLink(' + link.id + ')" title="Edit." >';
					newLi += '<span class="link_button edit_button" ></span>';
					newLi += '</a>';
					newLi += '<a href="javascript:deleteLink(' + link.id + ')" title="Delete." >';
					newLi += '<span class="link_button del_button" ></span>';
					newLi += '</a>';
					*/
					
					newLi += '<a class="link_button edit_button" href="javascript:editLink(' + link.id + ')" title="Edit." alt="Edit." >';
					newLi += '</a>';
					newLi += '<a class="link_button del_button" href="javascript:deleteLink(' + link.id + ')" title="Delete." alt="Delete." >';
					newLi += '</a>';
					
					
					
					newLi += '</li>';
					newLi += '';
					$( tgtUl ).insert( newLi );
					$( tgtUl ).show();
				} );
			}
		} );
		return tgtCol;
	}
}
function uiTag( valPair )
{
	this.id = valPair.id;
	this.name = valPair.name;
	this.onDisplay = false;
	this.tagLoc = null;
	this.addMyLi = function( ) {
		var newLi;
		newLi  = '';
		newLi += '';
		//newLi += '<li id="tag_li_' + this.id + '" >';
		if( this.id == 'all' ) {
			newLi += '<a id="tag_li_' + this.id + '" href="javascript:popLinkList(\'' + this.id + '\');" >';
		} else {
			newLi += '<a id="tag_li_' + this.id + '" href="javascript:popLinkList(' + this.id + ');" >';
		}
		var tempName = this.name;
		tempName = tempName.replace( ' ','&nbsp;' )
		newLi += tempName;
		
		newLi += '<span class="tag_loc" id="tag_loc_' + this.id + '" ></span>';
		newLi += '</a>';
		//newLi += '</li>';
		newLi += '';
		
		//$( 'fav_tag_ul' ).insert( newLi );
		$( 'fav_tag_box' ).insert( newLi );
	}
	this.clickMe = function( colOverride ) {
		// Am I on display right now?
		if( this.onDisplay ) {
			// I'm on.  User clicked me.  Turn me off, remove the loc.
			this.takeOffDisplay();
			
		} else {
			this.putOnDisplay( colOverride );
		}
	}
	this.putOnDisplay = function( colOverride ) {
		//this.tagLoc = lastPopulatedBox + 1;
		//if( this.tagLoc > numLinkColumns ) this.tagLoc = 1;
		//this.tagLoc = linkColObj.getColToPop();
		
		//c( $( 'tag_li_' + this.id ).innerHTML );
		
		$( 'tag_li_' + this.id ).addClassName( 'on_display' );
		//c( 'this.tagLoc : ' + this.tagLoc );
		//popLinkList( this.id,this.tagLoc );
		this.tagLoc = uiLinkObj.displayLinks( this.id,colOverride );
		$( 'tag_loc_' + this.id ).innerHTML = this.tagLoc;
		this.onDisplay = true;
	}
	this.takeOffDisplay = function( navOnly ) {
		//c( 'takeOffDisplay( navOnly : ' + navOnly + ' )' );
		if( ( navOnly == null ) || ( navOnly == undefined ) ) navOnly = false;
		$( 'tag_li_' + this.id ).removeClassName( 'on_display' );
		$( 'tag_loc_' + this.id ).innerHTML = '';
		this.onDisplay = false;
		//resetLinkCol( this.tagLoc );
		//c( 'this.tagLoc : ' + this.tagLoc );
		if( !navOnly ) {
			uiLinkObj.resetCol( this.tagLoc );
			var evStr = 'uiLinkObj.col' + this.tagLoc + 'Popped = false;';
			//c( 'evStr : ' + evStr );
			eval( evStr );
			//c( 'evStr : ' + evStr );
			
		}
		
		
		this.tagLoc = null;
	}
}



function initFavs( )
{
	//c( 'initFavs()' );
	//linkColObj = new linkColumns();
	paramString = '';
	//if( $( 'fav_link_ul' ) != null ) $( 'fav_link_ul' ).remove();
	
	//c( 'mhge' );
	
	//c( 'fav_tag_box.innerHTML : ' + $( 'fav_tag_box' ).innerHTML );
	
	/*
	if( $( 'fav_tag_ul' ) == null ) {
		//c( 'is null ' );
		$( 'fav_tag_box' ).insert( '<ul id="fav_tag_ul" ></ul>' );
	} else {
		//c( 'is not null' );
	}
	*/
	
	//c( 'megh' );
	
	var gtlUrl = 'http://fav.premo.biz/fav_manager.php';
	paramString = 'function=getTagList';
	//c( 'meh 0' );
	new Ajax.Request( gtlUrl,{
		method: 'post',
		parameters: paramString,
		onSuccess: function( response ) {
			//c( 'meh' );
			//$( 'fav_tag_ul' ).innerHTML = '';
			tagsArray = new Array();
			response.responseJSON.each( function( tagPair ) {
				var tempObj = new uiTag( tagPair );
				tagsArray.push( tempObj );
				//tagsArray[tagPair.name] = tempObj;
			} );
			var allPair = new function( ) {
				this.id = 'all';
				this.name = 'all';
			}
			var allObj = new uiTag( allPair );
			// This is the problem ... I'm creating objects ...
			
			
			tagsArray.push( allObj );
			//tagsArray['_all'] = tempObj;
			//$( 'fav_tag_ul' ).innerHTML = '';
			$( 'fav_tag_box' ).innerHTML = '';
			tagsArray.each( function( el ) {
				el.addMyLi();
				//alert( 'hey' );
			} );
			
			
			//$( 'fav_tag_ul' ).insert( '<li>|</li>' );
			//$( 'fav_tag_ul' ).insert( '<li><a href="javascript:addLink();">add&nbsp;link</a></li>' );
			$( 'fav_tag_box' ).insert( '<span id="pipe_char" >|</span>' );
			$( 'fav_tag_box' ).insert( '<a href="javascript:addLink();">add&nbsp;link</a>' );
			
			
			//$( 'fav_content' ).appear( { duration: 0.225 } );
			$( 'fav_tag_box' ).appear( { duration: 0.225 } );
			$( 'fav_link_box_container' ).appear( { duration: 0.225 } );
			
			
			
			var stUrl = 'http://fav.premo.biz/fav_manager.php';
			paramStringSt = 'function=getSessionTags';
			//c( 'getSessionTags' );
			new Ajax.Request( stUrl,{
				method: 'post',
				parameters: paramStringSt,
				onSuccess: function( response ) {
					//c( 'response : ' + response );
					response.responseJSON.each( function( tagCol ) {
						if( tagCol.tag != 'null' ) {
							//uiLinkObj.displayLinks( tagCol.tag,tagCol.col );
							tagsArray.each( function( tagObj ) {
								var temp =  tagCol.tag + ' : ' + tagObj.id + ' , ' + tagCol.col;
								if( tagObj.id == tagCol.tag ) {
									tagObj.putOnDisplay( tagCol.col );
								}
							} );
						}
					} );
					
				}
			} );
			
			
		}
	} );
	
	
	// Get current tag's links (cookie? session?) 
	
	
	
}

function resetFavs( )
{
	// $('id_of_element').fade({ duration: 3.0, from: 0, to: 1 });
	//c( 'resetFavs()' );
	$( 'fav_tag_box' ).fade( { 
		duration: 1.0,
		afterFinish: function( ) {
			//$( 'fav_tag_ul' ).innerHTML = '';
			//if( $( 'fav_link_ul' ) !== null ) $( 'fav_link_ul' ).innerHTML = '';
			//c( 'poo1' );
			$( 'fav_tag_box' ).innerHTML = '';
			//c( 'poo2' );
		}
	} );
	$( 'fav_link_box_container' ).fade( { 
		duration: 1.0,
		afterFinish: function( ) {
			//c( 'poo 2.5' );
			if( $( 'fav_link_ul_1' ) !== null ) $( 'fav_link_ul_1' ).remove();
			//c( 'poo3' );
			if( $( 'fav_link_ul_2' ) !== null ) $( 'fav_link_ul_2' ).remove();
			//c( 'poo4' );
			if( $( 'fav_link_ul_3' ) !== null ) $( 'fav_link_ul_3' ).remove();
			//c( 'poo5' );
		}
	} );
	
}

function resetLinkCol( colNum )
{
	$( 'fav_link_ul_' + colNum ).remove();
}


function popLinkList( tagId )
{
	//uiLinkObj.displayLinks( tagId );
	
	tagsArray.each( function( tagObj ) {
		if( tagObj.id == tagId ) tagObj.clickMe();
	} );
	
	
}

function popLinkList_old( tagId,ulToPop )
{
	// Is this tag already displayed?
	
	if( $( 'tag_loc_' + tagId ).innerHTML == '' ) {
		//$( 'fav_link_box' ).innerHTML = '';
		//var ctUrl = 'http://fav.premo.biz/edit/get_links_by_tag.php';
		var ctUrl = 'http://fav.premo.biz/fav_manager.php';
		var paramString = 'function=getLinksByTag';
		if( ( tagId != '' ) && ( tagId != undefined ) ) {
			paramString += '&tagid=' + tagId;
		}
		
		/*
		var ulToPop = null;
		// Find next open fav_link_box
		if( $( 'fav_link_ul_1' ) == null ) ulToPop = 1;
		if( ( ulToPop == null ) && ( $( 'fav_link_ul_2' ) == null ) ) ulToPop = 2;
		if( ( ulToPop == null ) && ( $( 'fav_link_ul_3' ) == null ) ) ulToPop = 3;
		if( ( ulToPop == null ) ) {
			if( lastPopulatedBox == 3 ) {
				ulToPop = 1;
			} else {
				ulToPop = lastPopulatedBox + 1;
			}
		}
		*/
		
		lastPopulatedBox = ulToPop;
		//c( 'ulToPop : ' + ulToPop );
		
		
		if( $( 'fav_link_ul_' + ulToPop ) == null ) $( 'fav_link_box_' + ulToPop ).insert( '<ul id="fav_link_ul_' + ulToPop + '" class="fav_link_ul" style="display:none;" ></ul>' );
		
		var maxLinkLen = 33;
		new Ajax.Request( ctUrl,{
			method: 'post',
			parameters: paramString,
			onSuccess: function( response ) {
				$( 'fav_link_ul_' + ulToPop ).innerHTML = '';
				response.responseJSON.each( function( link ) {
					var newLi;
					newLi  = '';
					newLi += '';
					newLi += '<li class="link_' + link.id + ' box_' + ulToPop + '" >';
					newLi += '<a href="' + link.url + '" target="_blank" >';
					var tempDesc = link.desc;
					if( tempDesc.length > maxLinkLen ) {
						tempDesc = tempDesc.substr( 0,( maxLinkLen - 3 ) ) + '...';
					}
					newLi += tempDesc;
					newLi += '</a>';
					//newLi += ' ';
					//newLi += '(';
					newLi += '<a href="javascript:editLink(' + link.id + ')" >';
					newLi += '<span class="link_button edit_button" >';
					newLi += '</a>';
					//newLi += ' ; ';
					newLi += '<a href="javascript:deleteLink(' + link.id + ')" >';
					newLi += '<span class="link_button del_button" >';
					newLi += '</a>';
					//newLi += ')';
					newLi += '</li>';
					newLi += '';
					$( 'fav_link_ul_' + ulToPop ).insert( newLi );
					$( 'fav_link_ul_' + ulToPop ).show();
				} );
				//c( 'poo' );
				$$( '.tag_loc' ).each( function( el ) {
					c( 'check : ' + el.innerHTML );
					//
					if( el.innerHTML == ulToPop ) {
						el.innerHTML = '';
						$( 'tag_li_' + el.innerHTML ).removeClassName( 'on_display' );
					}
				} );
				//c( 'poo2' );
				if( tagId == null ) {
					$( 'tag_loc_all' ).innerHTML = ulToPop;
					//$( 'tag_li_all' ).removeClassName( 'off_display' );
					$( 'tag_li_all' ).addClassName( 'on_display' );
				} else {
					$( 'tag_loc_' + tagId ).innerHTML = ulToPop;
					//$( 'tag_li_' + tagId ).removeClassName( 'off_display' );
					$( 'tag_li_' + tagId ).addClassName( 'on_display' );
				}
				
				//c( 'poo3' );
			}
		} );
	} else {
		var myLoc = $( 'tag_loc_' + tagId ).innerHTML;
		$( 'tag_loc_' + tagId ).innerHTML = '';
		$( 'tag_li_' + tagId ).removeClassName( 'on_display' );
		//$( 'tag_li_' + tagId ).addClassName( 'off_display' );
		$( 'fav_link_ul_' + myLoc ).fade( { duration: 0.225 } );
	}
}



function addLink( )
{
	$( 'edit_form' ).getInputs().each( function( el ) {
		el.enable();
	} );
	$( 'edit_form' ).reset();
	var newH = document.viewport.getHeight();
	var newLbt = ( newH - $( 'edit_link_box' ).getHeight() + 2 ) / 2;
	$( 'edit_link_box' ).setStyle( { top: newLbt + 'px' } );
	
	$( 'edit_title' ).innerHTML = 'Add Link.';
	$( 'new' ).value = true;
	
	$( 'edit_link_box' ).appear( {
		duration: 0.225,
		beforeStart: toggleShadowbox(),
		afterFinish: function() { $( 'edit_form' ).focusFirstElement(); }
	} );
}

/*
function login( )
{
	
	var paramHash = $( 'login_form' ).serialize();
	$( 'login_form' ).getInputs().each( function( el ) {
		el.disable();
	} );
	
	
	var lUrl = 'http://fav.premo.biz/login_manager.php';
	new Ajax.Request( lUrl,{
		method: 'post',
		parameters: paramHash,
		onSuccess: function( response ) {
			
			// Successful login?
			
			switch( response.responseText ) {
				case '101' :
					//success
					hideLoginBox();
					initHeader( $( 'email' ).value );
					initFavs();
					break;
				case '201' :
					//incorrect password
					alert( 'incorrect password' );
					$( 'login_form' ).getInputs().each( function( el ) {
						el.enable();
					} );
					break;
				case '202' :
					//email not found
					alert( 'email not found in system' );
					$( 'login_form' ).getInputs().each( function( el ) {
						el.enable();
					} );
					break;
			}
			
			
		}
	} );
}
*/


function netEditLink( )
{
	var paramHash = $( 'edit_form' ).serialize();
	$( 'edit_form' ).getInputs().each( function( el ) {
		el.disable();
	} );
	
	var aUrl = 'http://fav.premo.biz/fav_manager.php';
	new Ajax.Request( aUrl,{
		method: 'post',
		parameters: paramHash,
		onSuccess: function( response ) {
			
			// Successful login?
			
			switch( response.responseText ) {
				// cases for expired sessions
				case '501' :
					//success
					hideEditBox();
					//initHeader( $( 'email' ).value );
					initFavs();
					break;
			}
			
			
		}
	} );
	
	
	
}


function hideEditBox( )
{
	$( 'edit_link_box' ).fade( { duration: 0.225, beforeStart: toggleShadowbox() } );
}

function cancelEdit( )
{
	hideEditBox();
}


function deleteLink( linkId )
{
	/*
	<li id="link_41">
		<div class="link_text_container">
			<a class="link_text" target="_blank" href="http://www.google.com/movies?hl=en&near=30319&dq=30319+movies&sort=1&sa=X&oi=showtimes&ct=title&cd=1">30319 Movies @ Google</a>
		</div>
		<a class="link_button edit_button" alt="Edit." title="Edit." href="javascript:editLink(41)"/>
		<a class="link_button del_button" alt="Delete." title="Delete." href="javascript:deleteLink(41)"/>
	</li>
	*/
	
	
	
	/*
	c( 'link_' + linkId );
	$( 'link_' + linkId ).childElements().each( function( cE,index ) {
		c( index + ' : ' + cE.innerHTML );
		//c( index + ' : ' + cE.childElements()[0].readAttribute( 'href' ) );
		c( index + ' : ' + cE.readAttribute( 'href' ) );
	} );
	
	
	c( 'meh : ' + $( 'link_' + linkId ).childElements()[0].childElements()[0].readAttribute( 'href' ) );
	c( 'feh : ' + $( 'link_' + linkId ).childElements()[0].childElements()[0].innerHTML );
	*/
	
	
	var cTxt = 'Are you sure you want to delete the link';
	cTxt += "\n";
	cTxt += '"';
	//cTxt += $( 'link_' + linkId ).childElements()[0].readAttribute( 'href' );
	cTxt += $( 'link_' + linkId ).childElements()[0].childElements()[0].innerHTML;
	cTxt += '" ? ';
	cTxt += '';
	cTxt += '';
	
	
	
	
	
	
	/*
	var proceed = confirm( 'Are you sure you want to delete the link "' + $( 'link_' + linkId ).childElements()[0].innerHTML + '" ? ' );
	var proceed = confirm( 'Are you sure you want to delete the link "' + $( 'link_' + linkId ).childElements()[0].readAttribute( 'href' ) + '" ? ' );
	*/
	
	var proceed = confirm( cTxt );
	
	
	if( proceed ) {
		$( 'link_' + linkId ).remove();
		var dUrl = 'http://fav.premo.biz/fav_manager.php';
		var paramString = 'function=deleteLink&link=' + linkId;
		new Ajax.Request( dUrl,{
			method: 'post',
			parameters: paramString,
			onSuccess: function( response ) {
				
				// Successful login?
				
				switch( response.responseText ) {
					// cases for expired sessions
					case '701' :
						//success
						//hideAddBox();
						//initHeader( $( 'email' ).value );
						initFavs();
						break;
				}
				
				
			}
		} );
	}
	
}

function editLink( linkId )
{
	// spawn edit box
	
	$( 'edit_form' ).getInputs().each( function( el ) {
		el.enable();
	} );
	$( 'edit_form' ).reset();
	var newH = document.viewport.getHeight();
	var newLbt = ( newH - $( 'edit_link_box' ).getHeight() + 2 ) / 2;
	$( 'edit_link_box' ).setStyle( { top: newLbt + 'px' } );
	
	$( 'edit_title' ).innerHTML = 'Edit Link.';
	$( 'new' ).value = false;
	
	
	
	var elUrl = 'http://fav.premo.biz/fav_manager.php';
	var paramString = '';
	if( ( linkId != '' ) && ( linkId != undefined ) ) {
		paramString = 'function=getLinkInfo&link=' + linkId;
	}
	new Ajax.Request( elUrl,{
		method: 'post',
		parameters: paramString,
		onSuccess: function( response ) {
			response.responseJSON.each( function( link ) {
				$( 'desc' ).value = link.desc;
				$( 'url'  ).value = link.url;
				$( 'link' ).value = link.id;
				//$( 'link_exists' ).value = 'exists';
				//$( 'link_exists_exists' ).checked = true;
				var tagList = '';
				link.tags.each( function( tag ) {
					//c( 'tag : ' + tag );
					tagList += tag + ', ';
				} );
				var trimLength = tagList.length - 2;
				tagList = tagList.substr( 0,trimLength );
				//c( 'tagList : ' + tagList );
				$( 'tags' ).value = tagList;
			} );
		}
	} );
	
	
	
	
	
	window.scrollTo( 0,0 );
	//$( 'edit_link_box' ).appear( { duration: 0.225, beforeStart: toggleShadowbox() } );
	$( 'edit_link_box' ).appear( {
		duration: 0.225,
		beforeStart: toggleShadowbox(),
		afterFinish: function() { $( 'edit_form' ).focusFirstElement(); }
	} );
}


function tagTextFocus( )
{
	$( 'edit_message' ).innerHTML = 'Tip: Separate multiple tags with commas.';
}

function tagTextBlur( )
{
	$( 'edit_message' ).innerHTML = '';
}






