//animate the opening of the branch (span.grower jQueryElement)
function openBranch(jQueryElement, noAnimation, isclick) {

		if(noAnimation && isclick){
			//jQueryElement.parent().parent().find('ul').hide();
			jQueryElement.parent().parent().find('.open').addClass('close').removeClass('open');
		}
		else{
			if(isclick)
				jQueryElement.parent().parent().find('ul').slideUp();
		}
		if(isclick)
			jQueryElement.parent().parent().find('span').addClass('close').removeClass('open');

		jQueryElement.addClass('open').removeClass('close');
		jQueryElement.parent().addClass('open').removeClass('close');
		
		if(noAnimation)
			jQueryElement.parent().find('ul:first').show();
		else
			jQueryElement.parent().find('ul:first').slideDown();
}
//animate the closing of the branch (span.grower jQueryElement)
function closeBranch(jQueryElement, noAnimation) {
	
	jQueryElement.addClass('close').removeClass('open');
	jQueryElement.parent().find('span').addClass('close').removeClass('open');

	jQueryElement.parent().addClass('close').removeClass('open');

	if(noAnimation)
		jQueryElement.parent().find('ul').hide();
	else
		jQueryElement.parent().find('ul').slideUp();
}

//animate the closing or opening of the branch (ul jQueryElement)
function toggleBranch(jQueryElement, noAnimation, isclick) {
	if(jQueryElement.hasClass('open'))
		closeBranch(jQueryElement, noAnimation);
	else
		openBranch(jQueryElement, noAnimation, isclick);
}

//when the page is loaded...
$(document).ready(function () {
	//to do not execute this script as much as it's called...
	if(!$('ul.tree.dhtml').hasClass('dynamized'))
	{
		//add growers to each ul.tree elements
		$('ul.tree.dhtml ul').prev().before("<span class='grower close'> </span>");
		
		//dynamically add the '.last' class on each last item of a branch
		$('ul.tree.dhtml ul li:last-child, ul.tree.dhtml li:last-child').addClass('last');
		
		//collapse every expanded branch
		$('ul.tree.dhtml > li').addClass('close');
		$('ul.tree.dhtml ul span.grower.close').parent().find('ul:first').hide();
		$('ul.tree.dhtml').show();
		
		//open the tree for the selected branch
		$('ul.tree.dhtml .selected').parents().each( function() {
			if ($(this).is('ul'))
				openBranch($(this).prev().prev(), true, false);
		});
		toggleBranch( $('ul.tree.dhtml .selected').prev(), true, false);
		
		//add a function on clicks on growers
		$('ul.tree.dhtml span.grower').click(function(){
			toggleBranch($(this), true, true);			
			$('ul.tree').find('span.grower.close').each( function() {
				$(this).parent().find('ul:first').hide();
			});
		});
		//mark this 'ul.tree' elements as already 'dynamized'
		$('ul.tree.dhtml').addClass('dynamized');

		$('ul.tree.dhtml').removeClass('dhtml');
	}
	
	
	var COOKIE_NAME = 'parteaz_shopmode';
	var options = { path: '/', expires: 10 };
	
	// get cookie
	if($.cookie(COOKIE_NAME) == 'quick'){
		$('#product_list').addClass('quick');
		$('#quick-shop').addClass('selected');
		$('#full-shop').removeClass('selected');
	} 
	
	$('#full-shop').click(function(){
		$.cookie(COOKIE_NAME, 'full', options);
		$('#product_list').removeClass('quick');
		$('#quick-shop').removeClass('selected');
		$('#full-shop').addClass('selected');
	});
	
	$('#quick-shop').click(function(){
		$.cookie(COOKIE_NAME, 'quick', options);
		$('#product_list').addClass('quick');
		$('#quick-shop').addClass('selected');
		$('#full-shop').removeClass('selected');
	});
	

	
});


/**
 * 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;
    }
};

