/***********************************
Copyright 2008-2009
The City University of New York
Office of University Relations
Office of Communications and Media
Eri Sahagun, Web Developer
www.cuny.edu
************************************/
/**anchor code**/



//a global needs to be used for the previous url, as a new object is created and the last url val needs to be preserved
var prevUrlAnchor = -1;

function Anchs()
{
	//used name attrib for testing purposes, a different object is created when page is first visited, as opposed to when a href is clicked
	this.name;
	this.urlAnchor;
	this.prevUrlAnchor =-1;
	
}


Anchs.prototype.getAnchor = function()
{

	
	var anchorTag =-1;
	var temp_win = parent.location.hash;
	if(temp_win.search("#") !=-1 )
	{
    var anchorTag = temp_win.slice(temp_win.indexOf('#') +1, temp_win.indexOf('-'));
	
	 }
	
	  this.urlAnchor= anchorTag;
	if(anchorTag !=''){
  
	$("#"+anchorTag).show();
	}
}
//Attic is extending Anchs,  aappend item is for any extra item to be show if the anchor is there, in this case the tabs for findit, or findcollege 
function Attic(appendItem, fa)
{
	//parent constructor
	Anchs.call(this);
		
    this.append = appendItem;
	this.fakeAnchor = fa;
	
}
//assign base class to Anchs
Attic.prototype = new Anchs();
//assign Attic constructor to be Attic not Anchs
Attic.prototype.constructor = Attic;
Attic.prototype.showAttic =
function ()
{
	
	$('#'+this.urlAnchor+this.append).addClass('toggle-on');
	
	$("#"+this.urlAnchor+this.fakeAnchor).show();
	
	
//window.location.href =windOriginal +'#'+this.urlAnchor;		
		
}
function Example()
{
	Anchs.call(this);
		
	
}
Example.prototype = new Anchs();
Example.prototype.constructor = Example;




//function is independant of any classes, it listens for a statechange (clicking of a ) and if it is an anchor, a new anchor object is created
function onhashchange(){
var temp_win = window.location.href;
			
var anchorsa = new Attic("-link");
anchorsa.getAnchor();
anchorsa.showAttic();


}


//if need url without anchor for some reason  toChange is for a manual anchor tag insertion into turl
function clearAnchor(toChange)
{
	
	//stuff for url need to clear any previous anchors first.  windOriginal truncates the url to take off any previous anchor, so it won't keep appending it 
		var windOriginal= window.location.href.slice(window.location.href.indexOf('http'),window.location.href.indexOf('.html')+5);
		//window.location.href =windOriginal +'#'+toChange;		
		window.location.href =windOriginal;
}



/*** main-wrap.js ***/

/* DOCUMENT */
/* OnReady Hide FINDBAR which is code placeholder of ATTICBAR */
//$("document").ready(function(){ $("#atticbar").hide(0);});
//$("document").ready(function(){ $("#findbar").hide(0);});

$("document").ready(function(){
	
    var html = "";
	html = $("#findit-content").html();
	var tempFindit = document.getElementById("findit");
    if(html != null){
    tempFindit.innerHTML=html;
    }
	html = $("#findcollege-content").html();
	
    var tempFindCollege = document.getElementById("findcollege");
    if( html != null){
	tempFindCollege.innerHTML=html;
	}
	
	var tempfinditContent = document.getElementById("findit-content");
	if(tempfinditContent != null){
	tempfinditContent.innerHTML="";
	}
    var tempfinditCollegeContent = document.getElementById("findcollege-content");
	if(tempfinditCollegeContent !=null){
	tempfinditCollegeContent.innerHTML="";
	}

        

	$("div#findit").hide(0);
	$("div#findcollege").hide(0);
	
								 //for the attic on all pages
var anchorsa = new Attic("-link");

        anchorsa.getAnchor();
		anchorsa.showAttic();
	
});

/* ELEMENTS */
/* [^FindIt] and [^College Websites] Links
OnClick Get Content from FINDBAR into ATTICBAR and Toggle It */
$(function() { $("#findit-link").click(function(e){
												
	
	//var windOriginal= window.location.href.slice(window.location.href.indexOf('http'),window.location.href.indexOf('.html')+5);	
	//e.preventDefault();
	
	
	$("#findcollege").slideUp("normal");
	$("#findit").slideToggle("normal",function(){
						//this is to check if the item is hidden, and if so, remove the anchor tag. 							
		if($(this).css('display') == 'none')
		{
		parent.location.hash='';
		}
				else
				{
					
		var windOriginal= window.location.href.slice(window.location.href.indexOf('http'),window.location.href.indexOf('.html')+5);
		parent.location.hash ='#findit-ar';	
		//e.preventDefault();
	
		
				}
		
	});
	$("#findit-link").toggleClass("toggle-on");
	$("#findcollege-link").removeClass("toggle-on");
	
	})
});

$(function() { $("#findcollege-link").click(function(e){ 
													// e.preventDefault();
	//prevent default makes it so there is no jumpy animation because of anchoring
	//however, still need to put the href in the url, but this must be done Before the prevent default action, otherwise same jumpy animation occurs
	var windOriginal= window.location.href.slice(window.location.href.indexOf('http'),window.location.href.indexOf('.html')+5);
	
		parent.location.hash ='#findcollege-ar';	
		e.preventDefault();
	
	$("#findit").slideUp("normal");
	$("#findcollege").slideToggle("normal",function(){
													
	if($(this).css('display') == 'none')
	{						
		parent.location.hash ='';
	}																													
													
	}); 
	$("#findcollege-link").toggleClass("toggle-on");
	$("#findit-link").removeClass("toggle-on");
	
		
	})
});

