jQuery(document).ready(function () {
    var time = 300;
    var collapse = $(".collapse");
    if (collapse) {
	    collapse.collapse({
		    show: function (op) {
			    op = $.extend({ duration: time }, op);
			    this.animate({
				    opacity: 'toggle',
				    height: 'toggle'
			    }, op);
		    },
		    hide : function (op) {
			    op = $.extend({ duration: time }, op);
			    this.animate({
				    opacity: 'toggle',
				    height: 'toggle'
			    }, op);
		    }
	    });

	    collapse.not('.active').addClass('inactive');
    }

	$("a").each(function () {
		if (this.getAttribute("href") && this.getAttribute("rel") === "external") {
			this.target = "_blank";
		}
	});

    initmb();
});

function site_show_collapsed() {
	var arrow = $('#expand_toggle'),
        url = arrow.css('background-image'),
	    action,
        arrowDuration = 300,
        arrowOut,
        arrowIn,
        attText = $('#attorney_text');

    aHeight = arrow.css('height');

	if (url.match(/-less/)) {
		url = url.replace('-less', '-more');
		action = 'hide';
        arrowOut = { bottom: aHeight};
        arrowIn  = { bottom: 0};
        if (attText) {
            attText.animate({ marginBottom: '36px'});
        }
	} else {
		url = url.replace('-more', '-less');
		action = 'show';
        arrowOut = arrowIn = { height: 'toggle' };

        if (attText) {
            attText.animate({ marginBottom: '72px'});
        }
	}

    $('#expand_toggle').animate(
        arrowOut,
        { duration: arrowDuration,
          complete: function() { $('#expand_toggle').css('background-image', url) }
        }
    ).animate(
        arrowIn,
        { duration: arrowDuration,
          queue: true }
    )

	$(".collapse").trigger(action);

	return false;
}

function swap_client_info(nextlink) {
	nextlink = $(nextlink);
	var getLink = function (t, type) {
		var id = t.attr('id');
		return id.replace(/^client_[a-zA-Z0-9]+_/, 'client_' + type + '_');
	},
	prev = $('.collapse').filter('.active'),
	prevlink = $('#' + getLink(prev, 'name')),
	next = $('#' + getLink(nextlink, 'info'));

	if (next.get(0) === prev.get(0)) {
		return false;
	}

	prevlink.removeClass('active');
	prev.trigger('hide', {
		complete: function () {
			nextlink.addClass('active');
			next.trigger('show');
		}
	}
	            );

	return false;
}

function site_toggle_overflow(id) {
	id = $('#' + id);
	var o = id.css('overflow'),
	height,
	margin_bottom;

	if (o === "hidden") {
		o = 'visible';
		$('#attorney_more').hide();
		id.data('_height_save', id.css('height'));
		height = '';
		margin_bottom = '-=15';
	} else {
		o = 'hidden';
		$('#attorney_more').show();
		height = id.data('_height_save');
		margin_bottom = '+=15';
	}
	id.css('overflow', o);
	id.css('height', height);
	// id.css('margin-bottom', margin_bottom);

	return false;
}

function str_rot13(str) {
    // Perform the rot13 transform on a string
    //
    // version: 1009.2513
    // discuss at: http://phpjs.org/functions/str_rot13    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Ates Goral (http://magnetiq.com)
    // +   bugfixed by: Onno Marsman
    // +   improved by: Rafal Kukawski (http://blog.kukawski.pl)
    // *     example 1: str_rot13('Kevin van Zonneveld');    // *     returns 1: 'Xriva ina Mbaariryq'
    // *     example 2: str_rot13('Xriva ina Mbaariryq');
    // *     returns 2: 'Kevin van Zonneveld'
    // *     example 3: str_rot13(33);
    // *     returns 3: '33'
    return (str + '').replace(/[a-z]/gi, function (s) {
        return String.fromCharCode(s.charCodeAt(0) + (s.toLowerCase() < 'n' ? 13 : -13));
    });
}

