// globals
var CLICKEVENTADDED = false;
var IMAGESTIMEOUT;
var MOVINGIMAGES = false;


var DEBUG = false;
var DEVICE, DOUT, KEYS, putText;

// determine system type
/*if ( typeof(Common) !== 'undefined' ) {
    DEVICE = 'samsung';
    DOUT = function(msg) {
        if ( DEBUG ) {
            alert(msg);
        }
    };
} else {*/
    DEVICE = 'workstation';
        
    DOUT = function(msg) {
        if ( DEBUG ) {
            if ( typeof(console) != "undefined" ) {
                //console.log(msg);
            }
        }
    };
//}

window.onload = function() {

    if ( DEVICE == 'workstation' ) {
        putText = function(id, text) {
            $(id).innerHTML = text;
        };
        KEYS = {
            LEFT: 37,
            RIGHT: 39,
            UP: 38,
            DOWN: 40,
            OK: 13,
            BACK: 90,
			BACK: 8,
            /*TOOLS: 77,
            FF: 102,
            RW: 100,*/
            PLAY: 80,
			PAUSE: 19,
            /*RED: 82,
            YELLOW: 89,
            BLUE: 66,*/
			ESC: 27,
			GOOGLEPLAY: 179,
			GOOGLESTOP: 178,
			GOOGLENEXT: 176,
			GOOGLEPREV: 177,
			GOOGLEPAUSE: 179
        };
    }
	
	//fix zoom for page to fit screen
	//var w = screen.width;
	//var h = screen.height;
		
	/*var w = 1920*0.99;
	var h = 1080*0.99;
	
	var bw = window.innerWidth;
	var bh = window.innerHeight;

	var wRatio = bw/w;
	var hRatio = bh/h;
	var ratio = (wRatio + hRatio) / 2;

	if(bw != 1920 && bh != 1080){
		//document.getElementsByTagName('body')[0].style.zoom = ratio;
	}
	//alert("w: " + w + "\nh: " + h + "\nbw: " + bw + "\nbh: " + bh + "\nratio: " + ratio);

*/
    AMOSTV.init();
    //document.addEventListener('keydown', AMOSTV.keyHandler, false);
    document.onkeydown = AMOSTV.keyHandler;

    // var viewsRef = $('views');

    // if (typeof(viewsRef.addEventListener) != "undefined") {
    //     //viewsRef.addEventListener('mouseup', MouseHandling.onMouseClick, false);
    //     viewsRef.addEventListener('mouseover', MouseHandling.onMouseMove, false);

    // } else if (typeof(viewsRef.attachEvent) != "undefined" ) {
    //     //viewsRef.attachEvent('onmouseup', MouseHandling.onMouseClick, false);
    //     viewsRef.attachEvent('onmouseover', MouseHandling.onMouseMove, false);
    // }

    $('views').observe('click', MouseHandling.onMouseClick).observe('mouseover', MouseHandling.onMouseMove);
	
	
	var de = document.documentElement;
	var b = document.body;
	var cl = 0;
	var ct = 0;
	
	if(de)
	{
		cl = parseInt(de.clientWidth,10);
		ct = parseInt(de.clientHeight,10);
	}
	else
	{
		cl = parseInt(b.clientWidth,10);
		ct = parseInt(b.clientHeight,10);
	}
	
	
	var SW = cl;
	var SH = ct;
	
	if(SW < 1280)
	{
		SW = 1280;
	}
	if(SH < 890)
	{
		SH = 890;
	}
	var m = parseInt((SW-jQuery("#container").width())/2,10);
	jQuery("#container").css("margin-left",m);
	jQuery("#container").css("margin-right",m);
	jQuery(".fademe").each(function()
	{
		jQuery(this).width(SW);
		jQuery(this).height(SH);
	});	
	
	
	
	
	jQuery(".findus .itemf img").hover(
	function()
	{
		jQuery(this).animate({left:'-=10',bottom:'-=10',width:'+=20',zIndex:3},400);
	}
	,
	function()
	{
		jQuery(this).animate({left:'+=10',bottom:'+=10',width:'-=20',zIndex:"inherit"},400);
	}
	);


	jQuery("#findUsLinks  img").hover(
	function()
	{
		var src = this.src;
		this.src = src.replace(".png","_roll.png");
	}
	,
	function()
	{
		var src = this.src;
		this.src = src.replace("_roll.png",".png");
	}
	);

};

window.onshow = function() {
};

window.onunload = function() {
};


var images = new Array("img/bgs/Desert.jpg","img/bgs/Lake.jpg","img/bgs/River.jpg","img/bgs/River-2.jpg","img/bgs/Trees.jpg");
var images2 = new Array();
function preloadBackgrounds()
{
	for(var i = 0 ; i < images.length ; i++)
	{
		images2[i] = new Image();
		images2[i].onload = function()
		{
			this.lang = "done";
		}
		images2[i].src = images[i];
	}
}
function movefade(name,pos,time,everytime)
{
	MOVINGIMAGES = true;
	var curpos = parseInt(jQuery(name).attr("lang"),10);
	jQuery(name).attr("lang",pos); 
	var imgs = jQuery(name+" .fademe");
	if(pos == imgs.length)
	{
		pos = 0;
	}
	else
	{
		pos++;
	}
	jQuery(imgs[curpos]).fadeTo(time,0);
	jQuery(imgs[pos]).fadeTo(time,1);
	IMAGESTIMEOUT = setTimeout(
	function(){
	movefade(name,pos,time,everytime);
	},everytime);
}
function moveImages()
{
	if(MOVINGIMAGES == false)
	{
		movefade("#fadebody",0,1500,5000);
	}
}
function pauseImages()
{
	if(MOVINGIMAGES == true)
	{
		MOVINGIMAGES = false;	
		clearTimeout(IMAGESTIMEOUT);
	}
	else
	{
		var imgs = jQuery("#fadebody .fademe");
		var randomnumber=Math.floor(Math.random()*imgs.length)-1;/*0 to length-1*/		
		movefade("#fadebody",randomnumber,1500,5000);
		MOVINGIMAGES = false;
		clearTimeout(IMAGESTIMEOUT);
	}
}
preloadBackgrounds();
