﻿/*********************************************************
'Filename:      MediaControl.js
'Website:       etherstudios.com
'Created By:    Michael England
'Created Date:

Modified        By              Description
----------------------------------------------------------

*********************************************************/

var pageCurrent = 0;
var pageCount = 0;
var pageSize = 6;
var thumbnailWidth = 104;
var lightbox_image_array;
var lightbox_image_current = 0;
var lightbox_image_initiator;

function Media_Load(nImages) {
    pageCount = Math.ceil((nImages/pageSize) - 1);
    
    if(pageCount == 0)
    {
        document.getElementById("hlPagePrev").style.display = "none";
        document.getElementById("hlPageNext").style.display = "none";
    }
    
    document.getElementById("Thumbnails").style.width = ((pageCount+1) * (2 * thumbnailWidth)) + "px";
    
    if(document.getElementById("spnCaption").innerHTML == "" && document.getElementById("spnCredit").innerHTML == "")
        document.getElementById("divImageInfoWrapper").style.display = "none";
    else
        document.getElementById("divImageInfoWrapper").style.display = "block";
    
    //Effect.Appear('imgImage', {queue: 'front'});
    var image = document.getElementById("imgImage");
    image.onload = function() {Image_Load()};
}


function hlPagePrev_Click() {
    DisableElement("hlPagePrev", 200);
    if(pageCurrent > 0)
    {
        Effect.MoveBy("Thumbnails", 0, ((pageSize/3) * thumbnailWidth), {duration: 0.2});
        pageCurrent--;
    }
}

function hlPageNext_Click() {
    DisableElement("hlPageNext", 200);
    if(pageCurrent < pageCount)
    {
        Effect.MoveBy("Thumbnails", 0, (-1 * ((pageSize/3) * thumbnailWidth)), {duration: 0.2});
        pageCurrent++;
    }
}

function hlThumbnail_Click(whichpic, strAlt, strTitle, strCaption, strCredit, nImage) {
    
    Effect.Fade('imgImage', {queue: 'front', duration: 0.2});
    
    var divImageInfoWrapper = document.getElementById("divImageInfoWrapper");
    Effect.MoveBy("divImageInfoWrapper", divImageInfoWrapper.offsetHeight, 0, {duration: 0.2});
    
    setTimeout("Fade_Complete('" + whichpic.href + "','" + strAlt + "','" + strTitle + "','" + strCaption + "','" + strCredit + "'," + nImage + ")", 200);
    
    return false;
}

function Fade_Complete(strHref, strAlt, strTitle, strCaption, strCredit, nImage)
{    
    // set the captions
    if(strCaption != "") document.getElementById("spnCaption").innerHTML = Url.decode(strCaption);
    else document.getElementById("spnCaption").innerHTML = "";
    
    // set the credit
    if(strCredit != "") document.getElementById("spnCredit").innerHTML = Url.decode(strCredit);
    else document.getElementById("spnCredit").innerHTML = "";    
    
    if(strTitle != "") document.getElementById("imgImage").title = Url.decode(strTitle);
    else document.getElementById("imgImage").title = ""; 
    
    if(strCaption == "" && strCredit == "") 
        document.getElementById("divImageInfoWrapper").style.display = "none";
    else
        document.getElementById("divImageInfoWrapper").style.display = "block";
        
    
    lightbox_image_current = nImage;
    
    var imgImage = document.getElementById("imgImage");
    imgImage.src = strHref;
    imgImage.alt = strAlt;
    imgImage.onload = function() {Image_Load()};
}

function Image_Load()
{
    var divImage = document.getElementById("divImage");
    var imgImage = document.getElementById("imgImage");
    var newImage = new Image();
    newImage.src = imgImage.src;

    var width = newImage.width;
    var height = newImage.height;
    var divHeight = divImage.offsetHeight - 2
    var divWidth = divImage.offsetWidth - 2;
    var newHeight = 0;
    var newWidth = 0;
    var imageRatio = newImage.width / newImage.height;
    var destinationRatio = divWidth / divHeight;
    
    if (imageRatio > destinationRatio) {
        newWidth = divWidth;
        newHeight = Math.round(divWidth / imageRatio);
    } else if (imageRatio < destinationRatio) {
        newWidth = Math.round(divHeight * imageRatio);
        newHeight = divHeight;
    } else {
        newWidth = divWidth;
        newHeight = divHeight;
    }
    
    imgImage.style.width = newWidth + "px";
    imgImage.style.height = newHeight + "px";
    
    imgImage.style.top = Math.round((divImage.offsetHeight - newHeight) / 2) + "px";
    imgImage.style.left = Math.round((divImage.offsetWidth - newWidth) / 2) + "px";
    
    Effect.Appear('imgImage', {queue: 'front', duration: 0.2});
    
    var divImageInfoWrapper = document.getElementById("divImageInfoWrapper");
    divImageInfoWrapper.style.top = divImage.offsetHeight + "px";
    divImageInfoWrapper.style.height = document.getElementById("divImageInfo").offsetHeight + "px";
    Effect.MoveBy("divImageInfoWrapper", (divImageInfoWrapper.offsetHeight * -1), 0, {duration: .2});
    
}

