$(document).ready(function() {
    $("#loginform").click(function(e){
        e.stopPropagation();
        return $(e.target).is('input:image');  
    });

    $("#preloginBtn").click(function() {                
        if ($('#loginform').position().top == -200) {                     
            $("#loginform")
                .css('top', -78)
                .animate({ top: 0 }, 400, function(){                
                    $(document).one('click', function(){
                        $("#loginform").animate({ top: -78 }, 400, function(){
                            $(this).css('top', -200);
                    });
                });
            });                        
        }
    });
    
    $('#searchbox input.querytxt').focus(function(){ 
        if($.trim(this.value) == 'SEARCH') this.value='';    
        $(this).addClass('focus');  
    }).blur(function(){
        if($.trim(this.value) == '') {
            this.value='SEARCH';    
            $(this).removeClass('focus');
        }
    }).keypress(function(e){
        if(e.which == 13) {
            var q = $.trim(this.value);
            location.href =  '/search.aspx?q=' + Encoder.encode(q);
            return false;
        }
    });
    
    $('ul.gallery').addClass('mainImg_gallery'); // adds new class name to maintain degradability
    var galLi = $('ul.gallery li').length;
    $('ul.gallery li').eq($.randumRange(1,galLi-1)).addClass('active');
		
		$('ul.mainImg_gallery').galleria({
			history   : false, // activates the history object for bookmarking, back-button etc.
			clickNext : false, // helper for making the image clickable
			insert    : '.mainimage', // 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
				//if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
					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 >>');
			},
			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
				)
			}
		});

    /* This is basic fancybox - uses default settings */
	
	$("a.galleryImg").fancybox({
		'titleShow'	: false
	});
	
	$("a.disclaimer").fancybox();
	
	/* Using custom settings */
	
	$("a#inline").fancybox({
		'hideOnContentClick': true
	});

	$("a.group").fancybox({
		'titleShow'	: false,		
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	false
	});
    
});


var Encoder = {
 
	// 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));
	},
 
	// 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;
	}
 
}

function historyBack() {
    history.back();
}
