// JavaScript Document

jQuery.fn.fadeToggle = function(speed, easing, callback) {
   return this.animate({opacity: 'toggle'}, speed, easing, callback);
};



function reloadArticles(type,cat){

//$j("#articles").html("");
	if(cat < 1 || !cat){cat="0";}
	//alert("Cat: "+cat+", Type: "+type)
	$("#articles").slideUp("fast",function(){
	
		$("#articles").html("<img src=/assets/templates/velofox/img/ajax-loader.gif />");
	
		$.ajax({
			url: 	"/index.php",
			type: 	"GET",
			data: 	"id=939&type="+type+"&cat="+cat,
			success: function(html){
				
				$("#articles").html(html);
							//$("#articles").hide();
							//$("#articles").slideDown("fast");
							location.href = location.href+'&page=1'; 
			}
		})

	});
	
	//$("#catFilter").load("http://www.nezzup.de/index.php?id=940&type="+type+"&cat="+cat);
	//alert("Cat: "+cat+", Type: "+type)
	
	$.ajax({
		url: 	"/index.php",
		type: 	"GET",
		data: 	"id=940&type="+type+"&cat="+cat,
		success: function(html){

			$("#catFilter").html(html);
		}
	})
	

}

function choice(action,article,articlename){
	$.ajax({
		url: 	"/index.php",
		type: 	"GET",
		data: 	"id=941&article="+article+"&action="+action+"&actions=1",
		success: function(html){
                        if(action == "add"){actionstr= " hinzugefügt."};
                        if(action == "delete"){actionstr= " entfernt."};
                       	notifiy("Konfiguration geändert", articlename + actionstr);
						$("#choice").html(html);
                        //reloadArticles('','');
						location.reload();

                        
		}
	})
}

function notifiy(title,text){
                        $.pnotify({ pnotify_title: title, pnotify_text: text ,pnotify_fade_speed: "fast",pnotify_delay: 3000,pnotify_history: true,pnotify_opacity: 1,pnotify_notice_icon: "ui-icon ui-icon-circle-check"	});
}

$(document).ready(function() {
//$("#header").flashembed("/assets/flash/flashheader2.swf");

});

$(document).ready(function() {
//	$("img.reflect").reflect({
//		startOpacity: 0.3,     // Value between 0 and 1
							   //     the starting opacity of the gradient 
							   //     for the reflection
//		reflectionScale: 0.4   // Size of the reflection relative to the 
							   //     size of the image
//	});





if($('div.wlpeUsersList div.wlpeUserPage')){
$('div.wlpeUsersList div.wlpeUserPage').quicksearch({

	attached: '#memberFilter',
	loaderText: '',
	labelText: 'Mitgliedersuche',
	inputText: 'Suchwörter',
	delay: 20
})}

if($(".wlpeMessage")){
	
	$(".wlpeMessage").animate({opacity: 1.0}, 2000).fadeOut({slow:true,delay:10});
	}

});

function submitNewArticle(){
	
	if($("#articleName").val() != "" && $("#cat2").val() != "0"){

	$.ajax({
		url: 	"/index.php?id=944",
		type: 	"POST",
		data: 	"articleName=" + $("#articleName").val()
                              +"&articleSeries=" + $("#articleSeries").val()
                              +"&cat=" + $("#cat2").val()
                              +"&manufacturer2=" + $("#manufacturer2").val()
                              +"&description=" + $("#description").val(),
		success: function(html){
			notifiy("Artikel","Der neue Artikel wurde angelegt.");
			$("#newArticle").html(html);
			location.reload();
		}
	})
	}else{
	if($("#articleName").val() == "") alert("Du musst einen Artikelnamen eingeben...");	
	if($("#cat2").val() == "0") alert("Du musst einen Kategorie auswählen...");		
	}
}

