// JavaScript Document

var currentpos=0;
var currentthumbpos=0;
var moveamount=106;
var thumbnailnum;
var maxthumbvisible = 3; //Define how many thumbnails will be visible at one time in the thumbbox--for now this should only be 3 since it's actually the css that controls the size of the viewable box
var current_imgid=0;
var moving = false;
var finishedsizing=0;
var preloadimg="no"//Preload images ("yes" or "no"):
var myloadedimage = new Array();
     myloadedimage[0]=1;
var imggallery=new Array()
var firstimagestart=1;
var nextorprev=0;
var fadeout=1;

function loadinfo(){
	if(fadeout==0){
        //alert('fadeout==0');
		var fademe = new Fx.Style('iptc_info','opacity', {duration:transspeed });
		fademe.set(0);
		fadeout=1;
	}else{
        //alert('fadeout = '+fadeout);
		var fademe = new Fx.Style('iptc_info','opacity', {duration:transspeed });
		fademe.set(.75,0);
		fadeout=0;
	}
	
}
function areweready(loadarea, imgindex, img_id){
	if ( finishedsizing++ ) { 
		modifyimage(loadarea, imgindex, img_id);
		finishedsizing = 0;
	}
}

function modifyimage(loadarea, imgindex, img_id ){
	function loadimagenow(){
		//alert("Load Image Now Called");
		if (document.getElementById) {
				if(current_imgid!=img_id){
					
					var imgobj=document.getElementById(loadarea);
					var iptch=document.getElementById('iptc_info');
					var photonum=document.getElementById('photocount')
					var phototitle=document.getElementById('imgtitle')
					imgobj.innerHTML=returnimgcode(imggallery[imgindex]);
					iptch.innerHTML=returniptc(imggallery[imgindex]);
					photonum.innerHTML=(Number(imgindex)+1)+" of "+imggallery.length+" Photos";
					phototitle.innerHTML="<strong>"+tempgallery[img_id][4]+"</strong><br /> "+tempgallery[img_id][9];
					initImage(loadarea);
					current_imgid=img_id;
					myloadedimage[imgindex]=1;
				}
			}
		return false
	}
	if(myloadedimage[imgindex]==null){	
        new Asset.image(imggallery[imgindex][0], {onload: loadimagenow});
	}else{
		loadimagenow();
	}
	
}

function returnimgcode(theimg){
	var imghtml=""
	if (theimg[1]!="")
		imghtml=''
	imghtml+='<img src="'+theimg[0]+'" border="0" id="'+theimg[8]+'" />'
	if (theimg[1]!="")
		imghtml+=''
	return imghtml
}
function returniptc(theimg){
    if( theimg[7]=='' ){
        var iptchtml = '<div class=\'iptc_left\'><em>No description</em></div>';	
    } else {
        var iptchtml = '<div class=\'iptc_left\'>'+theimg[7]+'</div>';	
    }
    // var iptchtml = ''+
	// '<div class=\'iptc_left\'>'+
        //'	Title <br/>'+
        //'	Author <br/>'+
        //'	Copyright <br/><br/>'+
        //'	Description: '+
        // '</div>'+
        // '<div class=\'iptc_right\'>'+
        //'	'+theimg[4]+'<br/>'+ //title
        //'	'+theimg[5]+'<br/>'+ // author
        //'	'+theimg[6]+'<br/><br/>'+ //copyright
        // '	'+theimg[7]+ // description
	// '</div>';
	return iptchtml
}

function initImage(imageId) {
	var fader = new Fx.Style(imageId,'opacity', {duration:fadespeed});
	fader.set(0);
	fader.start(0,1);
	var titlefade = new Fx.Style('imgtitle','opacity', {duration:transspeed });
		titlefade.set(0);
		titlefade.start(0,1);
	var fadeiptc = new Fx.Style('iptc_btn','opacity', {duration:transspeed });
		fadeiptc.set(0);
		fadeiptc.start(0,1);
	if(fadeout==0){
		var fademe = new Fx.Style('iptc_info','opacity', {duration:transspeed });
		fademe.set(0);
		fadeout=1;
	}else{
		var fademe = new Fx.Style('iptc_info','opacity', {duration:transspeed });
		fademe.start(0,1);
		fadeout=0
	}
}

