﻿var gID=0;
var currentScroll=0;
function getGallery(theGallery){
	gID=theGallery;
	currentScroll = document.documentElement.scrollTop;
	document.getElementById('currentPageVar').value='mainGallery';
	try {
			var pageTracker = _gat._getTracker("UA-9356223-1");
			pageTracker._trackPageview("/openGallery_" + gID + ".aspx");
		} catch(err) {}
    sendGalleryRequest('../scripts/gallery.source.htm',theGallery);

}
function getStructure(theGallery){
	document.getElementById('currentPageVar').value='mainGalleryStructure';
    sendGalleryRequest('../scripts/gallery.structure.source.htm',0);
}

function sendGalleryRequest(doc,theGallery){
	
    // check for existing requests
    if(xmlobj!=null&&xmlobj.readyState!=0&&xmlobj.readyState!=4){
        xmlobj.abort();
    }
    try{
        // instantiate object for Firefox, Nestcape, etc.
        xmlobj=new XMLHttpRequest();
    }
    catch(e){
        try{
            // instantiate object for Internet Explorer
            xmlobj=new ActiveXObject('Microsoft.XMLHTTP');
        }
        catch(e){
            // Ajax is not supported by the browser
            xmlobj=null;
            return false;
        }
    }
    // assign state handler
    xmlobj.onreadystatechange=galleryStateChecker;
    // open socket connection
    xmlobj.open('GET',doc,true);
    // send request
    xmlobj.send(null);
    
    
}
function galleryStateChecker(){
	
    // if request is completed
    if(xmlobj.readyState==4){
        // if status == 200 display text file
        if(xmlobj.status==200){
            // create data container
            createGalleryDataContainer();
            // display data into data container

            if (document.getElementById('currentPageVar').value == 'mainGalleryStructure'){
	            data=xmlobj.responseText.split('|');
	            //$('#mainMidContent').html(data[0]);
	            //initGallery();
				
				getGalleryXml(gID,data[0]);
	        }
            data=xmlobj.responseText.split('|');
            displayGalleryData();
            
        }
        else{
            alert('Failed to get response :'+ xmlobj.statusText);
        }
    }
}

function createGalleryDataContainer(){
	
	if (document.getElementById('currentPageVar').value == 'mainGallery'){
		var div=document.createElement('div');
		div.setAttribute('id','container');
		// bodyWidth = Browser Width
		bodyWidth = document.body.offsetWidth;
		// minContactWidth = Minimum Dialoge Width
		minContactWidth = '1000';
		if (bodyWidth > minContactWidth){
			offset=((bodyWidth-minContactWidth) / 2);
		}else{
			offset=0;
		}
		bodyHeight = document.body.offsetHeight;
		if(div.style){
			div.style.width=minContactWidth + 'px';
			div.style.height=bodyHeight + 'px';
			div.style.position='absolute';
			div.style.top='0px';
			div.style.left=offset + 'px';
			div.className='contactMain';
			document.getElementsByTagName('body')[0].appendChild(div);
			var insideDiv=document.createElement('div');
			insideDiv.setAttribute('id','insideDiv');
			insideDiv.style.width=div.style.width;
			insideDiv.style.height=div.style.height;
			document.getElementById('container').appendChild(insideDiv);
			var insideDiv2=document.createElement('div');
			insideDiv2.setAttribute('id','insideDiv2');
			insideDiv2.style.width=div.style.width;
			insideDiv2.style.height=div.style.height;
			insideDiv2.style.position='absolute';
			if (checkIE() == 1){
				divInnerHTML=fixIE6('img/contactBG.png',minContactWidth,bodyHeight);
			}else{
				divInnerHTML = "<img src='../img/contactBG.png' width='" + minContactWidth + "px' height='" + bodyHeight + "px' alt='' />";
				}
			insideDiv2.innerHTML = divInnerHTML;
			document.getElementById('insideDiv').appendChild(insideDiv2);
			var insideDiv3=document.createElement('div');
			insideDiv3.setAttribute('id','insideDiv3');
			insideDiv3.style.width=div.style.width;
			insideDiv3.style.height=div.style.height;
			insideDiv3.style.position='absolute';
			document.getElementById('insideDiv').appendChild(insideDiv3);
			var insideDiv4=document.createElement('div');
			insideDiv4.setAttribute('id','insideDiv4');
			insideDiv4.className='contactInside';
			document.getElementById('insideDiv3').appendChild(insideDiv4);
		}
	}
}
function displayGalleryData(){

    //alert(document.getElementById('currentPageVar').value);
	if (document.getElementById('currentPageVar').value == 'mainGallery'){
		var theDiv = 'insideDiv3';
	}
	if(theDiv){
        i=0;
        if(i==data.length){i=0};

        document.getElementById(theDiv).innerHTML=data[i];
        //galleryTopContainer
        //alert(currentScroll);
        //.css({borderColor: 'red', borderWidth: '1px'});
        //$('#galleryTopContainer').css({margin-top:'500px'});
        $('#contactusMainContainer').css({marginTop:(currentScroll+50) + 'px'});
        $('#insideDiv3').hide().fadeIn(200);
        $('#mainMidContent').html("<div class='galleryLoader'></div>");
        getStructure();

		
       //var pic1= new Image();
       //pic1.onload = function() {
       //    fTest(pic1.src);
       //};
//
       //pic1.src="../img/1x1.jpg";

	
	}


	
}

