﻿//PLIK: jquery-functions.js
//OPIS: Instrukcje jQuery

var scroller  = null;
var scrollbar = null;
window.onload = function () {
    var node = document.getElementById("Scroller-1");
    if(node !== null) {
        scroller  = new jsScroller(document.getElementById("Scroller-1"), 400, 200);
        scrollbar = new jsScrollbar (document.getElementById("Scrollbar-Container"), scroller, false);
    }
};

function parseComaFloat(data) {
    data = data.replace(',', '.');
    return parseFloat(data);
}

Number.prototype.formatMoney = function() {
	var num = this;
	var str = num.toFixed(2).toString();
	return str.replace('.', ',');
};

$(document).ready(function()
{
    $('input[type=file]').css('opacity', 0.0);
     
    $('input[type=file]').change(function(){
        var $el = $('input[type=file]');
          
        $('#filedisplay').val( $el.val() );
    });

    $('input[type=file].zamowienie-fileinput').change(function(){
        $('div.fileinput-wrapper').css('display', 'none');
        $('div.loader-1').css('display', 'block');
          
        setTimeout(function() {
            $('a.dodaj-plik').css('display', 'block');
            $('div.loader-1').css('display', 'none');
            alert('ajax / reload');
        }, 3000);
    });

    $('input[type=file]').hover(
        function () {
            $('#filedisplay').css('opacity', 0.0);
            $('#fileinput-wrapper').css('background', 'url(/img/app/fieldinput.png) 0 0');
            $('.fileinput-wrapper').css('background', 'url(/img/app/fieldinput.png) 0 0');
        }, 
        function () {
            $('#filedisplay').css('opacity', 1.0);
            $('#fileinput-wrapper').css('background', 'url(/img/app/fieldinput.png) 0 0');
            $('.fileinput-wrapper').css('background', 'url(/img/app/fieldinput.png) 0 0');
        }
    );

    $('select').css('opacity', 0.0).change(function(e) {
          var node = $(e.currentTarget);
          var selectedOption = $(node.attr('options')[node.attr('selectedIndex')]);
          node.next().val(selectedOption.html());
    });

    $("input[type=checkbox]").css('opacity', 0.0);

    $("input[type=checkbox][checked]").each(function() { 
        $(this).parent("div").css("background-position", "0 -32px");
    });

    $(".checkbox").mouseover(function() {
        $(this).parent("div").css("background-position", "0 -32px");
    });

    $(".checkbox").mouseout(function() {
        if($(this).is(':checked')) {
            $(this).parent("div").css("background-position", "0 -32px");
        } else {
            $(this).parent("div").css("background-position", "0 0");
        }
    });

    $(".checkbox").click(function() {
        if($(this).is(':checked')) {
            $(this).parent("div").css("background-position", "0 0");
            $("p.output").html("checked");
        } else {
            $(this).parent("div").css("background-position", "0 -32px");
            $("p.output").html("not checked");
        }
    });

    $(".checkbox").click(function(){
        if($(this).is('#status-0')) {
            //alert('1');
          
            if( $(this).attr('checked') ) {
                //alert('1a');
                $("#status-1").parent("div").css("background-position", "0 0");
                $("#status-1").attr('checked', false);     
            } else {
                //alert('1b');           
                $("#status-1").parent("div").css("background-position", "0 -32px");
                $("#status-1").attr('checked', true);
            }
        }

        if($(this).is('#status-1')){
            //alert('2');
          
            if( $(this).attr('checked') ){
                //alert('2a');
                $("#status-0").parent("div").css("background-position", "0 0");
                $("#status-0").attr('checked', false);   
            } else {
                //alert('2b');
                $("#status-0").parent("div").css("background-position", "0 -32px");
                $("#status-0").attr('checked', true);
            }
        }
    });


    $(".checkbox").click(function() {
        if($(this).is('#status-2')) {
            //alert('1');
          
            if( $(this).attr('checked') ) {
                //alert('1a');
                $("#status-3").parent("div").css("background-position", "0 0");
                $("#status-3").attr('checked', false);  
            } else {
                //alert('1b');           
                $("#status-3").parent("div").css("background-position", "0 -32px");
                $("#status-3").attr('checked', true);
            }
        }

        if($(this).is('#status-3')) {
            //alert('2');
          
            if( $(this).attr('checked') ) {
                //alert('2a');
                $("#status-2").parent("div").css("background-position", "0 0");
                $("#status-2").attr('checked', false);    
            } else {
                //alert('2b');
                $("#status-2").parent("div").css("background-position", "0 -32px");
                $("#status-2").attr('checked', true);
            }
        }
    });
    
    $(".checkbox").click(function() {
        if($(this).is('#status-4')) {
            //alert('1');
          
            if( $(this).attr('checked') ) {
                //alert('1a');
                $("#status-5").parent("div").css("background-position", "0 0");
                $("#status-5").attr('checked', false);  
            } else {
                //alert('1b');           
                $("#status-5").parent("div").css("background-position", "0 -32px");
                $("#status-5").attr('checked', true);
            }
        }

        if($(this).is('#status-5')) {
            //alert('2');
          
            if( $(this).attr('checked') ) {
                //alert('2a');
                $("#status-4").parent("div").css("background-position", "0 0");
                $("#status-4").attr('checked', false);    
            } else {
                //alert('2b');
                $("#status-4").parent("div").css("background-position", "0 -32px");
                $("#status-4").attr('checked', true);
            }
        }
    });

    $("#polityka").css('opacity', '1.0');
    $("#zgadzam-sie").css('opacity', '1.0');
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
////////// PRODUCTS ///////////////////////////////////////////////////////////////////////////////////////////////////////    
    $('a.addToCart').click(function(e) {
		var url = $(this).attr('href');
		$.get(url+'.json', function(data, textStatus, req) {
			if(data.status == 0) alert($('body').children('#cartadd_message_0').html());
            else if(data.status == 1) alert($('body').children('#cartadd_message_1').html());
            else alert($('body').children('#cartadd_message_2').html());
		},'json');
		return false;
    });

////////// CART ////////////////////////////////////////////////////////////////////////////////////////////////////////////
    function calculateProductsPrice() {
        var container = $('#cartProductsList');
        var output = $('#podsumowanie');
        var totalPriceEUR = 0.0;
        var totalPricePL = 0.0;

        container.find('.cartProduct').each(function(index, o) {
            var quantity = parseInt($(o).find('.productQuantity').val());
                
            totalPriceEUR += quantity * parseComaFloat($(o).find('.productPriceEUR').html());
            totalPricePL += quantity * parseComaFloat($(o).find('.productPricePL').html());
        });

        var productsSummarize = output.find('.productsSummarize');
       
        productsSummarize.find('.productsTotalPriceEUR').html(totalPriceEUR.formatMoney());
        productsSummarize.find('.productsTotalPricePL').html((totalPricePL).formatMoney());
    }

   
    $('input.productQuantity').change(function(e) {
        var form = $($(this).attr('form'));
        var url = form.attr('action') + '.json';
        $.post(url, form.serialize(), function(data, textStatus, req) {
            if(data.status == 0) {
                calculateProductsPrice();
            } else {
                alert($('body').children('#cartup_message_1').html());
            }
        });
    }); 

    $('a.removeFromCart').click(function(e) {
		var url = $(this).attr('href');
		$.get(url+'.json', function(data, textStatus, req) {
			if(data.status == 0) {
                $(e.target).parent().parent().remove();
                calculateProductsPrice();
            } else alert($('body').children('#cartrem_message_1').html());
		},'json');
		return false;
    });

///////// NEWSLETTER ///////////////////////////////////////////////////////////////////////////////////////////////////////

    $('#newsletterSubscriberForm').submit(function(e) {
        var form = $(this);
        $.post($(this).attr('action') + '.json', $(this).serialize(), function(data, textStatus, req) {  
            if(data.status == 0) {
                var message = form.nextAll('#newsletter-message-0').html();
                alert(message);
            } else {
                if(data.status == 1) {
                    var message = form.nextAll('#newsletter-message-1').html();
                    alert(message);
                } else if(data.status == 2) {
                    var message = form.nextAll('#newsletter-message-2').html();
                    alert('Zaakceptuj politykę prywatności.');
                } else {
                    var message = form.nextAll('#newsletter-message-3').html();
                    alert(message);
                }
            }
        });
        return false;
    });

    $('.showLightbox').click(function(e) {
        var productMedia = $(this).parent().parent().find('.lightbox');
        productMedia.lightBox();
        productMedia.first().trigger('click');
        return false;
    });

    //---
});