function checkbutton(mynum){
    //alert('mynum:'+mynum+' thumbnailnum:'+thumbnailnum+' maxthumbvisible:'+maxthumbvisible+' ');
 	if ( mynum == 0 ) {
		mm_shl('eback','hidden');
		mm_shl('emore','visible');
	} else if ( mynum < thumbnailnum - maxthumbvisible ) {
		mm_shl('eback','visible');
		mm_shl('emore','visible');
	} else {
		mm_shl('eback','visible');
		mm_shl('emore','hidden');
	}
}
function checknext(mynum){
	thumbmax=(Number(thumbnailnum)-1);
	if ( mynum < 1 ) {
        //alert('hidden & visible');
		mm_shl('prev','hidden');
		mm_shl('next','visible');
	} else if ( mynum <  thumbmax ) {
        //alert('visible & visible');
		mm_shl('prev','visible');
		mm_shl('next','visible');
	} else {
        //alert('visible & hidden');
		mm_shl('prev','visible');
		mm_shl('next','hidden');
	}
}

function mm_shl() { //v6.0
	var obj,args=arguments;
	if ((obj=MM_findObj(args[0]))!=null) {
		if (obj.style) {
			obj=obj.style;
		}
		obj.visibility=args[1];
	}
}


function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function addposition(addwidth){
	if ( !moving ) {
		 // if animagic is still moving the image..don't update the current position till it's done
		if(addwidth=="minus"){
			currentpos-=1;
		}else if(addwidth=="plus"){
			currentpos+=1;
		}
	}
	return currentpos;
}
function movethumbs(way){
	if(way=='plus'){
		move=(currentthumbpos+moveamount);
		var movethumbs = new Fx.Styles('thumbgall', {duration: transspeed, transition: Fx.Transitions.quadOut});
		movethumbs.start({ left: [currentthumbpos, move]});
		currentthumbpos+=moveamount;
	
	}else if(way=='minus'){
		move=(currentthumbpos-moveamount);
		var movethumbs = new Fx.Styles('thumbgall', {duration: transspeed, transition: Fx.Transitions.quadOut});
		movethumbs.start({ left: [currentthumbpos, move]});
		currentthumbpos-=moveamount;		
	}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
var thumbopen=0
	function thumbs(){
		var resizethumb = new Fx.Styles('thumbhide',{duration:transspeed, transition: Fx.Transitions.quadOut});
		var movethumbs = new Fx.Styles('thumbbox', {duration: transspeed, transition: Fx.Transitions.quadOut});
		if(thumbopen==1){	
			
			resizethumb.start({'height': 119});
			movethumbs.start({ 'top': [-120, 0]});
			thumbopen=0
		}else{
			resizethumb.start({'height': 0});
			movethumbs.start({ 'top': [0, -120]});
			thumbopen=1
		}
	}
    
    

    //  ECARD
    function sendEcard(){	
        var ecardForm = document.getElementById('ecardForm');
        ecardForm.style.display = 'block';
    }
    
    function closeEcard(){
        var ecardForm = document.getElementById('ecardForm');
        var ecard_message   = document.getElementById('ecard_message');
        var sender_name     = document.getElementById('sender_name');
        var sender_email    = document.getElementById('sender_email');
        var recipient_name  = document.getElementById('recipient_name');
        var recipient_email = document.getElementById('recipient_email');
        var headline        = document.getElementById('headline');
        
        ecard_message.innerHTML= '';
        sender_name.value      = '';
        sender_email.value     = '';
        recipient_name.value   = '';
        recipient_email.value  = '';
        headline.value         = '';
        ecardForm.style.display = 'none';
    }

    // Get the HTTP Object
    function getHTTPObject(){
        if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
        else if (window.XMLHttpRequest) return new XMLHttpRequest();
        else {
            alert("Your browser does not support AJAX.");
            return null;
        }
    }

    // Change the value of the outputText field
    function setOutput(){
        if(httpObject.readyState == 4){
            var response = httpObject.responseText;
            var ecard_message = document.getElementById('ecard_message');
            ecard_message.innerHTML = response;
        }
    }

    // Implement business logic    
    function doWork(){    
        httpObject = getHTTPObject();
        if (httpObject != null) {
            var send_ecard = document.getElementById('send_ecard');
            var children = send_ecard.childNodes;
            var url = '';
            for(i=0; i<children.length; i++){
                var elm = children[i]; 
                if( elm && typeof elm.tagName != 'undefined' ){
                    if( elm.tagName.toUpperCase()=='INPUT' ){
                        url += elm.name + '=' + elm.value + '&';
                    }                
                    if( elm.tagName.toUpperCase()=='TEXTAREA' ){
                        if( elm.value=='' ) {
                            url += elm.name + '=' + elm.innerHTML + '&';
                        } else {
                            url += elm.name + '=' + elm.value + '&';
                        }
                    }    
                }                
            }
            url += 'photo_url=' + tempgallery[current_imgid][0];
            link = "/e2gallery/ajax/sendecard.ajax.php?" + url;
            httpObject.open("GET", link , true);
            httpObject.onreadystatechange = setOutput;
            httpObject.send(null);
        }
    }