function submitNewArticle2(){
	
	if($("#articleName").val() != "" && $("#cat2").val() != "0"){

	$.ajax({
		url: 	"/index.php?id=944",
		type: 	"POST",
		data: 	"articleName=" + $("#articleName").val()
                              +"&articleSeries=" + $("#articleSeries").val()
                              +"&cat=" + $("#cat2").val()
                              +"&manufacturer=" + $("#manufacturer").val()
                              +"&description=" + $("#description").val(),
		success: function(html){
			$("#newArticle").html(html);
			location.reload();
		}
	})
	}else{
	if($("#articleName").val() == "") alert("Du musst einen Artikelnamen eingeben...");	
	if($("#cat2").val() == "0") alert("Du musst einen Kategorie auswählen...");		
	}
}

function addNewArticle(){

	$("form#new").fadeToggle() 
}


function deleteConfig(){
	$("#choice").fadeOut("fast",function(){
		$.ajax({
		url: 	"/index.php",
		type: 	"GET",
		data: 	"id=945",
		success: function(html){

                       notifiy("Konfiguration geändert", "Alle Komponenten entfernt");
						//$("#choice").html(html);
            
						$("#catFilter").load("index.php?id=940");	 
						$("#articles").load("index.php?id=939");
						$("#choice").load("index.php?id=941");
						  location.reload();

                        
		}
	})
										 })
}

function saveConfig(){
        if($("#cusername").val() == "" || $("#configName").val() == "" || $("#email").val() == ""){
						alert("Es wurden nicht alle Pflichtfelder ausgefüllt!");
	
		}else{
			
				$.ajax({
			url: 	"/index.php?id=956",
			type: 	"POST",
			data: 	"username="+$("#cusername").val()+
					"&configName="+$("#configName").val()+
					"&configDescription="+$("#configDescription").val()+
					"&email="+$("#email").val()+
					"&userid="+$("#userid").val()+
					"&configid="+$("#configid").val()
			,
			success: function(html){
				//alert("ok")
				 notifiy("Konfiguration", "Deine Konfiguration wurde gespeichert:"+html);
				 //deleteConfig();
				 $("#saveConfig").html(html);
			}
		})
			

			
			}
	

}
function reactivate(){

	
		$.ajax({
			url: 	"/index.php?id=958",
			type: 	"POST",
			data: 	"reacode="+$("#reacode").val(),
			success: function(html){
				//alert("ok")
				 notifiy("Konfiguration", html);
				 //deleteConfig();
                        $("#articles").load("index.php?id=939");
						$("#catFilter").load("index.php?id=940");	
						//$("#choice").html("test");
						$("#choice").load("index.php?id=941");
						//location.reload();
						window.location.href = "http://velofox.com/de/raeder-komponenten/konfigurator.html?action=update";
			}
		})
	

}

function deleteCompFromGarage(comp){

	
		$.ajax({
			url: 	"/index.php",
			type: 	"GET",
			data: 	"id=1015&action=delete&comp="+ comp,
			success: function(html){
				//alert("ok")
				 notifiy("Garage", html);
				 //deleteConfig();
				location.reload();
			}
		})
	

}
function fromArchivToGarage(comp){

	
		$.ajax({
			url: 	"/index.php",
			type: 	"GET",
			data: 	"id=1015&action=garage&comp="+ comp,
			success: function(html){
				//alert("ok")
				 notifiy("Garage", html);
				 //deleteConfig();
				location.reload();
			}
		})
	

}
function fromGarageToArchiv(comp){

	
		$.ajax({
			url: 	"/index.php",
			type: 	"GET",
			data: 	"id=1015&action=archiv&comp="+ comp,
			success: function(html){
				//alert("ok")
				 notifiy("Garage", html);
				 //deleteConfig();
				location.reload();
			}
		})
	

}

jQuery.fn.fadeToggle = function(speed, easing, callback) {
   return this.animate({opacity: 'toggle'}, speed, easing, callback);

}; 

function toggleCompList(elm){
	
	$(elm).parent().parent().parent().children().toggle();

}
function flipConfigurator(elm){
	
	$("#configuratorPic").attr("src","/assets/images/konfigurator_mini_mo.jpg");
	elm.onmouseout = function(){$("#configuratorPic").attr("src","/assets/images/konfigurator_mini.jpg");}
	
	}
	
	