// Modal Dialog Box
// copyright 8th July 2006 by Stephen Chapman
// http://javascript.about.com/
// permission to use this Javascript on your web page is granted
// provided that all of the code in this script (including these
// comments) is used without any alteration
function pageWidth() {
    return window.innerWidth != null? window.innerWidth: document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth:document.body != null? document.body.clientWidth:null;
}
function pageHeight() {
    return window.innerHeight != null? window.innerHeight: document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight:document.body != null? document.body.clientHeight:null;
}
function posLeft() {
    return typeof window.pageXOffset != 'undefined' ? window.pageXOffset:document.documentElement && document.documentElement.scrollLeft? document.documentElement.scrollLeft:document.body.scrollLeft? document.body.scrollLeft:0;
}
function posTop() {
    return typeof window.pageYOffset != 'undefined' ? window.pageYOffset:document.documentElement && document.documentElement.scrollTop? document.documentElement.scrollTop: document.body.scrollTop?document.body.scrollTop:0;
}
function $$(x){
    return document.getElementById(x);
}
function scrollFix(){
    var obol=$$('ol');
    obol.style.top=posTop()+'px';
    obol.style.left=posLeft()+'px'}
function sizeFix(){
    var obol=$$('ol');
    obol.style.height=pageHeight()+'px';
    obol.style.width=pageWidth()+'px';
}
function kp(e){
    ky=e?e.which:event.keyCode;
    if(ky==88||ky==120)hm();
    return false;
}
function inf(h){
    tag=document.getElementsByTagName('select');
    for(i=tag.length-1;
        i>=0;
        i--)tag[i].style.visibility=h;
    tag=document.getElementsByTagName('iframe');
    for(i=tag.length-1;
        i>=0;
        i--)tag[i].style.visibility=h;
    tag=document.getElementsByTagName('object');
    for(i=tag.length-1;
        i>=0;
        i--)tag[i].style.visibility=h;
}
function sm(obl, wd, ht){
    var h='hidden';
    var b='block';
    var p='px';
    var obol=$$('ol');
    var obbxd = $$('mbd');
    obbxd.innerHTML = $$(obl).innerHTML;
    obol.style.height=pageHeight()+p;
    obol.style.width=pageWidth()+p;
    obol.style.top=posTop()+p;
    obol.style.left=posLeft()+p;
    //obol.style.display=b;
    var tp=posTop()+((pageHeight()-ht)/2)-12;
    var lt=posLeft()+((pageWidth()-wd)/2)-12;
    var obbx=$$('mbox');
    obbx.style.top=(tp<0?0:tp)+p;
    obbx.style.left=(lt<0?0:lt)+p;
    obbx.style.width=wd+p;
    /*obbx.style.height=ht+p; */
    inf(h);
    //obbx.style.display=b;


    var fade = 600;
    var opacity = 0.5;
    var ol = $(obol);
    ol.css({ opacity: 0 });
	ol.show();
	ol.fadeTo(fade, opacity);

    $(obbx).fadeIn(900);
    return false;
}
function hm(follow){
    var v='visible';
    var n='none';

    $('#ol').fadeOut(900);
    $('#mbox').fadeOut(600);
    inf(v);
    document.onkeypress=''

    if (follow) {
        var email = $$('dialog_email');
        location.href = email.innerHTML;
    }
    return follow;
}
function initmb(){
    var ab='absolute';
    var n='none';
    var obody=document.getElementsByTagName('body')[0];
    var frag=document.createDocumentFragment();
    var obol=document.createElement('div');
    obol.setAttribute('id','ol');
    obol.style.display=n;
    obol.style.position=ab;
    obol.style.top=0;
    obol.style.left=0;
    obol.style.zIndex=998;
    obol.style.width='100%';
    frag.appendChild(obol);
    var obbx=document.createElement('div');
    obbx.setAttribute('id','mbox');
    obbx.style.display=n;
    obbx.style.position=ab;
    obbx.style.zIndex=999;
    var obl=document.createElement('span');
    obbx.appendChild(obl);
    var obbxd=document.createElement('div');
    obbxd.setAttribute('id','mbd');
    obl.appendChild(obbxd);
    frag.insertBefore(obbx,obol.nextSibling);
    obody.insertBefore(frag,obody.firstChild);

    window.onscroll = scrollFix;
    window.onresize = sizeFix;
}

function email_link(linktext) {
    var box = document.getElementById('box');
    $$('dialog_email').innerHTML = linktext;
    return sm('box',640,140);
}

