//
// Claudio Gobetti 
// updated at 30/06/2010
// For template: danielstores2010
//

// SHOP MENU
$(document).ready(function(){

    /*
    $(".shop_nav > ul").addClass("level-1");
    $(".shop_nav > ul > li > ul").addClass("level-2");
    $(".shop_nav > ul > li > ul > li > ul").addClass("level-3");
    $(".shop_nav > ul > li").addClass("li-1");
    $(".shop_nav > ul > li > ul > li").addClass("li-2");
    $(".shop_nav > ul > li > ul > li > ul > li").addClass("li-3");
    $(".shop_nav > ul > li > a").addClass("a-1");
    $(".shop_nav > ul > li > ul > li > a").addClass("a-2");
    $(".shop_nav > ul > li > ul > li > ul > li > a").addClass("a-3");
    */
    
	// menu drop-down length
	$("ul.level-2").addClass("clearfix");
	$("ul.level-2").each(function(index){
		var count = $(this).children("li.li-2").length;
		var width = count * 170; // see CSS .level-2
		$(this).css("width", String(width+"px"));
	});
	
	// menu drop-down alignment
	$("ul.level-2").each(function(index){
		var menu_borders = 2;
		var max_menu_width = 940 - menu_borders; // CSS max width 940 minus bordes
		var position = $(this).parent().position();
		var li_left = parseInt(position.left);
		var li_right = parseInt(position.left) + parseInt($(this).parent().width());
		var count = $(this).children("li.li-2").length;
		var ul_width = parseInt($(this).width()); //count * 170; // see CSS .level-2
		var ul_left = 0;
		var delta = 0;
		
		var li_mid = parseInt((li_right - li_left) / 2 + li_left);
		
		var menu_lenght = ul_width + li_left;
		
		if( (max_menu_width/2) > li_mid ) // first half of the menu
		{
			//align to left
			if( menu_lenght > max_menu_width  )
			{
				delta = max_menu_width - menu_lenght;
				$(this).css("left", String(delta + "px"));	
			}
			else
			{
				$(this).css("left", "0px");		
			}
		}
		else // second half of the menu
		{
			// align to right
			if( ul_width > li_right )
			{
				delta = li_right - ul_width - menu_borders;
				$(this).css("right", String(delta + "px"));	
				//$(this).css("left", "auto");	
			}
			else
			{
				$(this).css("right", "0px");	
				//$(this).css("left", "auto");	
			}
		}
	});
	
	
	// LIST item align to 960gs
	if( !($.browser.msie && $.browser.version < 7) )
	{
		// index_item
	    $(".product_grid_container .index_item").each(function(index) {
			var i = parseInt(index / 4);
			$(this).css("margin-left", "0");
			if( index % (0 + (4 * i )) == 0 || index==0 )
			{
				$(this).css("margin-left", "0");
				$(this).css("margin-right", "10px");
			}
			else if( index % (3 + (4 * i )) == 0 )
			{
				$(this).css("margin-left", "10px");
				$(this).css("margin-right", "0");
			}
			else
			{
				$(this).css("margin-left", "10px");
				$(this).css("margin-right", "10px");
			}
		});
		
		// small_item
		$(".small_item").each(function(index){
			var i = parseInt(index / 4);
			$(this).css("margin-left", "0");
			if( index % (0 + (4 * i )) == 0 || index==0 )
			{
				$(this).css("margin-left", "0");
				$(this).css("margin-right", "10px");
			}
			else if( index % (3 + (4 * i )) == 0 )
			{
				$(this).css("margin-left", "10px");
				$(this).css("margin-right", "0");
			}
			else
			{
				$(this).css("margin-left", "10px");
				$(this).css("margin-right", "10px");
			}
		});
	}
	
})


// TESTIMONIALS
$(document).ready(function() {
    function update_testimonial() {
        $('#testimonial').hide();
        $.ajax({
            type: "GET",
            url: "/media_content/upload/testimonials.xml",
            dataType: "xml",
            success: function(xml) {
                var $quotes = $(xml).find('quote');
                randNum = (Math.floor(Math.random() * $quotes.length + 1)) - 1;
                var desc = $quotes.eq(randNum).find('quote_text').text();
                var author = $quotes.eq(randNum).find('quote_author').text();
                $('#testimonial').fadeIn('slow');
                $('#testimonial').html('<blockquote><p><a href="/testimonials/">' + desc + '</a></p><div><a href="/testimonials/">Read more<a></div></blockquote><p class="right"><em>' + author + '</em></p>');
                window.setTimeout(update_testimonial, 5000);

            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                $('#testimonial').html('Timeout error...');
                window.setTimeout(update_testimonial, 10000);
            }
        });
    }
    $('#testimonial').html('loading...');
    update_testimonial();
});


//SEARCH value animation
$(document).ready(function() {
    $("input#query").each(function() {
        $(this).click(function() {
            if (this.value == this.defaultValue) {
                this.value = '';
            }
        });
        $(this).blur(function() {
            if (this.value === '') {
                this.value = this.defaultValue;
            }
        });
    });
});  
