 function makeControl() {
                var control = document.createElement("a");
                var controlText = document.createTextNode("Close");
                
                control.href = "#"; // so browser shows it as link
                control.className = "control";
                control.appendChild(controlText);
                
                Seadragon.Utils.addEvent(control, "click", 
                    closeZoomImage);
                    
                return control;
            }
function QStoArray(qs){
	qs=qs.split('?',2);
	
	if(qs.length>1)
		qs=qs[1];
	else
		qs=qs[0];
	
var qsp = qs.split('&');
var arr ={};
$.each(qsp, function(x,s){
    var temp = s.split('=');
    arr[temp[0]] = temp[1];
});
return arr;
}

var inClick=false;
var currGal='';
var currX='';
var currY='';
var currHasVideo=false;
var showVideo = false;
var currGalVid=false;
var firstClick=true;
var checkspl='';
var gallery_data=[];
var mappedURLs=[];
var inverseMappedURLs=[];

function viewAll(){
	if(currGal=='7elwZkuBs1')
		location.hash="?go=page&gal_id=news";
	else
		location.hash="?go=gal&gal_id="+currGal;
	return false;
}


var prevHash="";
var pdfReturn="";


function thumbClick(){
	
	 //showVideo = false;
	 inClick=true;
	 
	 gourl=$(this).attr('go');
	
	 if(!gourl)
		 gourl=$(this).attr('href'); 
		
	 if(!gourl)
		 gourl=location.hash.slice(1);
	  
	 
	 if(gourl!="" && prevHash==gourl){
		 inClick=false;
		 return false;
	 }
     
     if(gourl=='')
     {
         if(mappedURLs[location.pathname]!=undefined){
             gourl=mappedURLs[location.pathname];
         }
     }
	 
	 prevHash=gourl;
    cancelHandling=false;
	 

    if(this.onclick)
		 cancelHandling=!this.onclick();

    
	 if(!gourl || gourl=='' || cancelHandling){
         if(firstClick){
        	 if(location.search!=''){
        		 gourl=location.search;
        		 if(location.hash!=gourl)
        			 location.hash=gourl;
        	 }
        	 else{
        		 //gourl="?go=page&gal_id=home";
        		 if(location.hash!=gourl)
        			 location.hash=gourl;
        	 }
                  
         if(inverseMappedURLs[location.hash]!=undefined){
             location.hash=inverseMappedURLs[location.hash];
         }
  
         }
         else{
                    inClick=false;
                    return;
             }
	 }
   
     
     firstClick=false;
	 
	 govars=QStoArray(gourl);
	 handled=true;
	
/*	 if(govars['go']=='special');
	if(checkspl!=govars['go'])
	{
		checkspl=govars['go'];
	}
	else
		govars['go']='';
	 
	 
	 //alert(checkspl);
	 /*if(!checkspl)
		{	
		inClick=false;
		return !handled;
		govars['go']=='';
		//return false;
		}*/
			
	
       if(govars['go'] && govars['go']!='' && (govars['go']!='pdf' && govars['go']!='url')){
			closeZoomImage();
			$("#download_pdf_container").hide();
			$("#overlay").hide();
			$("#view_all").hide();
			$('#gallery-page').hide();
		 	$('#gallery-page > div').hide();
		 	$('#gallery-thumbs > div').hide();
			$('#gallery_img > div').hide();
			$("#view_video").hide();
			$('#gallery-zoom').hide();
            //$('#gallery-thumbs-home .gallery-thumb').hide();
			$('#page-news').hide();
			$("#pod-photographerinfo").hide();
       }
	 switch(govars['go']){
		 case 'page':
		 
             $('#gallery-page').show();
		 	 $('#gallery_name').html('');
			
			$('#page-'+govars['gal_id']).show();
			
			$("#photo_pdf_link").hide();
			$("#pod-photographerinfo").hide();

			$('#nav_previous_bx').hide();
			$('#nav_next_bx').hide();
			
            if(govars['gal_id']=='lightbox')
            full_gallery(govars['gal_id']);
            
            if(govars['gal_id']=='home' ){
                showGal(govars['gal_id']);
               // $('#gallery-thumbs-home .gallery-thumb').hide();
				$('#page-home').hide();
//                setupHomePageMasonry();
            }
            
			if( govars['gal_id'] == "news" ) {
				showGal(govars['gal_id'], showVideo);
				//$('#page-news').show();
            }else{
				$('#page-news').hide();
			}
			if(govars['gal_id']=='home'){
            	//setTimeout(setupHomePageMasonry,10);
            	//$('#gallery-thumbs-home .gallery-thumb').hide();
            }
			 // if(govars['gal_id']=='aff1')
				// {
				// $('#gallery-thumbs-aff1 .gallery-thumb').show();
				// }
			// if(govars['gal_id']=='aff3')
			// {
			// $('#gallery-thumbs-aff3 .gallery-thumb').show();
			// }
			// if(govars['gal_id']=='aff5')
			// {
			// $('#gallery-thumbs-aff5 .gallery-thumb').show();
			// }
            //setTimeout(resetScrollPane,10);
            
            break;
		 
         case 'full':
            $('#gallery_name').html('');
            destImg=govars['img'];
            full_gallery(govars['gal_id']);
            $("#pod-photographerinfo").hide();
            break;
		 case 'portfolio':
			$('#gallery_name').html('');
            destImg=govars['img'];
			portfolio(govars['gal_id']);
			$("#pod-photographerinfo").hide();
			break;
		 
		 case 'bio':
			$('#gallery_name').html('');
			//$("#pod-photographerinfo").show();
			showInfo($(this), govars['gal_id']);
			break;
			//fall through
		case 'aff':
			$('#gallery_name').html('');
			$("#pod-photographerinfo").hide();
			if(govars['gal_id']=='aff1')
			{
				$('#gallery-thumbs-aff1 .gallery-thumb').show();
			}
			if(govars['gal_id']=='aff3')
			{
				$('#gallery-thumbs-aff3 .gallery-thumb').show();
			}
			if(govars['gal_id']=='aff5')
			{
				$('#gallery-thumbs-aff5 .gallery-thumb').show();
			}
			//setTimeout(resetScrollPane,10);
			
		case 'gal':
		 	changeLogo($(this));
		 	$('#gallery_name').html('');
		 	
		 	//$("#view_all").attr("href","?go=gal&gal_id="+govars['gal_id']);
			$("#view_all").hide();
			$("#pod-photographerinfo").hide();
									//alert( "go: gal..." );
			currHasVideo=false;
			 for (i=0;i<stylePhotogs[10].length;i++){
				if( stylePhotogs[10][i] == govars['gal_id'] ) {
					$("#view_video").attr("href","?go=gal&gal_id="+govars['gal_id']+"&vdo_gal=1");
					$("#view_video").show();

					currHasVideo = true;
				}
			}			

			
			
			//alert( "checking vdo_gal..." );
	           
			if( govars['vdo_gal'] != undefined ) {
				//alert( "vdo_gal");
				showVideo = true;
				showGal(govars['gal_id'], showVideo);
				
			}else {
				//alert( "no vdo_gal");
				showVideo = false;
				showGal(govars['gal_id'], showVideo);
				
			}
	        
            destImg=govars['img'];
              
		    //showInfo($(this), govars['gal_id']);
			
			resetPrevNext();
			//setTimeout(resetScrollPane,10);
		 	break;
		 case 'image':
		 case 'url':
            prevHash=location.hash.slice(1);
		 	handled=false;
		 	window.open(govars['url']);
		 break;
		 case 'dzi':
			re=/.*?galleryimages\/(.*)\/pyr\/(.*)\..*/;
			matches = re.exec(govars['dzi']);
			gal_name=gallery_data["gallery-thumbs-"+matches[1]]["name"];
			
			imageinfo=getImgData(matches[1],matches[2]);
			gal_author=imageinfo['info'];
			
			
			showGal(matches[1],false,true);
			$('#gallery_name').html(gal_author);
			$('#gallery_name').append('<br />',gal_name);
			
			
			//$('#nav_previous_bx').show();
			//$('#nav_next_bx').show();
		 					//alert( "CASE dzi....calling setCurrImg....." );
			setCurrImg(this,matches[1],matches[2]);
			 				//alert( "calling loadZoomImage....." );
							
			if( $("#view_video").is(":hidden") ) {
				//alert( "view_video hidden" );
			}else {
				//alert( "view_video NOT hidden" );
			}
			
			loadZoomImage(govars['dzi']);
			$("#view_all").show();
			$('#gallery-zoom').show();

							// alert( "... image shown!");
			break;
       case 'pdf':
       if(currGal!='' || govars['gal_id']!=''){
        pdfReturn=location.hash;
       $("#pod-photographerinfo").hide();
	   //$('#gallery-thumbs-home .gallery-thumb').hide();
       if(govars['action']=='close'){
           $("#download_pdf_container").hide();
			$("#overlay").hide();
       }else{
       if(currGal!='' || govars['gal_id']!=''){
           $("#pdf_link").hide();
               $('#pdf_gal').val(currGal!=''?currGal:govars['gal_id']);
               $("#pdf_error").html("");
               $("#overlay").show();
               $("#download_pdf_container").show();
       }
       }
       }

       case 'home':
       //do nothing, everything is already hidden.
       break;
   break;
		 default: 
		 	handled=false;
			return true;
		 	location.href=gourl;
		 break;
       
	 }
	 
     
//	 if($('#menu_'+govars['go']+'_'+govars['gal_id']).filter(':visible').length<1)
     {
		 $('#menu').show();

         if(govars['go']=='home' && $("#menu").hasClass("active")){
             $('*',$("#menu")).removeClass('active');
         }else{
             $('*',$("#menu")).removeClass('active');
             $('#menu a[href="'+gourl+'"]').parents("li").addClass('active');
             $('#menu a[href="'+location.pathname+'"]').parents("li").addClass('active');
             if(this && this.tagName && this.tagName=='A')
             $(this).parents("li").addClass('active');
             if( govars['vdo_gal'] != undefined )
             $('#menu_'+govars['go']+'_'+govars['gal_id']+'_1').parents('li').addClass('active');
             else
             $('#menu_'+govars['go']+'_'+govars['gal_id']).parents('li').addClass('active');
		     $('#menu_button,#menu_items,#menu').addClass('active');
		     //$('#menu_'+govars['go']+'_'+govars['gal_id']).click();
		     //$('#menu_'+govars['go']+'_'+govars['gal_id']+' > p').click();
		     $('#gallery-link-'+govars['gal_id']).addClass('bg_menu_sub_select');
		     $('#menu_page_'+govars['gal_id']).addClass('bg_menu_sub_select');
         }
         randBG();
	 }
	setMenuPosition(); 
	 
	 	//alert( "go done, calling resetScrollPane...." );
//	resetScrollPane();     
	            //setTimeout(resetScrollPane,10);

		//alert( "... resetScrollPane done. checking handled and location gourl..." );
	if(handled){
       /*  if(inverseMappedURLs[gourl]!=undefined){
             gourl=inverseMappedURLs[gourl];
         }
		if(location.hash!=gourl)
			location.hash=gourl;
		 */    
  
	}
	
	//alert(loacation.hash=gourl);
	//exit;
	//console.log("Thumbclick done:"+location.hash+"["+gourl+"]");	

			//alert( "... handled and location gourl done." );
inClick=false;
return !handled;
//return false;
}


