
/* Logging */

function log() {
    return window.console && console.log && console.log.apply(console, arguments);
}

/* Parse Comment Data - Fn */
var commentData=(function(){var c=function(a){var b=[],node=a.firstChild;if(!node){return b}do{if(node.nodeType===8){b[b.length]=node}if(node.nodeType===1){b=b.concat(c(node))}}while(node=node.nextSibling);return b},cache=[0],expando='data'+ +new Date(),data=function(a){var b=a[expando],nextCacheIndex=cache.length;if(!b){b=a[expando]=nextCacheIndex;cache[b]={}}return cache[b]};return function(a){a=a||document.documentElement;if(data(a)&&data(a).commentJSON){return data(a).commentJSON}var b=c(a),len=b.length,comment,cData;while(len--){comment=b[len];cData=comment.data.replace(/\n|\r\n/g,'');if(/^\s*?\{.+\}\s*?$/.test(cData)){try{data(comment.parentNode).commentJSON=(new Function('return '+cData+';'))()}catch(e){}}}return data(a).commentJSON||true}})();


/* New jQuery equalHeights plugin
   For equalising column heights on Game-page */

jQuery.fn.equalHeights = (function() {
    
    var _loaded;
    
    /* We monitor the window.onload event
       (Images need to be loaded if images are within collection) */
    jQuery(window).load(function(){
        _loaded = true;
    });
    
    /* Applies the highest element's height (within the collection)
      to all elements within the collection; will use minHeight if available */
    
    var minHeightSupported = (function(){
        var d = $('<div/>').css('minHeight',1).appendTo('body'),
            h = d.height();
        d.remove();
        return +h === 1;
    })();
    
    return function(explicit) {
        var self = this;
        if (!_loaded && $('img', self)[0]) {
            var interval = setInterval(function(){
                if (_loaded) {
                    clearInterval(interval);
                    self.equalHeights(explicit);
                }
            }, 50);
        }
        return this.css(minHeightSupported && !explicit ? 'min-height' : 'height',
            Math.max.apply(null,
                jQuery(jQuery.extend({},this)).map(function(){
                    return jQuery(this).height();
                }).get()
            )
        );
    };
    
})();

/* MouseWheel events interface for jQuery
 * Copyright (c) 2009 Brandon Aaron (http://brandonaaron.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 */
(function(c){var a=["DOMMouseScroll","mousewheel"];c.event.special.mousewheel={setup:function(){if(this.addEventListener){for(var d=a.length;d;){this.addEventListener(a[--d],b,false)}}else{this.onmousewheel=b}},teardown:function(){if(this.removeEventListener){for(var d=a.length;d;){this.removeEventListener(a[--d],b,false)}}else{this.onmousewheel=null}}};c.fn.extend({mousewheel:function(d){return d?this.bind("mousewheel",d):this.trigger("mousewheel")},unmousewheel:function(d){return this.unbind("mousewheel",d)}});function b(f){var d=[].slice.call(arguments,1),g=0,e=true;f=c.event.fix(f||window.event);f.type="mousewheel";if(f.wheelDelta){g=f.wheelDelta/120}if(f.detail){g=-f.detail/3}d.unshift(f,g);return c.event.handle.apply(this,d)}})(jQuery);

/* --- */



function ById(id){ return document.getElementById(id); }   

function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

function TrimString(str){
    var trimmed = str.replace(/^\s+|\s+$/g, '')
    return trimmed;            
}

condition = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[(2([0-4]\d|5[0-5])|1?\d{1,2})(\.(2([0-4]\d|5[0-5])|1?\d{1,2})){3} \])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
function isValidEmailAdd(someValue){
    if(condition.test(someValue)){
        return true;
    } else {
        return false;
    }
}

function isInvalid(startTime){
    var stTime = parseInt(startTime.substring(0,2));	    
    if(stTime < 0 || stTime > 23){
        return true;
    } else {
        return false;
    }
}

function NavigateToHere(objSelect){
    location.href = objSelect.value;
}

