       function loadTournamentPhotoDIV(){
		jQuery.ajax({
			url : "/pis/photoBanner.html",
			type : "GET",
			cache : false,
			success : function(data) { 
                             jQuery("body").append(data );
                             getTournamentPhotoListings();	
			}
		});
       }       

       var separator = ';';
       var quotes = '"';
       var comma = ',';
       var empty = '';
       
	function parseDirs(dirFromPHP) {
		var dirArr = dirFromPHP.split(separator);
		var dirElements = {};
		var commonEl = null;
		var thisEl = null;
            	for(index in dirArr) {
			if(null == commonEl) {
				commonEl = dirArr[index];
                            dirElements[commonEl] = empty;
				continue;
			}

			thisEl = dirArr[index];
			if(-1 != thisEl.indexOf(commonEl)) {
                            if(dirElements[commonEl] == '')
                                dirElements[commonEl] =  thisEl;
                            else 
                                dirElements[commonEl] = dirElements[commonEl] + comma + thisEl;

			} else {
				commonEl = thisEl;
                            if(commonEl  == null || commonEl  == '' || commonEl  == 'undefined'){
                                 break;
                            }
                            dirElements[commonEl] = empty;
			}
		}
              dirElements = sortAssoc(dirElements);
		return dirElements;
	}
	// And here comes the funciton itself
	function sortAssoc(dirElementsMap){
              var retMap = {};
		var keys = Array();
		for (var sKey in dirElementsMap){
		    keys.push(sKey);
              }
              keys.sort();
              keys.reverse();
              var max = keys.length;
              var cur = 0;
            	while(cur < max) {
                 key = keys[cur];
                 retMap[key ] = dirElementsMap[key];
                 cur++;
             }
	      return retMap;
	} 

       var selImages = null;
       var imageRoot = null;

	function makeDivHTML(dirRoot,dirStr) {
              var dirName = '/TournamentPictures/' + dirRoot + '/';
		var html = "";
		var liHtml = "";
		var dirArr = dirStr.split(separator);			
		liHtml = '';
              var max = dirArr.length;
              var cur = 0;
            	while(cur < max) {
                     if(dirArr[cur ] != null && dirArr[cur ] != ""){
			        liHtml = liHtml + '<li><a href="#"><img id="tPicture" align="middle" ' + 'src="' + dirName + dirArr[cur ] + quotes + '></img></a></li>';
                     }
                     cur++;
		};
              selImages = dirStr;
              imageRoot = dirName;
		html = html + liHtml;		
		jQuery('#navT').html(html);
	       jQuery("#tPicture").live("click", function() {
                     displayPhoto(jQuery(this).attr('src'),selImages ,imageRoot);
                     return false;
              });
	}

	function makeYearTreeHTML(dirTree) {

		var htmlToSet = '';
		for(var keys in dirTree) {
                     htmlToSet = htmlToSet + '<li><span class="folder">' +  keys + '</span>';
                     htmlToSet = htmlToSet  + makeTournamentTreeHTML(dirTree[keys],keys);                    
                     htmlToSet = htmlToSet + '</li>';                    
		}           

		jQuery('.filetree').html(htmlToSet );
              jQuery('.filetree').treeview();
		jQuery("#tPicMonthFolder").live("click", function() {
                        getTournamentPhotoNamesInDir(jQuery(this).attr('href'));
                        return false;
		});
              jQuery('#tPicMonthFolder').click(); 

	}

	function makeTournamentTreeHTML(dirArr,parentFolderName ) {
              var subFolderName  = '';
              var htmlTour = '';
              var dirNames = dirArr.split(comma);
              dirNames.sort();
              dirNames.reverse();
              var max = dirNames.length;
              var cur = 0;
            	while(cur < max) {
                     subFolderName = dirNames[cur];
                     subFolderName = subFolderName.replace(parentFolderName + '/',empty);
			htmlTour = htmlTour + '<ul><li><span class="file"><a id="tPicMonthFolder" href="' +  dirNames[cur] + quotes + '>' + subFolderName + '</a></span></li></ul>';
                     cur++;
		}
              return htmlTour;
	}

	function getTournamentPhotoListings() {
		//generate the parameter for the php script
		var data = null;
		jQuery.ajax({
			url : "/php/browseTournamentPictures.php",
			type : "GET",
			data : data,
			cache : false,
			success : function(phpData) {			
				makeYearTreeHTML(parseDirs(phpData));
			}
		});
	}

	function getTournamentPhotoNamesInDir(dirName) {
		//generate the parameter for the php script
              var rootPath = document.location.toString();
              var loc = rootPath.lastIndexOf("/");
              rootPath = rootPath.substring(0,loc+1);             
              loc = dirName.indexOf(rootPath);
              if(-1 != loc){
                  dirName= dirName.substring(rootPath.length);
              }     
              var data = "dirName=" + dirName;                      
		jQuery.ajax({
			url : "/php/openATournament.php/?" + data,
			type : "GET",
			data : null,
			cache : false,
			success : function(phpData) {
				showPictureNames(dirName,phpData);
			}
		});
	}

	function showPictureNames(dirName,phpData) {
           makeDivHTML(dirName,phpData);
	}

              var imageNamesProcessed = null;
              function displayPhoto(imageFullPath,dirImages,dirRoot){

                       // required for initial call
                       if(null != dirImages){
                           imageNamesProcessed = new Array();
		             var dirArr = dirImages.split(';');
      			      for(var index in dirArr){
				    imageNamesProcessed.push(dirRoot + dirArr[index ]);
                              }
                       }                       
                       jQuery('#photoImage').attr('src',imageFullPath);            
                       jQuery('#photoHolder').slideDown();

                       var db = document.body;
                       var dde = document.documentElement;
                       var docHeight = Math.max(db.scrollHeight, dde.scrollHeight, db.offsetHeight, dde.offsetHeight, db.clientHeight, dde.clientHeight);                      

                       var divHeight = jQuery('#photoImage').height();  
                       var divWidth = jQuery('#photoImage').width(); 

                       var clientHeight = jQuery(window).height();
                       var clientWidth = jQuery(window).width();      

                       var calcDivheight,calcDivWidth;

                       if(clientHeight < 500){
                           calcDivheight = Math.min(clientHeight*0.9, divHeight);
                       }else{
                           calcDivheight = Math.min(clientHeight*0.7, divHeight);
                       }
                       
                       if(calcDivheight < divHeight){ 
                           calcDivWidth = ((divWidth * calcDivheight)/divHeight);
                       }

                       var divTop = (clientHeight-calcDivheight)/2;
                       var divLeft = (clientWidth-calcDivWidth)/2;

                       jQuery('#photoHolder').css('top',jQuery(window).scrollTop() + divTop);
                       jQuery('#photoHolder').css('left',divLeft);
                       jQuery('#photoHolder').css('width',calcDivWidth);
                       jQuery('#photoHolder').css('height',calcDivheight);
                      
                       jQuery('#photoImage').css('width',calcDivWidth);
                       jQuery('#photoImage').css('height',calcDivheight);
                       //jQuery('#closePhotoBtn').css('left',calcDivWidth + (30 - jQuery('#closePhotoBtn').width()));

                       jQuery('#prevPhotoBtn').css('top',(calcDivheight/2)- (jQuery('#prevPhotoBtn').height()/2));
                       jQuery('#nextPhotoBtn').css('top',(calcDivheight/2)- (jQuery('#nextPhotoBtn').height()/2));
                       jQuery('#nextPhotoBtn').css('left',calcDivWidth + (30 - jQuery('#nextPhotoBtn').width()) );

                       jQuery('#nextPhotoBtn').show();
                       jQuery('#prevPhotoBtn').show();
                       // This is repeat call .. no need of opacity bgrnd
                       if(null != dirImages){
                           var overlay=document.createElement("div");
	                   overlay.setAttribute("id", "overlay");
                           var style = "width: 100%; height: " + docHeight  + "px; opacity: 0.7; background-color: #000;left: 0;position: absolute;top: 0;z-index: 10;";
	                   overlay.setAttribute("style",style );

                           document.body.appendChild(overlay);	
                           jQuery('.panelwrappers').hide();   
                           jQuery('equiproller').hide();    
                       }                              
             }

        function closePhoto() {

            jQuery('#overlay','body').each(function(i){ 
                jQuery(this).remove();             
            });
            jQuery('#photoHolder').slideUp();
            jQuery('#photoHolder').hide();
            jQuery('.panelwrappers').show();    
            jQuery('equiproller').show();        
        }

        function nextPhoto() {              
               var img = findNextOrPrevious(1);
               
               if(null != img){
                   displayPhoto(img,null,null);
                   jQuery('#nextPhotoBtn').show();
                   jQuery('#prevPhotoBtn').show();
               }else{
                   jQuery('#nextPhotoBtn').hide();
               }
        }

        function prevPhoto() {
               var img = findNextOrPrevious(0);
               if(null != img){
                   displayPhoto(img,null,null);
                   jQuery('#nextPhotoBtn').show();
                   jQuery('#prevPhotoBtn').show();
               }else{
                   jQuery('#prevPhotoBtn').hide();
               }
        }

	    function find(arrayObj,objToFind){
	        for(var i=0; i<arrayObj.length; i++){
	            if(arrayObj[i]==objToFind){
	                return i;
	            }
	        }
	        return -1;
	    }
        function  findNextOrPrevious(bNext){
            var curImage = jQuery('#photoImage').attr('src');
            
            var maxLen = imageNamesProcessed.length;
            var retImg = null;
            var index = find(imageNamesProcessed,curImage);

            if(bNext == 1 && ((index + 1) < maxLen-1)){
               retImg = imageNamesProcessed[index+1];
            }else if(bNext == 0 && ((index - 1) >= 0)){
                retImg = imageNamesProcessed[index-1];
            }                
	     return retImg;
        }

