$(function(){
    $(window).bind('load', function() {
        setContentSize();
        nav();
	hideSl();
        $(window).trigger('hashchange');
    });
});
function hideSl() {
    $('#box.public ul.sl').hide();
}
function setImgListW() {
    var imgListW = 0;
    $('#imgList').find('li').each(function(){
	imgListW = imgListW + $(this).width() + parseInt($(this).css("padding-right"));
    $('#imgList').width(imgListW);
    });
}
function setContentSize() {
    $('#imgList li:last').css({'padding-right':'0px'});
    setImgListW();
    $('#imgList').width();
    var marginSidebar = parseInt($('#sidebox').css("margin-right"));
    var minContentW = 550;
    var minContentH = 440;
    var boxMarginL = parseInt($('#box').css("margin-left"));
    var boxMarginR = parseInt($('#box').css("margin-right"));
    var contentPaddingT = parseInt($('#content').css("padding-top"));
    var contentPaddingB = parseInt($('#content').css("padding-bottom"));
    var sidebarW = $('#sidebox').width();
    var lessW=boxMarginL+boxMarginR+sidebarW+marginSidebar;
    function setW(newW) {
        if(newW < minContentW) {
            $('#box').width(minContentW+sidebarW+(2*marginSidebar)); $('#contentBox').width(minContentW);
        } else {
	    $('#box').width('auto');
	    $('#contentBox').width(newW);
        }
	if($('#contentWrapper').width() < $('#content').width()) {
	    $('#contentWrapper').css({'width':'100%'});
	}
    }
    winW = $(window).width()-30;
    setW(winW-lessW);
    function setH(newH) {
	if($('#box.public').length == 0) {
	    newH = newH - ($('#adminNav').height() + (parseInt($('#adminNav').css('padding-top'))*2) +100);
	}
        if(newH < minContentH) {
            $('#content').height(minContentH);
        } else {
	    $('#content').height(newH);
            //$('#contentBox').css({'height':'auto'});
            $('#contentBox').height($('#content').height() + contentPaddingT + contentPaddingB);
        }
    }
    winH = $(window).height();
    newH = winH-contentPaddingT-contentPaddingB-$('#header').height()-parseInt($('body').css("padding-top"))-2;
    setH(newH);
    
    $(window).resize(function(){
        winW = $(window).width();
	setW(winW-lessW);
	winH = $(window).height();
	newH = winH-contentPaddingT-contentPaddingB-$('#header').height()-parseInt($('body').css("padding-top"))-2;
	setH(newH);
    });
}


function handleSelectionOnLoad() {
    $('#ml li').removeClass('selected');
    newUrl = (window.location.hash.substr(1));
    h = newUrl.substring(newUrl.indexOf(location.host) + location.host.length + 1);
    hEscaped = escape(h);
    $('#ml').find('li a').each(function(){
        href=$(this).attr('href');
        href1 = href.substring(href.indexOf(location.host) + location.host.length + 1);
        if(h) {
            if(h.indexOf(href1) !== -1 || hEscaped.indexOf(href1) !== -1) {
		$('ul.sl a').css({'font-weight':'normal'});
		$(this).css({'font-weight':'bold'});
		$(this).parent().addClass('selected');
		$('#ml li.ml:not(.selected) ul.sl').slideUp(200,function(){});
		$('li.ml.selected ul.sl').slideDown(200,function(){ });
	    }
        } else { $('#ml li:first').addClass('selected'); }
    });
}
function setLinkImages() {
    $('#ml li.ml a.ml').each(function(){
	elem=$(this); name=elem.html();
	$.ajax({
	    url:'../theme/'+name+'.jpg', type:'HEAD', async:false,
	    success: function(){
		$('a.ml').each(function(){if($(this).html() == name) {$(this).css({'background':'url(../theme/'+name+'.jpg) 0px 3px no-repeat','text-indent':'-1000px'});}});
	    }
	});
    });
}
function nav() {
    if($('#box.public').length != 0) {
	setLinkImages();
        var newHash, minContentH=400, $content=$('#content'), $box=$('#contentBox'), $sidebox=$('#sidebox');
        $box.height($box.height());
        baseH=$box.height() - $content.height();
        function setDelgate(){
            arr = ['.public #ml'];
            for(i=0;i<arr.length;i++) {
                $(arr[i]).delegate('a', 'click', function(){
                    window.location.hash = $(this).attr('href');
                    return false;
                });
            }
        }
        setDelgate();
        $(window).hashchange( function(){
            newUrl = escape(window.location.hash.substr(1));
            newHash = newUrl.substring(newUrl.indexOf(location.host) + location.host.length + 1);
	    var p= location.pathname.split("/").length - 1;
	    if(p > 1) { newHash=newHash.substr(newHash.indexOf('/')+1); }
            if(newHash) {
                handleSelectionOnLoad();
                setDelgate();
                $content.find('#contentWrapper').stop().fadeOut(200,function(){
                    $content.hide().load(newHash + " #contentWrapper",function(){
                        $('#contentWrapper').show();
			$content.fadeIn(200,function(){
			    setImgListW();
			    $('#imgList li:last').css({'padding-right':'0px'});
                        });
                    });
                });
            }
        });
    }
}
function validateForm() {
    valid = true; selArr = [];
    $('.editIndex select').each(function(i, selected){ 
        if($.inArray($(selected).val(), selArr) > -1) {
            mark($(selected), false); valid = false;
        } else { mark($(selected), true); }
        selArr[i] = $(selected).val();
    });
    $('.req').each(function(){
        if($(this).val() == ''){ mark($(this), false); valid = false;
        } else { mark($(this), true); }
    });
    function mark(obj, pos) {
        if(pos) { obj.css('border','1px solid #D7E0C9');
        } else { obj.css('border','1px solid red'); }
    }
    return valid;
}
