/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

jQuery(document).ready(function($) {
	
	$('ul.SubCategories').each(function() {
		$(this).children('li').filter(':last').css('border-bottom', 'none');
	});
	$('#execphp-465343471 li:last').css('border-bottom', 'none');
	
	$('#execphp-465343471 > h4').wrapInner('<a href="/faq"></a>');
	$('#execphp-465343471 > div > ul').hide();
	$('#execphp-465343471 > h4 > a').bind('click', function() {
		if ($(this).parent('h4').next('div').children('ul').is(':hidden')) {
			$('#sidebar-1 li.MainCategory').children('strong').children('a').removeClass('active');
			$('#sidebar-1 ul.SubCategories').hide();
			$(this).addClass('active');
			$(this).parent('h4').next('div').children('ul').slideDown('fast', function() {
				var faqVisible = ($(this).is(':visible')) ? 'true' : 'false';
				$.cookie('faq', null, {path: "/"});
				$.cookie('subcat0', null, {path: "/"});
				$.cookie('subcat1', null, {path: "/"});
				$.cookie('subcat2', null, {path: "/"});
				$.cookie('subcat3', null, {path: "/"});
				$.cookie('faq', faqVisible, {path: "/"});
			});
		}
		return false;
	});
	
	if ($.cookie('faq') == 'true' || (!$.cookie('subcat0') && !$.cookie('subcat1') && !$.cookie('subcat2') && !$.cookie('subcat3'))) {
		$('#execphp-465343471 > h4 > a').addClass('active');
		$('#execphp-465343471 > div > ul').show();
	}
	
	$('#sidebar-1 ul.SubCategories').hide();
	$('#sidebar-1 li.MainCategory').children('strong').children('a').bind('click', function() {
		if ($(this).parent('strong').next('ul.SubCategories').is(':hidden')) {
			$('#execphp-465343471 > h4 > a').removeClass('active');
			$('#sidebar-1 li.MainCategory').children('strong').children('a').removeClass('active');
			$('#execphp-465343471 > div > ul').hide();
			$('#sidebar-1 ul.SubCategories').hide();
			$(this).addClass('active');
			$(this).parent('strong').next('ul.SubCategories').slideDown('fast', function() {
				var subcatIndex = $('ul.PeCategories ul.SubCategories').index($(this));
				var subcatVisible = ($(this).is(':visible')) ? 'true' : 'false';
				$.cookie('faq', null, {path: "/"});
				$.cookie('subcat0', null, {path: "/"});
				$.cookie('subcat1', null, {path: "/"});
				$.cookie('subcat2', null, {path: "/"});
				$.cookie('subcat3', null, {path: "/"});
				$.cookie('subcat'+subcatIndex, subcatVisible, {path: "/"});
			});
		}
		return false;
	});
	
	if ($.cookie('subcat0') == 'true') {
		$('#sidebar-1 li.MainCategory').children('strong').children('a').filter(':eq(0)').addClass('active');
		$('#sidebar-1 li.MainCategory').children('ul').filter(':eq(0)').show();
	}
	if ($.cookie('subcat1') == 'true') {
		$('#sidebar-1 li.MainCategory').children('strong').children('a').filter(':eq(1)').addClass('active');
		$('#sidebar-1 li.MainCategory').children('ul').filter(':eq(1)').show();
	}
	if ($.cookie('subcat2') == 'true') {
		$('#sidebar-1 li.MainCategory').children('strong').children('a').filter(':eq(2)').addClass('active');
		$('#sidebar-1 li.MainCategory').children('ul').filter(':eq(2)').show();
	}
	if ($.cookie('subcat3') == 'true') {
		$('#sidebar-1 li.MainCategory').children('strong').children('a').filter(':eq(3)').addClass('active');
		$('#sidebar-1 li.MainCategory').children('ul').filter(':eq(3)').show();
	}
	
	var legendText = $('#cformsform fieldset.cf-fs1 > legend:eq(0)').text();
	$('#cformsform fieldset.cf-fs1 > legend:eq(0)').replaceWith('<span class="legend">'+legendText+'</span>');
	
	$('#cforms2form, #cforms3form').show().wrap('<div class="popup-container"><div class="popup-form"></div></div>');
	$('div.popup-container').prepend('<div class="popup-bg"></div>');
	$('div.popup-form').prepend('<div class="popup-form-bg-top"></div><div class="popup-form-bg-middle"></div><div class="popup-form-bg-bottom"></div><div class="popup-form-close"></div>');
	$('div.popup-bg').css('opacity', '0.6');
	$('div.popup-container').hide().appendTo('body');
	
	$('#open-request-form').bind('click', function() {
		var popupWidth = $(document).width();
		var popupHeight = $(window).height();
		var formContainer = $('#cforms2form').parent('div').parent('div');
		$(formContainer).css('width', popupWidth+'px').css('height', popupHeight+'px');
		$(formContainer).show();
		var containerTop = $('#cforms2form').parent('div').height()/2;
		$('#cforms2form').parent('div').css('margin-top', '-'+containerTop+'px');
		var middleHeight = $('#cforms2form').parent('div').height() - 32;
		$('#cforms2form').siblings('div.popup-form-bg-middle').css('height', middleHeight+'px');
		
		if ($.browser.msie && $.browser.version < 7) {
			window.scroll(0, 0);
			var popupHeight = $(document).height();
			$('div.popup-bg').css('height', popupHeight+'px');
		}
		return false;
	});
	$('#open-question-form').bind('click', function() {
		var popupWidth = $(document).width();
		var popupHeight = $(window).height();
		var formContainer = $('#cforms3form').parent('div').parent('div');
		$(formContainer).css('width', popupWidth+'px').css('height', popupHeight+'px');
		$(formContainer).show();
		var containerTop = $('#cforms3form').parent('div').height()/2;
		$('#cforms3form').parent('div').css('margin-top', '-'+containerTop+'px');
		var middleHeight = $('#cforms3form').parent('div').height() - 32;
		$('#cforms3form').siblings('div.popup-form-bg-middle').css('height', middleHeight+'px');
		
		if ($.browser.msie && $.browser.version < 7) {
			window.scroll(0, 0);
			var popupHeight = $(document).height();
			$('div.popup-bg').css('height', popupHeight+'px');
		}
		return false;
	});
	$('div.popup-bg, div.popup-form-close').bind('click', function() {
		$('div.popup-container').hide();
	});
	
	$('input.sendbutton').attr('value', ' ');
	
	var currentCat = $('#products_page_container').attr('rel');
	$('a.catlink_'+currentCat).addClass('activelink');
	
});
