BindEvents();
BindGearlist();
updateBlognewEvents();
syncContainer();

function BindEvents() {
    $('.toplink').click(function() { 
        var v = $(this).next().is(':visible');
        if (!v) {
           $(this).next().show(); 
        }
        else {
           $(this).next().hide(); 
        }
        return false; 
    });
}

function BindGearlist() {
    $('.gledit_container select').change(function() {
        $(this).parents('form').submit();
        return false;
    });
}

function syncContainer(){
    $('#gledit_container').append(function(index, html) {
       ret = "";
       chk = [];
       $('#gledit_container option').each(function()
        {
            chk.push($(this).val());
        });
       $("#changecont_container option").each(function()
        {
           if ($.inArray($(this).val(), chk) == -1) {
             ret += '<option value="'+$(this).val()+'">'+$(this).text()+'</option>';
             chk.push($(this).val());
           }
        });
       return ret;
    });
}

$("#h3_rc").append('<div rel="0" id="del_dialog">Willst Du den Kommentar wirklich löschen?</div>');

$("#del_dialog").dialog({
    	resizable: false,
    	modal: true,
    	autoOpen:false,
    	buttons: {
    		"Ja": function() {
    			$(this).dialog( "close" );
    			window.location.href = $('#mainlink_home').attr("href") + "TestberichtKommentarloeschen/"+$(this).attr("rel");
    		},
    		"Nein": function() {
    			$(this).dialog( "close" );
    		}
    	}
});

$(".del_ratingcomment").click(function() {
    $("#del_dialog").attr("rel",$(this).attr("rel"));
    $("#del_dialog").dialog("open");
});

$('.wd_add2gearlist input[type=submit]').click(function() {
     $(this).after(' <img src="/images/loading_small.gif" height="13px" id="tmp_loading">');
     var action = $(this).parents('form');
     var dataString = 'addwitem[ammount]='+ action.find('#addwitem_ammount').val()
                    + '&addwitem[gearlistid]=' + action.find('#addwitem_gearlistid').val()
                    + '&addwitem[witemid]=' + action.find('#addwitem_witemid').val()
                    + '&addwitem[container]=' + $('#gledit_container').val()
                    + '&addwitem[_csrf_token]=' + action.find('#addwitem__csrf_token').val();   
     $.ajax({  
       type: "POST",  
       url: action.attr("action"),  
       data: dataString,  
       success: function(ret) { 
         ret = jQuery.parseJSON(ret);
         $('#gearlistdisplay').load(ret,function() {
            BindGearlist();
            syncContainer();
            $('#tmp_loading').remove();
            });
       }  
     });  
  return false; 
});

$('#mainlink_quick').click(function() { 
    var url =  $('#mainlink_home').attr("href") + "stock/MyWdAjax"; 
    var v = $('#quickbox').is(':visible');
    if (!v) {
        $('#quickbox').load(url, function(){
            BindEvents();
            });
        $('#quickbox').fadeIn(400,function(){
            
            });
        }
    else {
       $('#quickbox').fadeOut(400,function() {});
        }
    return false; 
});


$('.itemlink').click(function() { 
    var url = this.id; 
    var queryString = url.replace('ail_',''); 
    var v = $('#aif_'+queryString).is(':visible');
    $('.add_item_form').each(function(index){
        $(this).hide();
        });
    if (!v) {
        $('#aif_'+queryString).show(); }
    return false; }
); 

$("#order_sel").change(function() {
	var expl = window.location.href.split("/");
	var n = expl.length;
	expl[n-3] = "20";
	expl[n-4] = "0";
	expl[n-2] = $(this).val();
	var str = expl[0];
	for (i=1;i<n;i++) {
		str += "/"+expl[i];
	}
	window.location.href = str;
});

$("#brand_search_review").autocomplete({
        minLength: 2,
		source: "/brand/Search2Ajax",
		select: function(event, ui) {
			var expl = window.location.href.split("/");
			var n = expl.length-1;
			k = $.inArray("Testberichte",expl);
			if (n==k) {
			  expl[k+1] = "gear";
			  expl[k+2] = "1"; 
			} 
			k+=2;
			expl[k+1] = ui.item.value;
			expl[k+2] = ui.item.id;
			var str = expl[0];
			for (i=1;i<=k+2;i++) {
				str += "/"+expl[i];
			}
		window.location.href = str;
		}
	});

$("#brand_search").autocomplete({
        minLength: 2,
		source: "/brand/Search2Ajax",
		select: function(event, ui) {
			var expl = window.location.href.split("/");
			var n = expl.length;
			expl[n-3] = "20";
			expl[n-4] = "0";
			expl[n-5] = ui.item.value;
			expl[n-6] = ui.item.id;
			var str = expl[0];
			for (i=1;i<n;i++) {
				str += "/"+expl[i];
			}
		window.location.href = str;
		}
	});

$("#newproduct_brand").autocomplete({
    minLength: 2,
	source: "/brand/Search2Ajax",
	select: function(event, ui) {
		$("#newproduct_brand_id").val(ui.item.id);
	}
});