$(document).ready(function() {

	/* Rollovers on animals */
	$("div#duckInner").mouseover(function() {
		$("div#duckOuter .quote").css("display","block");
		document.getElementById("duckInner").title = "";
	});
	
	$("div#duckInner").mouseout(function() {
		$("div#duckOuter .quote").css("display","none");
	});
	
	$("div#crocInner").mouseover(function() {
		$("div#crocOuter .quote").css("display","block");
		document.getElementById("crocInner").title = "";
	});
	
	$("div#crocInner").mouseout(function() {
		$("div#crocOuter .quote").css("display","none");
	});
	
	$("div#hippoInner").mouseover(function() {
		$("div#hippoOuter .quote").css("display","block");
		document.getElementById("hippoInner").title = "";
	});
	
	$("div#hippoInner").mouseout(function() {
		$("div#hippoOuter .quote").css("display","none");
	});	
	
	
	/* ======== day and night time background images /start ================ */		
    if(DayTimeStart == "" || isInvalid(DayTimeStart) || DayTimeStart >= NightTimeStart || isNaN(DayTimeStart)){
        DayTimeStart = "6"; // 6am
    }
    if(NightTimeStart == "" || isInvalid(NightTimeStart) || DayTimeStart >= NightTimeStart || isNaN(NightTimeStart)){
        NightTimeStart = "18"; // 6pm
    }
    DayTimeStart = parseInt(DayTimeStart.substring(0,2),10);
    NightTimeStart = parseInt(NightTimeStart.substring(0,2),10);
    var now = new Date();
    var nowHour = now.getHours();	
    if(nowHour >= DayTimeStart && nowHour < NightTimeStart){
        $("body").css("background-color","#27BBBC");
            $("body").css("background-image","url('images/bg_bodyDay.png')");
            $("body").css("background-repeat","repeat-x");
            $("div#main h1").css("background-image","url('images/h1_dayHeader.gif')");
            //$("div#content").css("border-top","2px solid #e6f0f2");
            //$("div#contentNoBg").css("border-top","2px solid #e6f0f2");
    } else {
            $("body").css("background-color","#117071");
            $("body").css("background-image","url('images/bg_bodyNight.png')");
            $("body").css("background-repeat","repeat-x");
            $("div#main h1").css("background-image","url('images/h1_nightHeader.gif')");
            //$("div#content").css("border-top","2px solid #032e55");
            //$("div#contentNoBg").css("border-top","2px solid #032e55");
            $("div#footer").css("background-color","#117071");
    }
    /* ======== day and night time background images /end ================ */
    
    /* EqualHeights */
    /* NOTE: Footer MUST be done first in order for this to work */
    var gameContent = $('#gameContent');
    if (gameContent[0]) {
        gameContent
            .find('div.footer').equalHeights().end()
            .find('div.header').equalHeights();
    }
        
    $('.leftpanel, .rightpanel', '#center').find('.contentArea').equalHeights(true);
    
    /* Make mouse-scrolling work for flash <object>s */
    $('body').bind('mousewheel', function(e, delta){
        if (e.target.nodeName.toLowerCase() === 'object') {
            var docEl = $(document.documentElement);
            docEl.scrollTop( docEl.scrollTop() + (delta>0?-120:120) )
        }
    });
    
    /* Voting without page refresh; using iFrame much easier than XHR approach */
    $('.htVoteNow :image').each(function(){
        
        var input = $(this);
        var parents = input.parents('.htVoting, .htVideoVoting');
        var form = input.parents('form');
        
        input.click(function(e){
            
            $.get("/voting.aspx",
				{ id: parents.find('.txtVotingId').val(), type: parents.find('.txtVotingType').val(), rand: Math.random() },
			   	function(data) {
					// success assumed
				}, "json"
			);
			
            if (parents.find('.txtVotingType').val() == "tip") {
    			input.animate({opacity:0}, function(){
                    /* Fade and change msg to "thank you" msg */
                    parents.find('.txtArea').animate({
                        opacity: 0
                    }, function(){
                        $(this).html(commentData(input.parent()[0]).successMessage).animate({opacity:1});
                    })
                    
                    var updateBox = parents.find('.htVotesSoFar'),
                        span = updateBox.find('span:first').clone(),
                        innerText = updateBox.text();
                        
                    /* Increment the vote number */
                    updateBox.empty().append(span).append(
                        '' + (parseInt(innerText.match(/\d+/)[0], 10) + 1)
                    );
                    input.css({visibility:'hidden', cursor: 'default'});
                });
            } else {
                $(this).animate({opacity:0});
                $("#voteMessage").animate({opacity:0}, function(){
                    $("#voteMessage").html(commentData(input.parent()[0]).successMessage).animate({opacity:1});
                });
                
                /* update count & hide image */
                $("#htRating").html(parseInt($("#htRating").html()) + 1);
                input.css({visibility:'hidden', cursor: 'default'});
            }
			return false;
        });
        
    });
    
    
});
 