function showvideo() {

	$('#gallery-video').show();
	$('#gallery-thumbs').hide();
	//resetScrollPane();
}

function changeLogo(ele) {
	if( ele.attr("class") == "new_talent" ) {
		$("#pod-bot #logo").addClass("fresh");
	}else {
		$("#pod-bot #logo").removeClass("fresh");
	}	
}

function showInfo(ele, gal_id) {

	var content, infoid;
	if(typeof ele == "string"){
		return;
		
	}
	var gall_name="";
	
	if( ele.attr("href") != "" ) {
		if( gal_id == "home" ) {
			showInfo($("#latest_news"));
			return;
		}else {	
			infoid = gal_id;		//infoid = ele.attr("href").substring(15);
			$("#photo_pdf_link").show();
			pdf_link = "download_pdf.php?gal="+infoid;
			//$("#photolink_pdf").attr("href", pdf_link );
			$("#pdf_gal").val(infoid);
			$("#photo_pdf_link").show();
			
//			$("#photolink_url").attr("href", photoLink[infoid]);
			/*if( photoLink[infoid] != undefined && photoLink[infoid] != "http://" ) {
				$("#photolink_url").attr("href", photoLink[infoid]);
				$("#photolink_url").show();
			}else {
				$("#photolink_url").hide();
			} */
				
		}
	}else {
		infoid = ele.attr("gal_id");
		$("#photoLink_url").hide();
		$("#photo_pdf_link").hide();
	}
	if(gallery_data['gallery-thumbs-'+gal_id])
	gall_name=gallery_data['gallery-thumbs-'+gal_id]['name'];
	
    content="";
    
    if(gall_name && gall_name!="" && infoid && infoid!="" && photoInfo[infoid] && photoInfo[infoid]!="")
	content = "<h3>"+gall_name+"</h3>"+ photoInfo[infoid];
	$("#pod-photographerinfo").html(content);
	if(content!="" && $("#pod-photographerinfo").contents().length>0)
        $("#pod-photographerinfo").show();
    else
        $("#pod-photographerinfo").hide();
    
	/*
	if( photoLink[infoid] != "undefined" ) {
		$("#photolink_url").href(photoLink[infoid]);
		$("#photoLink_url").show();
	}else {
		$("#photoLink_url").hide();
	}
	*/
}

