var resize_set = false

var fill = function(selector, n){
    $(selector).html('')
    if(n==undefined) n=2
    for(i=0;i<n;i++)
        $(selector).append( $('#lorem').html() )
}

var fit_blocks = function(force_height){
    var debug = false
    //var debug = true

    //$('.right #right-content').css('height','auto')
    //$('.left #left-content').css('height','auto')
	
	var junkLeft =  $('.left').height() - $('#left-content').height();
	var junkRight = $('.right').height() - $('#right-content').height();
    var difference = junkLeft - junkRight;
	var leftParent = $($('.left')[0]).height();
    var left = $('#left-content').height();
    var right = $('#right-content').height();
    var min = left<right ? left : right
    var max = left>right ? left : right
    if(debug) console.log('left: '+left)
    if(debug) console.log('right: '+right)
    if(debug) console.log('difference: '+difference)

    var final_h = left
    if(difference<0){
        final_h = left + difference
        if(left+difference<0) final_h = right + ($('body').hasClass('right-fix') ? 30 : 0)
        //if(right-difference > left) final_h = right
        if(final_h<150) final_h = 150
    }else{
        final_h = left + difference
    }
    if( leftParent > 2070 ) final_h = 2070 - junkLeft + difference;
    if(force_height){
        $($('#left-content')[0]).css('height',force_height + 11 +  'px')
        $($('#right-content')[0]).css('height', force_height + 'px')
        return
    }

    if(debug) console.log('final_h: '+final_h)

    var difference_left = -difference
    var difference_right = 0
    if($('body').hasClass('height-fix')){
        difference_left = 0
        difference_right = difference
    }
    if($('body').hasClass('height-fix-photo')){
        //difference_left = 70
        //difference_right = 70 + difference
        difference_left = 11
        difference_right = 0
        final_h = 800
    }
	new_left_height = 0 + final_h + difference_left + 0;
	new_right_height = 0 + final_h + difference_right + 0;
    $($('#left-content')[0]).css('height', new_left_height + 'px')
    $($('#right-content')[0]).css('height', new_right_height + 'px')
}

var fix_lengths = []
var current_length_index = 0

var init = function(left_p, right_p){
	if($('.photoman').length > 0 && location.hash!='' && /photo\d+/.test(location.hash)){
		$('.photoman:not('+location.hash+')').hide()
	}
    if(left_p != undefined)
        fill('#left-content', left_p)
    if(right_p != undefined)
        fill('#right-content', right_p)

    if($('#left-content').jScrollPaneRemove != undefined){
       //alert('success');
       $('#left-content').jScrollPaneRemove() }
    if($('#right-content').jScrollPaneRemove != undefined)
       $('#right-content').jScrollPaneRemove()

    $('#left-content').attr('style', '')
    $('#right-content').attr('style', '')
    fit_blocks()

    $('#left-content').jScrollPane({showArrows:true, scrollbarWidth: 17, wheelSpeed:50});
    $('#right-content').jScrollPane({showArrows:true, scrollbarWidth: 17, wheelSpeed:50});

    $('a').focus(function(){ this.blur() })
    $('img').focus(function(){ this.blur() })
    $('div').focus(function(){ this.blur() })

    resize_set = true

    /*
    if($('.right-content-container').length!=0){

        var i=0
        $('.right-content-container').each(function(){
            fix_lengths[i] = $(this).height()
            i++
        })

        console.dir(fix_lengths)

        $('.right-content-container').each(function(){
            $(this).parent().show()
        })
    }*/
    /*$('input').unbind('focus')
    $('textarea').unbind('focus')*/
}

var re_init = function(height){
    if(!resize_set) return
    //if($.browser.msie){ return; }
    $('#left-content').jScrollPaneRemove()
    $('#right-content').jScrollPaneRemove()

    $('#left-content').removeAttr('style')
    $('#right-content').removeAttr('style')

    $('.right-content-container').each(function(){
        if($(this).parent().css('display')=='none'){
            $(this).removeAttr('id')
        }else{
            $(this).attr('id', 'right-content')
        }
    })
    //alert($('#right-content').height())

    if(height!=undefined){
        fit_blocks(height)
        return
    }
    
	fit_blocks();	
	
	$('#left-content').jScrollPane({showArrows:true, scrollbarWidth: 17, wheelSpeed:50});
    $('#right-content').jScrollPane({showArrows:true, scrollbarWidth: 17, wheelSpeed:50});

}

/*
$(document).ready(function(){
    init()
})
*/

$(document).ready(function(){
	$('#text').jScrollPane({showArrows:true, scrollbarWidth: 17});

	$('.bigmenu').click(function(){
	    var t = this
	    var i = 1
	    var index = 0
	    $('.bigmenu').each(function(){
	        if(t==this) index=i
	        i++
	    })
	    var i = 1
	    $('.bigmenu').each(function(){
	        if(i!=index){
	            $(this).parent().find('ul').hide()
	            $(this).removeClass('b'+i+'a')
	        }
	        i++
	    })
	    var menu_hidden = $(this).parent().find('ul').css('display')!='none'
	    if($(this).hasClass('b'+index+'a') && menu_hidden){
            $(this).parent().find('ul').hide()
            if(!menu_hidden || index_page)
                $(this).removeClass('b'+index+'a')
        }else{
            $(this).parent().find('ul').show()
            $(this).addClass('b'+index+'a')
        }
	});
	/*
	jQuery("div").focus(
			function(){
			this.blur();

			});
	jQuery("img").focus(
			function(){
			this.blur();

			});
	jQuery("li").focus(
			function(){
			this.blur();

			});
	jQuery("a").focus(
					function(){
					this.blur();
	});
	*/

	$('#text').jScrollPane({showArrows:true, scrollbarWidth: 17});
});

var collapse = function(el, n){
    if(n == undefined)
        var n = parseInt( el.id.replace('more-', '') )

    $('.block-n').hide()
    $('.more-n').show()
    $('.map-n').hide()

    //$(el).hide()
    $('#block-'+n).show()
    $('#map-'+n).show()
    init()
}