/* [x] Links inside ATTICBAR: OnClick Hide ATTICBAR */
$(function() { $("#findit-hide-button").click(function(){ 
	$("#findit").slideUp("normal");
	parent.location.hash ='';
	$("#findit-link").removeClass("toggle-on");	
	})
});
$(function() { $("#findcollege-hide-button").click(function(){ 
	$("#findcollege").slideUp("normal");
	parent.location.hash ='';
	$("#findcollege-link").removeClass("toggle-on");	
	})
});


$("document").ready(function(){ $(".hidden-content").hide(0);});

/* This function is to toggle inner text, see how it is used below. */
	
jQuery.fn.toggleText = function(a,b) {
        return this.html(this.html().replace(new RegExp("("+a+"|"+b
+")"),function(x){return(x==a)?b:a;}));

}

/*This toggle button is used for paragraph text mostly. The button must read "<read more>" and it toggles to "<hide text>" */

$(function() { $(".toggle-button-show").click(function(){ 	
	$(this).parent().find(".toggle-button-show").toggleText('&lt;read more&gt;','&lt;hide text&gt;');				
	$(this).parent().find("+ .hidden-content").toggle("normal");	
	$(this).siblings(".hidden-content").toggle("normal");		
	//$(this).parent().siblings().filter(":first").toggle("normal");	

	})
});

/*This toggle button is just a text variation on the one above. The button must read ">>" and it toggles to "<<" */


$(function() { $(".toggle-button-arrow").click(function(){ 	
	$(this).parent().find(".toggle-button-arrow").toggleText('&gt;&gt;','&lt;&lt;');				
	$(this).parent().find("+ .hidden-content").toggle("normal");	
	$(this).siblings(".hidden-content").toggle("normal");
		//$(this).parent().siblings().filter(":first").toggle("normal");	

	})
});

/*This toggle button's text does not change */

$(function() { $(".toggle-button").click(function(){ 				
	$(this).parent().find("+ .hidden-content").toggle("normal");
	$(this).siblings(".hidden-content").toggle("normal");
	//$(this).parent().siblings().filter(":first").toggle("normal");	

	})
});


/*This button is to be placed within a block of hidden content. It hides the block of content that it is in. */



$(function() { $(".button-hide").click(function(){
													  										   							 	
	$(this).parent().siblings().find("+ .hidden-content").hide("fast");
	$(".toggle-button-show").text("<read more>");
	
	
	//$(this).parent().siblings().filter(":first").toggle("normal");	

	})
});




/*Using this for the apply pages */

function toggle_show(toggleId,detailsId,toggleShow,toggleHide){ 
	$("#" + detailsId).toggle("normal"); 
	
	
	var toggleGrab = document.getElementById(toggleId);
	var toggleHTML = toggleGrab.innerHTML;
	
	if ((toggleHTML == toggleShow) || (toggleHTML == "&gt;&gt;")) {
	
		$("#" + toggleId).text(toggleHide);
		
		
	}
	
	else if ((toggleHTML == toggleHide) || (toggleHTML == "&lt;&lt;")) {
		
		$("#" + toggleId).text(toggleShow);
	
	}
	
	
		
}

/* This clears temporary copy in a search box when user enters the box */
function clearIt(what) {
    what.value = '';
}

/* This puts temporary copy back in a search box when user leaved the box without entering content */
function defaultIt(what) {
    if (what.value == '') {
        what.value = what.defaultValue;
    } else {
        what.value = what.value;
    }
}


function goTo(menu) {
	try {
		if (menu) {
			location = menu.options[menu.selectedIndex].value;
		}//end if 
		else { 
			alert(typeof(menu));
		}//end else
	}// end try
	catch(err) { }//end catch
}//end goTo

$(document).ready(function(){
			var lastShown='';
//next few lines to detect/show anchor if it is present in url on page load

	var anchorTag =-1;
	var temp_win = window.location.href;
	if(temp_win.search("#") !=-1 )
	{
    var anchorTag = temp_win.slice(temp_win.indexOf('#') +1);
    lastShown= anchorTag;

	if(anchorTag !=''){
	$('.'+anchorTag).removeClass('hidden-item');  
	}
	
	}
	
	//looking for a.anchored tags that read <read more>
	if (anchorTag.length > 2) {
	$('a.anchored[href='+ anchorTag +']').text('<hide text>');

	}
		
//Isaac's Anchors; use this function if you want to create a anchor tag when you hide/show a certain piece of content. 	   
	   	
$('a.anchored').click(function(event){
				//stay on same page
				event.preventDefault();
								
					//var toShow = $(this).attr('href');
                    var toShow = $(this).attr('id');
                    //alert(test);
					location.hash='';
				
				
				//looking for a.anchored tags that read <hide text> when another div is shown.
				$("a.anchored:contains('hide text')").text('<read more>');
				
				
					//hide last div	
				if(lastShown !='' )
					{
					$('.' +lastShown).addClass('hidden-item');  
					}
						
				//show current div	
				$('.' +toShow).removeClass('hidden-item'); 	
		           location.hash ="#"+toShow;
		          if(toShow==lastShown)
				  {
					$('.' +lastShown).addClass('hidden-item');
					location.hash='';
					toShow='';
				  }
					   
						//reset lastShown
						lastShown = toShow;
						
						
				
				
				if (lastShown !='' ) {
				
				$(this).parent().find("a.anchored:contains('read more')").text('<hide text>');

				}
				
		});//end a.anchroed clicked				   	
						   
 });//end doc ready global anchors


