var noflash_div, flash_div, footer, footer_pad, bgr_div, content_div, regionCookie, f_hdr, develop;
var IE = !window.getComputedStyle;
var IE6 = IE && navigator.appVersion.indexOf('MSIE 7') == -1;

var regionCookie=new jsCookie();
regionCookie.path="/";
regionCookie.domain="";
regionCookie.secure=false;
regionCookie.name="region";
regionCookie.expires="0,0,0,1"; // Minutes, Hours, Days, Years

function __load(){
	noflash_div = $('noflash');
	flash_div = $('flash');
	footer = $('footer');
	bgr_div = $('second-bgr');
	content_div = $('content-block');
	develop = $('develop');
	window.onresize = __resize;
	__resize();

	init_content();
	init_regions();
	init_text_inputs();
	if ( IE6 ) {
		png_fix();
	}
}
function __resize(){
										//not ie						//ie
	var w = Math.max(900, window.innerWidth || document.body.clientWidth);
	var h =	window.innerHeight || document.body.clientHeight;


	resize_noflash(w, h);
	width_flash(w, h);
	resize_bgr(w, h);
	place_content(w, h);

	var right_margin = Math.round((w - 917) / 2);
	develop.style.right = -right_margin  + 'px';

}
function width_flash(w, h){

	if ( flash_div && flash_div.offsetWidth < 900 ) {
		flash_div.style.width =  '900px';
	}
	if(flash_div){
		var val = 780;
		var val2 = 700;
		var top = 0;
		if(h < val && h > val2){
			top = h - val;
		}else if(h < val && h <= val2){
			top = -80;
		}
		flash_div.style.top = top + 'px';
	}
	
}
function resize_noflash(w, h){
	if( !document.getElementById('noflash') ){
		return false;
	}
	var noflash_top = h - noflash_div.offsetHeight - 126;
	if(h < 766){
		noflash_top = -20;
	}
	noflash_div.style.top =  noflash_top + 'px';

	var fleft = noflash_div.offsetLeft;
	var right_margin = Math.round((w - 900) / 2);


	noflash_div.style.width = 900 - fleft + right_margin - 2 + 'px';
}
function resize_bgr(w, h){
	if(!bgr_div){
		return false;
	}
	var right_margin = Math.round((w - 900) / 2);
	var left =  bgr_div.offsetLeft;
	bgr_div.style.width = 900 - left + right_margin + 'px';
	var top = h - bgr_div.offsetHeight - 132;
	if(top > 0){
		top = 0;
	}
	if(h < 766){
		top = -100;
	}
	bgr_div.style.top = top + 'px';

}
function init_content(){
	if(!content_div){
		return false;
	}
	var root = content_div;
	var s_holder = $('scroll-holder');
	var s_content = $('scroll-content');

	var s_bar = $('scrollbar');
	var s_divs = $T('DIV', s_bar);
	var track = $C('track', s_divs, '', 1);
	var thumb = $C('thumb', s_divs, '', 1);
	var b_plus = $C('b-plus', s_divs, '', 1);
	var b_minus = $C('b-minus', s_divs, '', 1);

	var tim = new Timer(50);
	tim.start();

	var scroll = new Scroll(s_holder, s_content, {X: null, Y: {
		bar: s_bar,
		track: track,
		thumb: thumb,
		bPlus: b_plus, 
		bMinus: b_minus
	}}, tim);
	if(scroll.barY.th_mov){
		scroll.barY.th_mov.onaftermove.register('set angle', function(){
			this.elem.style.left = Math.round(this.left - (this.top / 4.5)) + 'px';
		});
	}
}
function init_regions(){
	
	var timer = null;
	
	var root = $('regions');
	if(!root){
		return false;
	}
	var divs = $T('DIV', root);
	root.button = $C('button', divs, '', 1);
	root.dropdown = $C('dropdown', divs, '', 1);
	root.display = $C('display', divs, '', 1);
	root.button.onclick = root.display.onclick = function(){
		this.dropdown.style.visibility = 'visible';
	}.bind(root);

	root.dropdown.onmouseout = function (e) {
		timer = setTimeout(function () {root.dropdown.style.visibility = 'hidden'}, 300 );
	}
	
	root.dropdown.onmouseover = function (e) {
		clearTimeout(timer);
		root.dropdown.style.visibility = 'visible';
	}
	
	root.items = $C('item', divs, 'DIV');

	if ( regionCookie.exists( ) ) {

		regionCookie.get( );
		regId = regionCookie.value;

		root.items.each(function(item){
			item.model = root;
			item.link = $T('A', item, 1);
			item._id = $T('INPUT', item, 1).value;
			item._text = item.link.firstChild.nodeValue;
			if ( item._id == regId ) {
				root.display.innerHTML = item._text;
			}
		});

	}

	root.items.each(function(item){
		item.model = root;
		item.link = $T('A', item, 1);
		item._id = $T('INPUT', item, 1).value;
		item._text = item.link.firstChild.nodeValue;
		item.link.onclick = function(){
			this.model.dropdown.style.visibility = 'hidden';
			this.model.display.innerHTML = this._text;
			setRegion(this._id);
			return false;
		}.bind(item);
		
		item.link.onmouseover = function () {
			clearTimeout(timer);
		}
		
		
	});
}
function place_content(w, h){
	if(!content_div){
		return false;
	}
	return false;
	var top;
	if(h < 766){
		top = 115;
	}else{
		top = ((h - 766) / 1.7) + 115;
	}
	content_div.style.top = top + 'px';
}
function init_text_inputs(){
	var ips = $C('text', document, 'INPUT');
	if ( ips ) {
		ips.each(function(ip){
			var obj = text_inputs[ip.name];
			if(obj){
				ip._value = obj.value;
				ip.onblur = function(){
					if(this.value == ''){
						this.value = this._value;
					}
				};
				ip.onfocus = function(){
					if(this.value == this._value){
						this.value = '';
					}
				};
			}
		});
	}
}
var text_inputs = {
	astroauth_login: {
		value: 'Login'
	},
	astroauth_pass: {
		value: 'Password'
	}
}
AttachEvent(window, 'load', __load);

function setRegion( id ){

	if ( regionCookie.exists( ) ) {

		regionCookie.get( );
		if ( regionCookie.value != id ) {
			regionCookie.value = id;
		}

	}
	else {
		regionCookie.value = "1";
	}

	regionCookie.set( );	

}
function png_fix(){
	var imgs = $T('IMG');
	imgs.each(function(img){
		var name = img.src;
		if(name.match('\.png$')){
			var src = img.src;
			img.style.width = img.width + 'px';
			img.style.height = img.height + 'px';
			img.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + src + '", sizingMethod="crop")';
			img.src = _root+'i/photo/blank.gif';		
		}
	})
}