function fTest(picSrc){
       //getStructure();
	   
       //$('#mainMidContent').html("<img src='" + picSrc + "' width='90' height='90' />");
       //xmlDataReader();
}



function xmlDataReader(theID){
    var doc = '../gallery.aspx?gID=' + theID;
    
    if(xmlobj!=null&&xmlobj.readyState!=0&&xmlobj.readyState!=4){
        xmlobj.abort();
    }
    try{
        // instantiate object for Firefox, Nestcape, etc.
        xmlobj=new XMLHttpRequest();
    }
    catch(e){
        try{
            // instantiate object for Internet Explorer
            xmlobj=new ActiveXObject('Microsoft.XMLHTTP');
        }
        catch(e){
            // Ajax is not supported by the browser
            xmlobj=null;
            return false;
        }
    }
    
    xmlobj.onreadystatechange=galleryDataStateChecker;
    xmlobj.open('GET',doc,false);
    xmlobj.send(null);
    xmlData=xmlobj.responseXML;
    var root = xmlData.getElementsByTagName("item");
	var theValues = new Array(root.length);
    for (i = 0; i < root.length; i++) {
        theValues[i] = root[i].childNodes[0].nodeValue;
        //alert(theValues[i]);
    }
	return theValues;
}
function galleryDataStateChecker(){
    // if request is completed
    
    if(xmlobj.readyState==4){
        // if status == 200 display text file
        if(xmlobj.status==200){
            // create data container
            //createGalleryDataContainer();
            // display data into data container
            //data=xmlobj.responseText.split('|');
            //displayGalleryData();
            
            //xmlobj.load;
            
            //alert('state');
            
        }
        else{
            alert('Failed to get response :'+ xmlobj.statusText);
        }
    }
}

