function initPos(){
	var divs = document.getElementsByTagName('div');
	var prod = new Array();
	var j=0;
	for (var i = 0; i < divs.length; i++){
		if (divs[i].className.indexOf('productInfobox') != -1) {
			prod[j] = divs[i];
			j++;
		}
	}
	for (var k = 0; k < prod.length; k++) {
		prod[k].style.zIndex = prod.length - k;
	}
}

$(document).ready(function(){

    var base_url="http://www.levidevries.nl/";

    $("#topTab li").mouseover(function(){
        var container=$(this);
        container.addClass('hover');
        container.find('.iconText').show();
    });
    $("#topTab li").mouseout(function(){
        var container=$(this);
        container.removeClass('hover');
        container.find('.iconText').hide();
    });
    
	$(".productAttrForm li span").add($('.prijis em')).add($('.related-sizes span')).mouseover(function(){
        var container=$(this);
        container.addClass('hover');
        container.find('.price-tip').show();
    });
    $(".productAttrForm li span").add($('.prijis em')).add($('.related-sizes span')).mouseout(function(){
        var container=$(this);
        container.removeClass('hover');
        container.find('.price-tip').hide();
    });
	
    $('ul').each(function(){
        $(this).find('li:last').addClass('last-item');
        $(this).find('li:odd').addClass('odd');
        $(this).find('li:even').addClass('even');
        $(this).find('li:first').addClass('first-item');
    });
	 
    
    $('.no-bmargin').css('margin-bottom','0');
    
    $('#mainnav li').not('.current').mouseover(function(){
        $(this).addClass('hover');
    });
    $('#mainnav li').not('.current').mouseout(function(){
        $(this).removeClass('hover');
    });
    
    $(".product-pic a,.productInfobox .productPic a").lightBox();
    $("#product-smallpics a").lightBox();
    $(".tabs").idTabs();
    
    $("#asics-tooltip-trigger").tooltip({
        effect: 'slide',
        position:'top right',
        offset: [8,-40],
        opacity:0.95,
        tip:'#asics'
	}).dynamic({bottom:{offset:[0,-40]},position:'bottom right'});
	
	
    $("#asics-tooltip-trigger2").tooltip({
        effect: 'slide',
        position:'top right',
        offset: [8,-65],
        opacity:0.95,
        tip:'#asics'
	}).dynamic({bottom:{offset:[-8,-65]},position:'bottom right'});
	
		
	
    $('#asics').insertBefore('#wrapper');

    $(".normal-tooltip-trigger").tooltip({
            effect: 'slide',
            position:'top right',
            opacity:0.95,
            offset:[-78,-53],
            tipClass:'normal-tooltip'
    });

    $("input[type=radio]").addClass("radio");
        
    
    
    $("table").each(function(){
        $(this).find('tr:odd').not('.tblHeader').addClass('odd');
        $(this).find('tr:even').not('.tblHeader').addClass('even');
    });
    
    // Note: assumes __CurrentCountry is defined (sits in our template)
    $('select#countries').each(function(){
        var countriesNumber = $(this).find("option").length;
        var iconsArr = new Array(countriesNumber);
        for(var i=1;i<countriesNumber+1;i++)
        {
            var item = {
                find:'.country_'+i
                };
            iconsArr.push(item);
        }
        $(this).val(__CurrentCountry);
        $(this).change(function()
        {
            window.location = "/change_lc.php?country_id=" + $(this).val() + "&url=" + escape(window.location);
        });
        $(this).selectmenu({
            icons: iconsArr ,
            width: 128,
            style: 'dropdown',
            menuWidth: 339
        });
    });

    // Note: assumes __CurrentLanguage is defined (sits in our template)
    $('select#languages').each(function(){
        var countriesNumber = $(this).find("option").length;
        var iconsArr = new Array(countriesNumber);        
        for(var i=1;i<countriesNumber+1;i++)
        {
            var item = {
                find:'.language_'+i
                };
            iconsArr.push(item);
        }
        $(this).val(__CurrentLanguage);    
        $(this).change(function()
        {
            window.location = "/change_lc.php?language_id=" + $(this).val() + "&url=" + escape(window.location);
        });
        $(this).selectmenu({
            icons: iconsArr,
            width: 128,
            style: 'dropdown',
            menuWidth: 339
        });                 
    });
    
    //otherProducts box gallery
    $('.productsGallery').serialScroll({
        items:'li',
        prev:'#otherProducts-prev',
        next:'#otherProducts-next',
        duration:500,
        start:0,
        step:1,
        force:true,
        cycle:false,
        //easing:'easeOutSine',
        lock:false
			
    /*
			onBefore:function( e, elem, $pane, $items, pos ){
				var the_items=$("#mens-gallery li");
                var lastPos=the_items.length-1;
				if(pos==0)
				{
					setTimeout(function(){
						$('#mens-gallery-prev').hide();
					    $('#mens-gallery-next').show();				
					},1000);
					
				}
				else if(pos==lastPos)
				{
					setTimeout(function(){
						$('#mens-gallery-prev').show();
					    $('#mens-gallery-next').hide();				
					},1000);
					
				}
				else
				{
					$('#mens-gallery-prev').hide();
		            $('#mens-gallery-next').show();
				}
			}
			*/
			
    });
        
    // other images gallery
    $('.gallery-holder').serialScroll({
        items:'li',
        prev:'#gallery-holder-prev',
        next:'#gallery-holder-next',
        duration:500,
        start:0,
        step:1,
        force:true,
        cycle:false,
        lock:false			
    });
    
	initPos();
    $('.doinwinkel-dropdown-trigger').click(function(){
        var dropdown = $(this).parents('.doinwinkel').find(".doinwinkel-dropdown");
        var product_name =  $(this).attr('productname');
        dropdown.toggle();
        if (product_name != '')
        {
            $(this).attr('productname', '');    // clear, prevents reloading for every toggle
            var content_div = $(this).parents('.doinwinkel').find(".doinwinkel-dropdown-content");
            content_div.load('/ajax_attributes/' + product_name, function(responseText, textStatus, XMLHttpRequest)
            {
                if (textStatus == 'success' || textStatus == 'notmodified')
                { // successfully loaded
                    // add ajaxy stuff to the form (post it when submitted)
                    content_div.find(".productAttrForm").ajaxForm(               //"In Winkelwagen" button clicked
                    {
                        dataType: "json",
                        success: function(responseText, statusText, xhr, $form)
                        {
                            showProductAttrFormResponse(responseText, statusText, xhr, $form);
                            dropdown.css('zIndex', 10);// decrease z-index fall behind other dropdowns
                            dropdown.hide();
                        }
                    }
                    );
                    // add on-click handlers on attribute checkboxes: update price and stock
                    content_div.find(".productAttrForm input[type=radio]").click(function()
                    {
                        UpdatePriceAndStock($(this).parents(".productAttrForm"));
                    }
                    );
                    // add on-lockk handler to annuleer button
                    content_div.find("#annuneerBtn").click(function(){
                        dropdown.css('zIndex', 10);// decrease z-index fall behind other dropdowns
                        dropdown.hide();
                        return false;
                    });
                    UpdatePriceAndStock(content_div.find(".productAttrForm"));
                }
            }
            );;
        }
        return false;
    });
		
    $(".doinwinkel-dropdown #annuneerBtn").click(function(){
        $(this).parents(".doinwinkel").find(".doinwinkel-dropdown").hide();
			$(this).parents(".doinwinkel").find(".doinwinkel-dropdown").css('zIndex', 10);
        return false;
    });
		
    //select checkbox on seletion wizzard page
    $(".selection-checkbox").click(function(){
        if($(this).attr("checked")==true)
        {
            $(this).parents("tr").addClass("selectedProduct");
        }
        else
        {
            $(this).parents("tr").removeClass("selectedProduct");
        }
    });
		
    //mouse over select checkbox on seletion wizzard page
    $(".checkboxColumn").mouseover(function(){
        $(this).parents("tr").addClass("mouseoverProduct");
    });
    $(".checkboxColumn").mouseout(function(){
        $(this).parents("tr").removeClass("mouseoverProduct");
    });
        
    $("#matrixTbl tr").mouseover(function(){
        $(this).find("td").css("background","#feffcf");
    });
    $("#matrixTbl tr").mouseout(function(){
        $(this).find("td").css("background","#fff");
    });
		
		
    $("#shoppingTbl td:last").css("padding","10px 0 0 0");
		
		
		
    $(".paymentDeliveryList .editBtn").click(
        function(){
            var button = $(this);
            var section = button.parents("li[title=editmode]");
            $(".edit").each(function(){
                $(this).find("form").children().eq(2).trigger("click");
            });
            $(".edit").editable("disable");
            if(section.length<=0)
            {
                var editlines=button.parents("li").find(".edit");
                $("li[title=editmode]").each(function(){
                    $(this).find("button[type=cancel]").trigger("click");
                    $(this).attr("title","");
                });
                button.parents("li").attr("title","editmode");
                editlines.each(function(){
                    $(this).editable("enable");
                    $(this).editable(base_url+'server/updateBezorging.php',
                    {
                        submit:"OK",
                        width:150,
                        cancel: "Cancel",
                        onblur: "ignore",
                        tooltip: ""
                    }).trigger("click");
                });
            }
            else if(section.length>0)
            {
                $(this).parents("li").attr("title","");
                $(this).parents("li").find(".edit").editable("disable");
                $(this).parents("li").find(".edit").each(function(){
                    $(this).find("form").children().eq(2).trigger("click");
                });
            }
            return false;
        }
        );
		
		
    $(".shoppingForm li:first").css('position','relative');
});
