/**
 * @copyright  Copyright (c) Simple Web-Solutions GmbH
 * info@simple-web-solutions.de
 * www.simple-web-solutions.de
 *
 * @category   SWS
 * @package    SWS_Click2Call
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 * @author     Julia Rabai
 */

jQuery.noConflict();
jQuery(document).ready(function(){

	//Wenn auf dieser Seite ClickCall existiert
    if(jQuery('.mini-clickcall') && jQuery('#baseUrl')){
    	baseurl = jQuery('#baseUrl').val()+"click2call/";
    	var pin = '';
    	var shop_session = '';
    	if(jQuery("#session").length != 0){
    		var shop_session = jQuery("#session").val();
    	}
    	if(jQuery("#pin").length != 0){
    		var pin = jQuery("#pin").val();
    		var pin_text = jQuery(".cc_messages .pin").html();
    	}
    	
		//Meldungen zwischenspeichern
		var error_1 = jQuery(".cc_messages .error_1").html();
		var error_2 = jQuery(".cc_messages .error_2").html();
		var error_3 = jQuery(".cc_messages .error_3").html();
		var error_4 = jQuery(".cc_messages .error_4").html();
		var state_connect_1 = jQuery(".cc_messages .state_connect_1").html();
		var state_connect_2 = jQuery(".cc_messages .state_connect_2").html();
		var state_connect_3 = jQuery(".cc_messages .state_connect_3").html();
		var state_end = jQuery(".cc_messages .state_end").html();
		
		var frame_form = jQuery(".mini-clickcall .frame_form").html();
		var no_function = jQuery(".mini-clickcall .no_function").html();
		
		jQuery(".mini-clickcall .content_holder").empty();

		//ist der Dienst erreichbar?
	    jQuery.ajax({
	        type: "GET",
	        url: baseurl+"active.php",
	        data: "{}",
	        contentType: "application/json; charset=utf-8",
	        dataType: "json",
	        jsonp:"callback",
	        success: function(msg) {
	        	if(msg.type == "info" && msg.code == "20000"){
	        	//Dienst erreichbar	
		        	if(jQuery('.mini-clickcall')){
						jQuery('.mini-clickcall').show();
	                    jQuery('.mini-clickcall .content_holder').html(frame_form);
	                }
	        	}
	        	if(msg.type == "info" && (msg.code == "20001"||msg.code == "20002")){	
                //ausserhalb der Geschäftszeiten oder keine Destination
                  	if(jQuery('.mini-clickcall')){
						jQuery('.mini-clickcall').show();
						jQuery('.mini-clickcall .content_holder').html(no_function);
	                }
                }
	        },
             complete:function() {
					//Wenn auf den Button geklickt wird
                   jQuery('#callService').click(function(){
                   		//ist die Checkbox angehakt, falls vorhanden?
                   		if(jQuery('#agree').length != 0){
                   			agree = jQuery('#agree:checked').length;
                   		}else{
                   			agree = 1;
                   		}
                   		//nur wenn die Checkbox angehakt ist, falls vorhanden
                   		if(agree != 0 && jQuery('#callnumber').val() != ''){
                   	        var number = jQuery('#callnumber').val();
							//den Error löschen, falls es schon einen gab
							jQuery(".mini-clickcall .error").empty();
                   	        //den Anruf anstoßen
                            jQuery.ajax({
				                type: "GET",
				                url: baseurl+"call.php?&pin="+pin+"&shop_session="+shop_session+"&number="+number,
				                data: "{}",
				                contentType: "application/json; charset=utf-8",
				                dataType: "json",
				                jsonp:"callback",
				                success: function(msg) {
				                	//es ist ein Fehler passiert
				                	if(msg.type == "error"){
				                		jQuery(".mini-clickcall .error").html(error_1);
				                	}
									//es ist ein Fehler passiert
				                	if(msg.type == "info" && msg.code == "20004"){ 
				                	    jQuery(".mini-clickcall .error").html(error_2);
				                	}
				                	//Unknown message sent by DTAG
				                	if(msg.type == "info" && msg.code == "20005"){ 
				                	    jQuery(".mini-clickcall .error").html(error_2);
				                	}
				                    if(msg.type == "info" && msg.code == "20003"){
				                    	//die Form und den Button verstecken
			                   	        jQuery(".mini-clickcall .initial").hide();
										jQuery(".mini-clickcall .form").hide();
										jQuery(".mini-clickcall .actions").hide();
										//einen Status ausgeben
			                            jQuery(".mini-clickcall .status").html(state_connect_1);
			                            if(pin != ''){
											jQuery(".mini-clickcall .pin").html(pin_text);
			                            }
				                        jQuery('body').everyTime(1000, 'controlled', function() {
				                            jQuery.ajax({
				                                type: "GET",
				                                url: baseurl+"status.php?callservice_id="+msg.id+"&shop_session="+shop_session+"&session="+msg.message+"&number="+number,
				                                data: "{}",
				                                contentType: "application/json; charset=utf-8",
				                                dataType: "json",
				                                jsonp:"callback",
				                                success: function(stat) {
													//Es klingelt beim Benutzer
				                                	if(stat.type == "info" && stat.code == "20007"){
                                                        jQuery(".mini-clickcall .status").html(state_connect_2);
				                                	}
													//der Benutzer hört die Ansage
				                                	if(stat.type == "info" && (stat.code == "20009" || stat.code == "20008")){
                                                        jQuery(".mini-clickcall .status").html(state_connect_3);
                                                    }
													//
                                                    if(stat.type == "info" && stat.code == "20010"){
                                                        jQuery(".mini-clickcall .status").html(state_connect_4);
                                                    }
													//Die Verbindung wurde beendet
                                                    if(stat.type == "info" && stat.code == "20011"){
                                                        jQuery(".mini-clickcall .status").html(state_end);
                                                    }
													//Die Verbindung wurde endgültig beendet
				                                    if(stat.type == "info" &&  stat.code == "20006"){
				                                    	//only if there's no PIN
				                                    	if(pin == ''){
	                                                        jQuery('body').stopTime('controlled');
															//blind out step by step, cause otherwise the action an the button is gone.
									                        jQuery(".mini-clickcall .status").empty();
															jQuery(".mini-clickcall .error").empty();
															jQuery(".mini-clickcall .initial").show();
															jQuery(".mini-clickcall .form").show();
															jQuery(".mini-clickcall .actions").show();
				                                    	}else{
				                                    		//reload to get a new PIN & to get the updated data
				                                    		window.location.reload();
				                                    	}
														
                                                    }
				                                }
				                            });
				                        });

				                    }
				                }
				            });
						}else{
							//put the right error-message
							if(agree == 0 && jQuery('#callnumber').val() == ''){
								jQuery(".mini-clickcall .error").html(error_3+'<br \><br \>'+error_4);
							}else{
								if(jQuery('#callnumber').val() == ''){
									jQuery(".mini-clickcall .error").html(error_3);
								}else{
									jQuery(".mini-clickcall .error").html(error_4);
								}
							}
             				
             			}
                   }); 
             }
	    });
	}
});