function getImgData(gallery_id,img_id){
	search_gal=gallery_data["gallery-thumbs-"+gallery_id]["images"][0];
	
	ret_data=null;
	for(i=0;ret_data==null && i<search_gal.length;i++){
		if(search_gal[i]["id"]==img_id){
			ret_data=search_gal[i];
		}
	}
	return ret_data;
}

function getImgIdx(gallery_id,img_id){
	search_gal=gallery_data["gallery-thumbs-"+gallery_id]["images"][0];
	ret_idx=-1;
	for(i=0;ret_idx<0 && i<search_gal.length;i++){
		if(search_gal[i]["id"]==img_id){
			ret_idx=i;
		}
	}
	return ret_idx;
}



function setCurrImg(el,gal_id,img_id){
	re=/^gallery-(thumbs-)?([^_]*)_thumb_(\d+)_(\d+)/g;
	
	matches = re.exec(el.id);
	elid=el.id;
	
	if(!matches || matches.length<2){
		if(el.id=="nav_next" || el.id=="nav_previous"){
			elid=$(el).attr('elid');
		}
		matches = re.exec(elid);
	}
	
	if((!matches || matches.length<2) && !(gal_id && img_id))
	return;
	
	if(!matches &&(gal_id && img_id)){
		currGal=gal_id;
		currX=0;
		currY=getImgIdx(gal_id,img_id);
	}else{
		
		currGal=matches[2];
		currX=matches[3];
		currY=matches[4];
		
	}
	
	nextimg=getNextImg();
	
	if(nextimg!=''){
		//$('#nav_next').html('Next');
		$('#nav_next_bx').show();
		$('#nav_next').attr('go',$('#'+nextimg).attr('go'));
		$('#nav_next').attr('elid',nextimg);
//		$('#nav_next').click(thumbClick);
	}else{
		resetNext();
	}

	previmg=getPrevImg();
	if(previmg!=''){
		//$('#nav_previous').html('Previous');
		$('#nav_previous_bx').show();
		$('#nav_previous').attr('go',$('#'+previmg).attr('go'));
		$('#nav_previous').attr('elid',previmg);
//		$('#nav_previous').click(thumbClick);
	}else{
		resetPrev();
	}
}

