$(document).ready(function(){
    $(document).pngFix(); 

    for(var name in jQuery.browser) {
    	if(name == "version") {
    		continue;
    	}
    	var version = jQuery.browser.version.split('.');
    	$(document.body).addClass(name).addClass(name + version[0]).addClass(name + version[0] + "." + version[1]);
    }
    
    var directAccessHidden = true;
    $('.direct-access').bind('click', function() {
    	if(directAccessHidden) {
    		$('.direct-access-holder').show('fast');
    	} else {
    		$('.direct-access-holder').hide('fast');
    	}
    	directAccessHidden = !directAccessHidden;
    });

    $('.hide').hide();
	
    window.onresize=timerResize;
    setminiwidth();
});

var timeOut;

var timerResize = function() {
	if(timeOut) {
		return;
	}
	timeOut = setTimeout(function() {
		setminiwidth();
		timeOut = null;
	}, 50);
}

function setminiwidth() {
	var height = $(window).height();
    var windowWidth = $(window).width();
    
    //$('#header').css('left', Math.max(0,(((windowWidth - $('#header').width())) / 2)) + "px");
    var submenuLeft = Math.max(0,(((windowWidth - $('#submenu').width())) / 2));
    //$('#submenu').css('left', Math.max(0,(((windowWidth - $('#submenu').width())) / 2)) + "px");
    //$('#footer').width(windowWidth - 55);
    //$('#footer').css('left', Math.max(28,(((windowWidth - $('#footer').width())) / 2)));
    
    $('#footer').width(windowWidth - 55);
	
	var wrapperW = $('#footer').length > 0 ? $('#footer').width() : windowWidth - 55;
	var wrapperH = wrapperW*.5;
	
    if(height - $('#header').outerHeight(true) < wrapperH) {
	    wrapperH = Math.max(500, height - $('#footer').outerHeight(true) - $('#header').outerHeight(true));
        wrapperW = wrapperH/.5 + $('#footer').outerHeight(true);
	}
	
	$('#submenu').width(wrapperW).css('left', Math.max(0,(((windowWidth - $('#submenu').width())) / 2)) + "px");
	$('#header, #footer').width(wrapperW).css('left', Math.max(28,(((windowWidth - $('#footer').width())) / 2)));
	$('.msie8 #footer').css('left', '-=2');
	$('#flash-wrap').height(wrapperH).width(wrapperW).css('left', Math.max(0,(((windowWidth - $('#flash-wrap').width())) / 2)) + "px");
	$('#flashcontent').height(wrapperH).width(wrapperW);

	if($('.homeSection ').length <= 0) {
		//var bottom = Math.max(0, height - $('#header').height() - $('#footer').height() - $('#footer').width() / 2);
		var footerTop = $('#header').height() + $('#flash-wrap').height();
		$('#footer').css('top', footerTop);
	}
	
    var menus = ['radiance', 'intimate', 'house'];
    for(var id in menus) {
		
        $('#' + menus[id]+ ' li').show();

        var offset = $('.' + menus[id] + 'Header').offset();
        if(offset) {
            $('.' + menus[id]+ 'Menu').css('left', (offset.left - submenuLeft) + 'px');

			var totalWidth = 0;
			$('#' + menus[id] + ' li').each(function() {
				totalWidth += $(this).outerWidth(true);
			});

			if(offset.left + totalWidth > $(window).width() - 30) {
				$('.' + menus[id]+ 'Menu').css('left', $(window).width() - totalWidth - 50);
			}
        }
    }
    
	$('#copy').triggerHandler('mainResize');
}

setminiwidth();

function smenu(pageId){
	var splitted = pageId.split('/');
	pageId = splitted[2];
	if(splitted[3]) {
		pageId += "/" + splitted[3];
	}
	if(! routes[pageId]) {
		return;
	}
	var link = routes[pageId];
	if(splitted[4]) {
		link += "?id=" + splitted[4];
	}
	
	window.location=link;
}