// Questions

$(function() {
	$('#manufacturer').change(function(){
		$.ajax({
			url: ('/index.php'),
			data: 'id=936&type=json&'+'mId=' + $("#manufacturer").val() + '&cId=' + $('#cat').val(),
			type: 'GET',
			timeout: 2000,
			dataType: 'json',
			error: function() {
				alert('Error loading json data!');
			},
			success: function(json){
				var output = '';
				for (p in json) {
					output += '<option value=\"' + json[p].article_id + '\">' + json[p].article + '</option>\n';
				}
				$('#componentSelect').html(output);
			}
		})
	});
	$('#manufacturer2').change(function(){
		$.ajax({
			url: ('/index.php'),
			data: 'id=936&type=json&'+'mId=' + $("#manufacturer2").val() + '&cId=' + $('#cat2').val(),
			type: 'GET',
			timeout: 2000,
			dataType: 'json',
			error: function() {
				alert('Error loading json data!');
			},
			success: function(json){
				var output = '';
				for (p in json) {
					output += '<option value=\"' + json[p].article_id + '\">' + json[p].article + '</option>\n';
				}
				$('#componentSelect').html(output);
			}
		})
	});
		
	$('#cat').change(function(){
		$.ajax({
			url: ('/index.php'),
			data: 'id=936&type=json&'+'mId=' + $("#manufacturer").val() + '&cId=' + $('#cat').val(),
			type: 'GET',
			timeout: 2000,
			dataType: 'json',
			error: function() {
				alert('Error loading json data!');
			},
			success: function(json){
				var output = '';
				for (p in json) {
					output += '<option value=\"' + json[p].article_id + '\">' + json[p].article + '</option>\n';
				}
				$('#componentSelect').html(output);
			}
		})
	});
		
	$("#addComponent").click(function(){
			if($("#componentSelect").val() != "0" && $("#componentSelect").val() != ""  && $("#componentSelect").val() != null){
					$.ajax({
						url: ('/index.php?id=1005'),
						data: 'component=' + $('#componentSelect').val(),
						type: 'POST',
						timeout: 2000,
						error: function(html) {
							alert(html);
						},
						success: function(html){ 
							var output = html;
							$("#complist").html(output);
							if($("#component").val() == ""){
								
								$("#component").val($("#componentSelect").val() )
								
							}else{
								
								$("#component").val($("#component").val() + "||"+$("#componentSelect").val())
								}
							
						}
					})	
					$.ajax({
						url: ('/index.php'),
						data: 'id=1006&type=all',
						type: 'GET',
						timeout: 2000,
						error: function(html) {
							alert(html);
						},
						success: function(html){
							var output = html;
							$("#userInConfig").html(output);
							
						}
					})	
					
	
				}else{
					alert("Du musst zuerst einen Hersteller und eine Komponenten auswählen");
					}
			})
$("#comp2garage").click(function(){
  if($("#componentSelect").val() != "0"){ 
	  
	 $.ajax({
			url: 	"/index.php",
			type: 	"GET",
			data: 	"id=1015&action=add&comp="+ $("#componentSelect").val() ,
			success: function(html){
				//alert("ok")
				 notifiy("Garage", html);
				 //deleteConfig();
				location.reload();
			}
		})
										  
										  
	  }
  
  });
});

$(function() { 
 
    // if the function argument is given to overlay, 
    // it is assumed to be the onBeforeLoad event listener 
    $("a[rel]").overlay({ 
 
        expose: 'Gray', 
        effect: 'apple', 
 
        onBeforeLoad: function() { 
 
            // grab wrapper element inside content 
            var wrap = this.getContent().find(".contentWrap"); 
 
            // load the page specified in the trigger 
            wrap.load(this.getTrigger().attr("href")); 
        } 
 
    }); 
});