$("#product_search").autocomplete({
    minLength: 3,
	source: "/product/SearchAjaxSlug",
	select: function(event, ui) {
	    href = $("#mainlink_home").attr("href");
        window.location.href = href+'Produkt/'+ui.item.id;
		return false;
	}
});

// Blog NEW

$("#blog_brand_select").autocomplete({
    minLength: 2,
	source: "/brand/Search2Ajax",
	select: function(event, ui) {
		$("#blog_brand_select").val('');
        addToBlogBrands(ui.item.id, ui.item.value)
		return false;
	}
});

function addToBlogBrands(brandid, brand){
    brands = $("#blog_brands").val();  
    brands = brands.split(";");
    i = $.inArray(brandid, brands);
    if (i == -1) {
      $("#blog_brands").val($("#blog_brands").val() + brandid + ";");
      $("#brands").append('<a class="add_blog_brand" rel="'+ brandid +'">' + brand  + "</a>");
      updateBlognewEvents();
    } 
}

$("#blog_product_select").autocomplete({
    minLength: 3,
	source: "/product/SearchAjax",
	select: function(event, ui) {
		$("#blog_product_select").val('');
        addToBlogProducts(ui.item.id, ui.item.value)
		return false;
	}
});

function addToBlogProducts(prodid, prod){
    prods = $("#blog_products").val();  
    prods = prods.split(";");
    i = $.inArray(prodid, prods);
    if (i == -1) {
      $("#blog_products").val($("#blog_products").val() + prodid + ";");
      $("#products").append('<a class="add_blog_prod" rel="'+ prodid +'">' + prod  + "</a>");
      updateBlognewEvents();
    } 
}

$("#blog_category_select").change(function(){
     addToBlogCategories($("#blog_category_select").val(),$("#blog_category_select :selected").text());
     $("#blog_category_select").val(0);
});

function addToBlogCategories(catid, cat){
    cats = $("#blog_categories").val();  
    cats = cats.split(";");
    i = $.inArray(catid, cats);
    if (i == -1) {
      $("#blog_categories").val($("#blog_categories").val() + catid + ";");
      $("#categories").append('<a class="add_blog_category" rel="'+ catid +'">' + cat  + "</a>");
      updateBlognewEvents();
    } 
}

$('#blog_tag_select').keypress(function(e){
      if(e.which == 13){
        $("#blog_tag_select").autocomplete("close");
        addToBlogTags($('#blog_tag_select').val());
        $('#blog_tag_select').val('');
        return false;
       }
      });

$("#blog_tag_select").autocomplete({
    minLength: 2,
	source: "/blog/SearchTagAjax",
	select: function(event, ui) {
		$("#blog_tag_select").val('');
        addToBlogTags(ui.item.value);
		return false;
	}
});

function addToBlogTags(tag){
    tags = $("#blog_tags").val();  
    tags = tags.split(";");
    i = $.inArray(tag, tags);
    if (i == -1) {
      $("#blog_tags").val($("#blog_tags").val() + tag + ";");
      $("#tags").append('<a class="add_blog_tag" rel="'+ tag +'">' + tag  + "</a>");
      updateBlognewEvents();
    } 
}

function updateBlognewEvents(){
    $('.add_blog_brand').click(function() { 
        brands = $("#blog_brands").val();  
        brands = brands.split(";");
        brandid = $(this).attr("rel");
        $("#blog_brands").val('');
        for (i=0;i<brands.length;i++){
            if (brands[i]!=brandid && brands[i]!=""){
                $("#blog_brands").val($("#blog_brands").val() + brands[i] + ";");
            }
        }
        $(this).remove();         
    });
    
    $('.add_blog_category').click(function() { 
        cats = $("#blog_categories").val();  
        cats = cats.split(";");
        catid = $(this).attr("rel");
        $("#blog_categories").val('');
        for (i=0;i<cats.length;i++){
            if (cats[i]!=catid && cats[i]!=""){
                $("#blog_categories").val($("#blog_categories").val() + cats[i] + ";");
            }
        }
        $(this).remove();         
    });
   
    $('.add_blog_tag').click(function() {
        tags = $("#blog_tags").val();
        tags = tags.split(";");
        tag = $(this).attr("rel");
        $("#blog_tags").val('');
        for (i=0;i<tags.length;i++){
            if (tags[i]!=tag && tags[i]!=""){
                $("#blog_tags").val($("#blog_tags").val() + tags[i] + ";");
            }
        }
        $(this).remove();         
    });
    
    $('.add_blog_prod').click(function() { 
        prods = $("#blog_products").val(); 
        prods = prods.split(";");
        prodid = $(this).attr("rel");
        $("#blog_products").val('');
        for (i=0;i<prods.length;i++){
            if (prods[i]!=prodid && prods[i]!=""){
                $("#blog_products").val($("#blog_products").val() + prods[i] + ";");
            }
        }
        $(this).remove();         
    });
}


