/*
#---------------------------------------------------------------------------
# Copyright   : rexx systems GmbH, 20097 Hamburg, Heidenkampsweg 101
#               Veränderung oder Nutzung der Scripte/Anwendung ist nur mit
#               schriftlicher Genehmigung der rexx systems GmbH gestattet.
#---------------------------------------------------------------------------
*/


/**
* wegen Konflikten mit anderen js-libaries
* @todo wo könnten diese Zeilen zu problemen führen?
*/
$ = jQuery.noConflict();
j$ = $;

shopArticles = (function() {

    return {

        /*
         * Intialisierung von Event-Handlern, etc.
         * wird am Ende dieser Datei per $(document).ready() aufgerufen
         */
        init: function() {
            $("img[id^='groessentabelle_']").click(shopArticles.showSizetable);

            /*
             * Bei aktiven Staffelpreisen wird hier
             * der Preis nach Staffelpreis angepasst.
             */
            if($("tr[id^='staffel_']").html() != null){
               original_preis = $(".original_preis").html();
               $("input[id^='quantity_']").keyup(shopArticles.updatePrice);
            }
//            j$('.jqmWindow').jqm({overlay: 0}).jqDrag('.jqmDrag');
            $('#rexxJqmClose').click( function (){j$('.jqmWindow').jqmHide();} );
            $('img.add_article_to_basket').click(function() {
                var detail_id = $(this).attr('id');
                shopArticles.addArticleToBasket(detail_id,$(this));
            });
        },

        showSizetable: function() {

        	if(typeof(cust_setSizeTableCSSWidth) == "function"){
        		var width = cust_setSizeTableCSSWidth;
        		$('.jqmWindow').css('width', width);
        	} else {
        		$('.jqmWindow').css('width', '350px');
        	}

        	var catid = this.id.replace(/groessentabelle_/, "");

        	$.ajax({
                    type: "GET",
                    url: "../inc/functions/xml_http_requests.php",
                    data: {
                        f : "get_sizetable",
                        p0: catid,
                        sid: mySession
                    },
                    dataType: 'json',

                    success: function(phpData) {

                        if($.browser.version < 7 && $.browser.msie){
                            $('.jqmWindow').css('width', 'auto');
                            $('#sizeTableLayer').css("display", "block");
                            $('#sizeTableContent').html(phpData.content);
                            $('#SizemodalClose').click(function (){$('#sizeTableLayer').css("display", "none");});
                        } else {

                            shopBasicJs.showContentLayer(phpData.content, phpData.title);

//                	   $('.jqmWindowContent').html(phpData.content);
//                           $('.jqmWindowHead').show().html(phpData.title);
//                	   $('.jqmWindow').jqm({overlay: 1}).jqmShow();
//                   	   $('.jqmWindow').css('opacity', 1);
//                   	   $('.jqmOverlay').css('opacity', 0.3).css('position', 'fixed');
//                	   $('#rexxJqmClose').show();
//                           $('#rexxJqmClose').click(function() { $('.jqmWindow').jqmHide(); j$('.jqmWindowHead').hide(); j$('#rexxJqmClose').hide(); $('.jqmWindow').css('width', 'auto'); } );
                	}
                    }
    		});
        },

        closeSizeTableLayer: function(){
           $('#sizeTableLayer').css("display", "none");
        },

        /**
         * Bei Aenderung der Menge wird
         * der Preis nach Staffelpreisen angepasst
         * falls diese vorhanden sind
        **/
        updatePrice: function(){

            var detail_id = this.id.replace(/quantity_/, "");
            var menge     = $("#quantity_" + detail_id).val();

        	$.ajax({
    	        type: "GET",
    			url: "../inc/functions/xml_http_requests.php",
    			data: {
                    f : "update_price",
                    p0: menge,
					p1: detail_id,
                    sid: mySession
                },

                success: function(phpData) {

                   if(phpData != ''){
                      $(".preis_" + detail_id).html(phpData);
                   } else {
                      $(".preis_" + detail_id).html(original_preis);
                   }

                }
    		});
        },

        showTitleLayer: function(user, token, aid, lang) {

        	$.ajax({
    	        type: "GET",
    			url: "../inc/functions/xml_http_requests.php",
    			data: {
                    f  : 'get_article_titles_form',
                    p0 : user,
                    p1 : token,
                    p2 : aid,
                    p3 : lang
                },

                success: function(response) {
                    shopArticles.showLayer(response);
                }
    		});

        },

        showStocksLayer: function(user, token, aid) {

        	$.ajax({
    	        type: "GET",
    			url: "../inc/functions/xml_http_requests.php",
    			data: {
                    f  : 'get_article_stocks',
                    p0 : user,
                    p1 : token,
                    p2 : aid
                },

                success: function(response) {
                    shopArticles.showLayer(response);
                }
    		});

        },

        saveTitles: function(user, token, aid) {

            data = new Object();

            $('form#titles_form input.article_title').each(function() {
                data[$(this).attr('name')] = new Object();
                data[$(this).attr('name')]['title'] = $(this).val();
            });
            $('form#titles_form textarea.article_beschreibung').each(function() {
                data[$(this).attr('name')]['beschreibung'] = $(this).val();
            });
            $('form#titles_form textarea.article_beschreibung2').each(function() {
                data[$(this).attr('name')]['beschreibung2'] = $(this).val();
            });
            $.each(data, function(lang, values) {
                shopArticles.saveTitle(user, token, aid, lang, values.title, values.beschreibung, values.beschreibung2);
            });

        },

        saveTitle: function(user, token, aid, lang, title, beschreibung, beschreibung2) {

        	$.ajax({
    	        type: "POST",
    			url: "../inc/functions/xml_http_requests.php",
    			data: {
                    f  : 'save_article_title',
                    p0 : user,
                    p1 : token,
                    p2 : aid,
                    p3 : lang,
                    p4 : title,
                    p5 : beschreibung,
                    p6 : beschreibung2

                },

                success: function(response) {
                    if(response=='nok') {
                        alert('Fehler!');
                    } else {
                        if(myLanguage==lang) {
                            $('#artikel_title').html(title);
                            $('#artikel_beschreibung').html(beschreibung);
                            $('#artikel_beschreibung2').html(beschreibung2);
                        }
                        j$('.jqmWindow').jqmHide();
                    }
                }
    		});

        },

        showLayer: function(content) {
            $('#rexxJqmClose').show();
            j$('.jqmWindow').jqmShow();
            $('.jqmWindowContent').html('<div style="max-height: 500px;overflow-y: scroll;overflow-x: hidden;">'+content+'</div>');
        },

        switchLanguage: function() {
            $('form#titles_form div.lang').each(function() {
                $(this).toggle();
            });
        },

        logout: function() {
        	$.ajax({
    	        type: "GET",
    			url: "../inc/functions/xml_http_requests.php",
    			data: {
                    f   : 'logout',
                    sid : mySession
                },

                success: function() {
                    $('#ERPFunctions').animate({
                        height: 'toggle'
                    });
                }
    		});
        },

        addArticleToBasket: function(detail_id,$this) {
            var quantity = $('#quantity_'+detail_id).val();
            if($this.length > 0 && $this.attr('attrib') && $this.attr('attrib').length > 0 ) {
                var attribut_id = $this.attr('attrib');
                if($('#select_'+attribut_id) && $('#select_'+attribut_id).length > 0 && $('#select_'+attribut_id).is('select')) {
                    detail_id = $('#select_'+attribut_id).val();
                }
            }
            $.ajax({
    	        type: "POST",
                dataType: 'json',
    			url: "../inc/functions/xml_http_requests.php",
    			data: {
                    f   : 'add_article_to_basket',
                    p0  : detail_id,
                    p1  : quantity,
                    sid : mySession
                },
                success: function(response) {
                    shopArticles.showConfirmWindow(response.content);
                    shopArticles.updateBasketCount(response.basket_count);
                }
            });
        },

        showConfirmWindow: function(content) {
            j$('.jqmWindow').jqmShow();
            $('.jqmWindowContent').html('<div>'+content+'</div>');
            $('#continue').click(function(e) {
                e.preventDefault();
                shopArticles.hideConfirmWindow();
            });
        },

        hideConfirmWindow: function() {
            j$('.jqmWindow').jqmHide();
        },

        updateBasketCount: function(basket_count) {
            $('#basket_img').css('background-image', 'url(/images/warenkorb-active.gif)');
            $('#basket_count').css('color', '#0c0');
            $('#basket_count').html(basket_count);
        },

        /**
         * bei Klick auf 'Einkauf fortsetzen' den AddToBasket-Layer verstecken
         */
        hideAddToBasket: function() {
            $('#confirmWindow').hide();
        }
    };
})();

/*
 * Initialisierung, wenn Dokument geladen
 */
$(document).ready(function(){
	shopArticles.init();
});
