function findInput(typeName, nameToFind) {
    var inputObj = document.getElementsByTagName("input");
    for (var i = 0; i < inputObj.length; i++) {
        if (inputObj[i].type == typeName) {
            var ioId = inputObj[i].id;
            if (ioId.indexOf(nameToFind) > 0) {
                return inputObj[i];
            }
        }
    }
}

	function toggleCallFrame() {
	    var lis = document.getElementById('demo').getElementsByTagName('li');
	    for (var i = 0; i < lis.length; i++) {
	        if(lis[i].className == "active") {
	            var hf = lis[i].getElementsByTagName('input')[0].value;
	            var tbCalling = findInput("text", "tbCallingFrame");
                if (tbCalling != null) {
                    tbCalling.value = hf;
                }
	        }
	    }
	}

$(document).ready(function(){
	
	$('.gallery_demo_unstyled').addClass('gallery_demo'); // adds new class name to maintain degradability
	
	$('ul.gallery_demo').galleria({
		history   : false, // activates the history object for bookmarking, back-button etc.
		clickNext : true, // helper for making the image clickable
		insert    : '#main_image', // the containing selector for our main image
		onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
			
			// fade in the image & caption
			image.css('display','none').fadeIn(1000);
			caption.css('display','none').fadeIn(1000);
			
			// fetch the thumbnail container
			var _li = thumb.parents('li');
			
			// fade out inactive thumbnail
			_li.siblings().children('img.selected').fadeTo(500,0.3);
			
			// fade in active thumbnail
			thumb.fadeTo('fast',1).addClass('selected');
			
			// add a title for the clickable image
			image.attr('title','Next image >>');
			
			// update the description area
			document.getElementById('desc_area').innerHTML =  caption.text();
		    
		    // can i add the height and width attributes to the image here or no?
            var h = image.height();
            var w = image.width();
		    image.attr('height', h);
		    image.attr('width', w);
		    var h2 = h + 13;
		    var w2 = w + 17;
		    var curHtml = image.parent()[0].innerHTML;
		    //image.parent()[0].innerHTML = "<u id='sh' style='width:" + w2 + "px;height:" + h2 + "px'><u><u><u><u>" + curHtml + "</u></u></u></u></u>";
		    var beforePart = "<u id='sh' style='width:" + w2 + "px;height:" + h2 + "px'><u><u><u><u>";
		    var afterPart = "</u></u></u></u></u>";
		    image.wrap(beforePart + afterPart);
		    var w3 = w2 + 10;
		    var strw3 = "width:" + w3 + "px";
		    document.getElementById('demo').setAttribute('style', strw3);
		    // update the calling art id
		    toggleCallFrame();
		    // reset the ratings
			Ratings.init();
		},
		onThumb : function(thumb) { // thumbnail effects goes here
			
			// fetch the thumbnail container
			var _li = thumb.parents('li');
			
			// if thumbnail is active, fade all the way.
			var _fadeTo = _li.is('.active') ? '1' : '0.3';
			
			// fade in the thumbnail when finnished loading
			thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
			
			// hover effects
			thumb.hover(
				function() { thumb.fadeTo('fast',1); },
				function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
			)
		}
	});
});