function getGalleryXml(theID, theHTML){
	//$('#mainMidContent').html(theHTML)
	//initGallery();
	theValues = xmlDataReader(theID);
	imageObj = new Array(theValues.length);
    var loadCount=0;
    var currentImg = "";
    htmlBuilder="<ul class='sc_menu'>";
	
	for (i = 0; i < theValues.length; i++) {
		
		if (theValues[i].indexOf('galleryHeader') != '-1'){
			picSrc = "../img/"+theValues[i];
		}else if(theValues[i].indexOf('mnuItem') != '-1'){
		    picSrc = "../gallery/common/"+theValues[i];
		}else{
		    picSrc = "../gallery/"+theID+"/"+theValues[i];
		}
		if (i==0){
		    currentImg=picSrc;
		}
		imageObj[i] = new Image();
		if(theValues[i].indexOf('_tn') != '-1'){
			imgRoot=theValues[i].split("_");
		    htmlBuilder=htmlBuilder + "<li><a href='javascript:getImg(" + '"' + imgRoot[0] + '"' + ", " + '"' + gID + '"' + ")'><img src='" + picSrc + "' alt=''/></a></li>";
		}
		imageObj[i].onload = function() {
			loadCount = (loadCount + 1);
		    if (loadCount == theValues.length){
			    $('#mainMidContent').hide().html(theHTML).fadeIn(500);
			    htmlBuilder=htmlBuilder+"</ul>";
                $('#gallerySlider').html(htmlBuilder);
				//alert(htmlBuilder);
                $('#galleryDisplay').html("<img src='" + currentImg + "' />");
				initGallery();
				initMenu();
			}
       };
		imageObj[i].src=picSrc;
        //alert(loadCount);
		
    }
}
function getImg(theImg, theID){
	//alert(theImg + ", " + theID);
	try {
			var pageTracker = _gat._getTracker("UA-9356223-1");
			pageTracker._trackPageview("/viewGallery_" + theID + "_pic_" + theImg + ".aspx");
		} catch(err) {}
	$('#galleryDisplay').html("<div id='tmpContainer' class='galleryTopGfxNB'><div class='gfxWindowLoader'></div></div>");
	var newGfx = new Image();
	newGfxSrc = "../gallery/" + theID + "/" + theImg + ".jpg";
	newGfx.onload = function(){
		$('#tmpContainer').hide();
		$('#tmpContainer').html("<img src='" + newGfxSrc + "' />");
		$('#tmpContainer').fadeIn(300);
		
	}
	newGfxSrc = "../gallery/" + theID + "/" + theImg + ".jpg";
	newGfx.src=newGfxSrc;
	//$('#galleryDisplay').html("<img src='" + currentImg + "' />");
}
function getGal(theID){
    try {
			var pageTracker = _gat._getTracker("UA-9356223-1");
			pageTracker._trackPageview("/viewGallery_" + theID + ".aspx");
		} catch(err) {}
	$('#galleryDisplay').html("<div id='tmpContainer' class='galleryTopGfxNB'><div class='gfxWindowLoader'></div></div>");
	$('#mainGalleryContainer').html("<div id='tmpMnuContainer' class='galleryBottomContainerNB'><div class='mnuWindowLoader'></div></div>");
    theVals = xmlDataReader(theID);
	imageObj = new Array(theVals.length);
    var loadCount=0;
    var currentImg = "";
    htmlBuilder="<ul class='sc_menu'>";
	
	for (i = 0; i < theVals.length; i++) {
		
		if (theVals[i].indexOf('galleryHeader') != '-1'){
			picSrc = "../img/"+theVals[i];
			//alert(picSrc);
		}else if(theVals[i].indexOf('mnuItem') != '-1'){
		    picSrc = "../gallery/common/"+theVals[i];
		}else{
		    picSrc = "../gallery/"+theID+"/"+theVals[i];
		}
		if (i==0){
		    currentImg=picSrc;
		}
		//alert(picSrc);
		imageObj[i] = new Image();
		if(theVals[i].indexOf('_tn') != '-1'){
			imgRoot=theVals[i].split("_");
		    htmlBuilder=htmlBuilder + "<li><a href='javascript:getImg(" + '"' + imgRoot[0] + '"' + ", " + '"' + theID + '"' + ")'><img src='" + picSrc + "' alt=''/></a></li>";
		}
		imageObj[i].onload = function() {
			loadCount = (loadCount + 1);
			//alert(loadCount + "/" + theVals.length);
		    if (loadCount == theVals.length){
			    htmlBuilder=htmlBuilder+"</ul>";
                $('#gallerySlider').html(htmlBuilder);
				//alert(htmlBuilder);
				$('#mainGalleryContainer').hide().html("<div id='gallerySlider' class='sc_menu'></div>");
				$('#gallerySlider').html(htmlBuilder);
				
				$('#galleryDisplay').hide();
				$('#galleryDisplay').html("<img src='" + currentImg + "' />");
				

                //$('#galleryDisplay').html("<img src='" + currentImg + "' />");
				
				$('#mainGalleryContainer').fadeIn(300);
				$('#galleryDisplay').fadeIn(300);
				initGallery();
				initMenu();
			}
       };
		imageObj[i].src=picSrc;
        //alert(loadCount);
		
    }
	//alert(theVals[0]);
}
<!-- BEGIN GALLERY HOR MENU CODE -->
function initGallery(){

  //Get our elements for faster access and set overlay width
  var div = $('div.sc_menu'),
               ul = $('ul.sc_menu'),
               // unordered list's left margin
               ulPadding = 0;

  //Get menu width
  var divWidth = div.width();

  //Remove scrollbars
  div.css({overflow: 'hidden'});

  //Find last image container
  var lastLi = ul.find('li:last-child');

  //When user move mouse over menu
  div.mousemove(function(e){

    //As images are loaded ul width increases,
    //so we recalculate it each time
    var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;

    var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
    div.scrollLeft(left);
  });
}
<!-- END GALLERY HOR MENU CODE -->

<!-- BEGIN MENU HOR CODE -->
function initMenu(){

  //Get our elements for faster access and set overlay width
  var div = $('div.ms_menu'),
               ul = $('ul.ms_menu'),
               // unordered list's left margin
               ulPadding = 0;

  //Get menu width
  var divHeight = div.height();

  //Remove scrollbars
  div.css({overflow: 'hidden'});

  //Find last image container
  var lastLi = ul.find('li:last-child');

  //When user move mouse over menu
  div.mousemove(function(e){

    //As images are loaded ul width increases,
    //so we recalculate it each time
    //var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;
	var ulHeight = lastLi[0].offsetTop + lastLi.outerHeight() + ulPadding;
	

    //var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
	//alert( divHeight);
	var up = (e.pageY - div.offset().top) * (ulHeight-divHeight) / divHeight;
    div.scrollTop(up);
	//alert('test');
  });
}
<!-- END MENU HOR CODE -->