function hlImage_Click()
{
    return false;
}

//function lightbox_init(){
//    hide_selects();
//    
//    lightbox_pager_links();
//    
//    var lightbox_bg = document.getElementById("lightbox_bg");
//    lightbox_bg.style.height = document.body.clientHeight + "px";
//    lightbox_bg.style.width = document.body.clientWidth + "px";
//    
//    lightbox_image_initiator = lightbox_image_current;
//    document.getElementById("lightbox_image").src = lightbox_image_array[lightbox_image_current];
//    Effect.Appear("lightbox_bg", {queue: 'start',from:0,to:0.85});
//    Effect.Appear("lightbox_wrapper", {queue: 'end',from:0,to:1});
//    return false;
//}

//function lightbox_close(){
//    lightbox_image_current = lightbox_image_initiator;
//    Effect.Fade("lightbox_wrapper", {queue: 'start'});
//    Effect.Fade("lightbox_bg", {queue: 'end'});
//    setTimeout("show_selects();",1000);
//    return false;
//}

//function lightbox_previous(){
//    if(lightbox_image_current > 0)
//    {
//        lightbox_image_current--;
//        Effect.Fade('lightbox_image', {queue: 'front'});
//        setTimeout("lightbox_previous_fade_complete()", 1000);
//    }
//    
//    lightbox_pager_links();
//    return false;
//}

//function lightbox_previous_fade_complete(){
//    document.getElementById("lightbox_image").src = lightbox_image_array[lightbox_image_current];
//    document.getElementById("lightbox_image").onload = function() {
//        Effect.Appear('lightbox_image', {queue: 'front'});
//    }
//}

//function lightbox_next(){
//    if(lightbox_image_current < lightbox_image_array.length - 1) 
//    {
//        lightbox_image_current++;
//        Effect.Fade('lightbox_image', {queue: 'front'});
//        setTimeout("lightbox_next_fade_complete()", 1000);
//    }
//    lightbox_pager_links();
//    return false;
//}

//function lightbox_next_fade_complete(){
//    document.getElementById("lightbox_image").src = lightbox_image_array[lightbox_image_current];
//    document.getElementById("lightbox_image").onload = function() {
//        Effect.Appear('lightbox_image', {queue: 'front'});
//    }
//}

//var old_click_previous;
//var old_click_next;

//function lightbox_pager_links(){

//    var object_previous = document.getElementById("lightbox_previous");
//    var object_next = document.getElementById("lightbox_next");
//    
//    if(lightbox_image_current == 0)
//    {
//        object_previous.style.opacity = ".5";
//        object_previous.style.filter = "alpha(opacity=50)";
//    } else {
//        object_previous.style.opacity = "1";
//        object_previous.style.filter = "alpha(opacity=100)";
//    }

//    if(lightbox_image_current == lightbox_image_array.length - 1)
//    {
//        object_next.style.opacity = ".5";
//        object_next.style.filter = "alpha(opacity=50)";
//    } else {
//        object_next.style.opacity = "1";
//        object_next.style.filter = "alpha(opacity=100)";
//    }
//}

// ENABLE/DISABLE ELEMENT
var onclick;

function DisableElement(elementID, timeout){
    var object = document.getElementById(elementID);
    onclick = object.onclick;
    object.onclick = "return false";
    object.style.opacity = ".5";
    object.style.filter = "alpha(opacity=50)"
    
    if(timeout>0){setTimeout("EnableElement('" + elementID + "')",timeout);}
}

function EnableElement(elementID){
    var object = document.getElementById(elementID);
    object.onclick = onclick;
    object.style.opacity = "1";
    object.style.filter = "alpha(opacity=100)"
}
// /ENABLE/DISABLE ELEMENT

var Url = {
 
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
 
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string.replace(/\+/g, " ")));
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
			var c = string.charCodeAt(n);
			if (c < 128) {
				utftext += String.fromCharCode(c);
			} else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			} else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
		}
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
			c = utftext.charCodeAt(i);
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			} else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			} else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
		}
		return string;
	}
}
