/**
 * @author apipkin
 */
try {
    document.execCommand( "BackgroundImageCache", false, true );
} catch( e ) { };


function getQueryParameters(qs) {
    qs = qs.split("+").join(" ");
    if(qs.charAt(0) == '?')
    {
        qs = qs.substr(1);
    }
    qs = qs.replace(/&amp;/,"&");
    var params = {};
    var tokens = qs.split('&');

    for(var i = 0; i < tokens.length; i++)
    {
        var bits = tokens[i].split('=');
        params[decodeURIComponent(bits[0])] = decodeURIComponent(bits[1]);
    }

    return params;
}

$().ready(function(){
	
	var count = 0;
	var heightObj = {};
	
	$('.srToggle').click(function(){
		// target sibling
		var sib = $(this).next();
		
		// change class and play animation
		if(sib.css('display') == 'none' || sib.hasClass('srClosed')) {
			sib.animate(
					{
						height:heightObj[$(this).attr('name')]
					},{
						duration: 'normal',
						easing: 'easeOutQuad',
						step: function() {
							$(window).resize();
						},
						complete: function(){
							$(window).resize();
						}
					}
				)
			   .removeClass('srClosed')
			   .addClass('srOpened');
			   
			$(this).removeClass('closed')
			       .addClass('opened');
		}else if(sib.css('display') == 'block' || sib.hasClass('srOpened')) {
			sib.animate(
					{
						height:0
					},{
						duration: 'normal',
						easing: 'easeOutQuad',
						step: function() {
							$(window).resize();
						},
						complete: function(){
							sib.hide();
							$(window).resize();
						}
					}
				)
			   .removeClass('srOpened')
			   .addClass('srClosed');
			   
			$(this).removeClass('opened')
			       .addClass('closed');
		}
		
		$(window).resize();
	}).each(function(){
		$(this).attr('name','srCount' + (count++));
		if(heightObj[$(this).attr('name')] == 'undefined' || heightObj[$(this).attr('name')] == null){
			heightObj[$(this).attr('name')] = $(this).next().height();
		}

		$(this).next().hide().css('height', 0);
	});

	var $_GET = getQueryParameters(document.location.search);
	if($_GET.o != undefined)
	{
		var openPanel = parseFloat($_GET.o);
		var p = $('.srToggle').eq(openPanel);
		p.click();
		p.animate({opacity:1},1000,function(){
			self.scrollTo(0,p.offset().top);
			p.removeAttr('style');
		});
	}
	
});

