/*
Name: jQuery Horizontal Accordion Plugin
Version: 1.0.0
Last Updated: 11/18/2010
Author: Slobodan Jovanovic
Copyright(c): 2010 
License: Dual licensed under the MIT and GPL licenses:
http://www.opensource.org/licenses/mit-license.php
http://www.gnu.org/licenses/gpl.html
Options:
	{
		'width':'400px', // optional - force width
		'height':'300px', // optional - force height
		'handleWidth':'30px' // optional - width of the accordion handle/button
	}
*/
(function($){
 $.fn.hackordion = function(opt) {
    return this.each(function() {
    	var jqelem = $(this);
		jqelem.css({
			'width':opt&&opt.width?opt.width:'',
			'height':opt&&opt.height?opt.height:'',
			'position':'relative'
		});
		jqelem.children().each(function(index){
			if (index % 2) {
				$(this).addClass('hackordionContent');
			}else{
				$(this).addClass('hackordionButton');
			}
		});
		var nc = (jqelem.children().length/1) / 2;
		var hwidth = opt&&opt.handleWidth?opt.handleWidth:48;
		var pdiff = getDiff(jqelem.children(':eq(0)'));
		hwidth = hwidth - pdiff;
		var pwidth = jqelem.width() - (nc * (hwidth + pdiff)) - getDiff(jqelem.children(':eq(1)'));
		jqelem.children().each(function(index){
			var hdiff = getDiff($(this), true);
			if(index%2){
				$(this).css({
					'width':(pwidth)+'px',
					'height':(jqelem.height() - hdiff)+'px',
					'float':'left',
					'display':index==1?'block':'none'
				});
			}else{
				$(this).css({
					'width':(hwidth)+'px',
					'height':(jqelem.height() - hdiff)+'px',
					'position':'relative',
					'zIndex':'100',
					'float':'left'
				});
				if($.browser.msie){
					
				} else if ($.browser.safari) {
					
				}else{
					var sfnt = {'Arial':true,'Tahoma':true,'Verdana':true,'Impact':true,'Georgia':true,'Times':true,'Serif':true};
					var fnt = $.trim($(this).css('fontFamily').split(',')[0]);
					if(!sfnt[fnt]){fnt = 'Arial';}
					$(this).html("<object style=\"z-index:-1;width:"+$(this).width()+"px;height:"+$(this).height()+"px;\" type=\"image/svg+xml\"\
				    data=\"data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' style='background-color:"+$(this).css('background-color')+";'>\
				    <text x='-"+($(this).height()-18)+"' y='30' font-weight='"+$(this).css('fontWeight')+"' font-family='Trebuchet MS,Arial,Helvetica,sans-serif' font-size='"+$(this).css('fontSize')+"' transform='rotate(-90)' text-rendering='optimizeSpeed' fill='"+$(this).css('color')+"'>"+$(this).text()+"</text>\
				    </svg>\">\
					<div class=\"old_canv\">"+$(this).text()+"</div>\
				    </object><div style='width:"+$(this).width()+"px;height:"+$(this).height()+"px;position:absolute;top:0px;left:0px;'></div>");	
				}
				$(this).click(function(){
					var clicked = $(this);
					if(clicked.next().css('display') != 'none'){return false}
					$(this).parent().children().each(function(){
						var t = $(this);
						if(t.hasClass('hackordionContent') && t.css('display') != 'none'){
							t.prev().removeClass('hackordionSelected');
							t.animate({'width':(pwidth - 100)+'px'}, 25, function(){
								t.animate({'width':'0px'}, 25, function(){
									$(this).css({'display':'none','width':0+'px'});
								});
								clicked.next().css({'width':'0px', 'display':'block'}).animate({'width':(pwidth)+'px'}, 50);
								clicked.addClass('hackordionSelected');
							});
							return false;
						}
					});
				});
				
			}
		});
		jqelem.append('<div style="clear:both;height:0px line-height:0px; fon-size:1px;"/>');
		function getDiff(el, horizontal){
			if(horizontal){
				var itms = ['borderTopWidth','borderBottomWidth','marginTop','marginBottom','paddingTop','paddingBottom'];
			}else{
				var itms = ['borderLeftWidth','borderRightWidth','marginLeft','marginRight','paddingLeft','paddingRight'];
			}
			var diff = 0;
			for(var i in itms){
				if((el.css(itms[i]).replace('px', '')/1) > 0){
					diff += (el.css(itms[i]).replace('px', '')/1);
				}
			}
			return diff;
		};
		
    });
 };
})(jQuery);
