Europolgaz = {};

function fadeInAnimButton() {
    $('div.page-animation-play-act').fadeIn(500, function(){
        $('div.page-animation-play-act').fadeOut(500, function(){
            fadeInAnimButton();
        });
    });
}
$(document).ready(function() {

    gasPricesInitialize('pipie_in_numbers', 'p', 5000);
    gasPricesInitialize('press_stations', 'a', 3000);
    Europolgaz.subscription.initialize();
    Europolgaz.maps.initialize();
	Europolgaz.mapAnimationSmall.initialize();
    new Europolgaz.mapAnimation();
    
    fadeInAnimButton();
    
    $('div.page-animation-play').click(function(){
        $('div.animation-wraper').show();
        var anim = new Animation(animationConfig);
    });
    
  //PAGE MENU

  $('div.first-level-item').mouseenter(function() {
    $('div.second-level-menu').hide();
    var _subitem = $(this).next('div.second-level-menu');
    if (_subitem.length>0) {
      var _offset = $(this.children[0]).width();
      var _menuOffset = _offset + 30; //30px is space for elem padding (20px) and additional offset (10px)
      var _maskWidth = 142 - _menuOffset;
      
      var _maskTop = _subitem[0].children[0];
          _maskTop.style.width = _maskWidth + 'px';
      
      var _maskBottom = _subitem[0].children[1];
          _maskBottom.style.width = _maskWidth + 'px';
      
      var _menu = $(_subitem[0]);
          _menu[0].style.right = -_menuOffset + 'px';
          _menu.show();
    }
  });
  
  $('div.second-level-menu').mouseleave(function() {
    $(this).hide();
  });
  
  $('ul.page-menu').mouseleave(function() {
    $('div.second-level-menu').hide();
  });


//SEARCH FIELD
Europolgaz.search = {
    SearchField: {
        isEmpty: true,
        emptyValue: search_on_page      
    },
    SearchString: function(string) {
        if(string.length < 3) {
            return false;
        }
        var _url = $('#search-form').attr('action');
        if(_url.length > 0) {
            document.location.href = _url + string + ".html";
        }
        return false;
    }
};

$('#search-field').val(Europolgaz.search.SearchField.emptyValue);

$('#search-field').keyup(function() {
    var _value = $('#search-field').val();
    Europolgaz.search.SearchField.isEmpty = (_value.length>0) ? false : true;
});
  
  $('#search-field').keydown(function(event) {
    if(event.keyCode==13 && !Europolgaz.search.SearchField.isEmpty) {
      var _valueToSearch = $('#search-field').val();
      Europolgaz.search.SearchString(_valueToSearch);
      event.stopPropagation();
      return false;
    }
  });

  $('#search-field').focusin(function() {
    if (Europolgaz.search.SearchField.isEmpty) $('#search-field').val('');
  });
  
  $('#search-field').focusout(function() {
    if (Europolgaz.search.SearchField.isEmpty) $('#search-field').val(Europolgaz.search.SearchField.emptyValue);
  });
  
     
  //SEARCH BUTTON
  
  $('#search-button').click(function() {
    if (!Europolgaz.search.SearchField.isEmpty) {
      var _valueToSearch = $('#search-field').val();
      Europolgaz.search.SearchString(_valueToSearch);
    }
  });    
  
  
});
/*************************************************************************************************/
function gasPricesInitialize(id, tag, delay) {
    var elems = $('#' + id + ' ' + tag);
    if(elems.length > 1) {        
        elems.hide();
        $('#' + id + ' ' + tag + ':first').show();
        window.setTimeout(function() {gasPricesSwitch(id, tag);}, delay);
    }
    $('#' + id).show();
}
function gasPricesSwitch(id, tag) {
    var elems = $('#' + id + ' ' + tag);
    var count = elems.length;
    var idx = $('#' + id + ' ' + tag + ':visible').index();
    idx++;
    if(idx >= count) {
        idx = 0
    }
    if(jQuery.browser === 'msie') {
        $('#' + id + ' ' + tag + ':visible b').css('font-weight', 'normal');
    }
    $('#' + id + ' ' + tag + ':visible').fadeOut(1000, "linear", function() {
        if(jQuery.browser === 'msie') {
            $('#' + id + ' ' + tag + ':eq(' + idx + ') b').css('font-weight', 'normal');
        }
        $('#' + id + ' ' + tag + ':eq(' + idx + ')').fadeIn(1000, "linear", function() {
            if(jQuery.browser === 'msie') {
                $('#' + id + ' ' + tag + ':eq(' + idx + ') b').css('font-weight', 'bold');
            }
            window.setTimeout(function() {
                gasPricesSwitch(id, tag);
            }, 5000);
        });
    });
}
/*************************************************************************************************/
Europolgaz.subscription = {
    initialize: function() {
        $('#subscription_email').focus(this.emailFieldFocus);
        $('#subscription_email').blur(this.emailFieldBlur);
    },
    emailFieldFocus: function() {
        if($('#subscription_email').val() == 'wpisz adres email') {
            $('#subscription_email').val('');
        }
    },
    emailFieldBlur: function() {
        if($('#subscription_email').val() == '') {
            $('#subscription_email').val('wpisz adres email');
        }
    },
    popupShow: function() {
        if($('div.subscription-popup:hidden').length > 0) {
            $('div.subscription-popup').fadeIn('slow');
        }
    },
    popupHide: function() {
        if($('div.subscription-popup:visible').length > 0) {
            $('div.subscription-popup').fadeOut('slow');
        }
    },
    popupSubmit: function() {
        var email = $('#subscription_email').val();
        if(email.length == 0){
            alert('Prosze podac adres email');
            $('#subscription_email').focus();
            return;
        }
        if(!this.validateEmail(email)){
            alert('Podany adres email jest nieprawidlowy');
            $('#subscription_email').focus();
            return;
        }
        if($('div.subscription-popup-checkbox-wrap input[type="checkbox"]:checked').length <= 0) {
            alert('Prosze zaznaczyc przynajmniej jedna z opcji');
            return;
        }
        $('#subscription_form').submit();
        this.popupHide();
    },
    validateEmail: function(email) 
    {
        var emailValidation = /^([\w\-]+)(\.[\w\-]+)*@([\w\-]+\.){1,5}([A-Za-z]){2,4}$/;
        return emailValidation.test(email);
    }    
}
/*************************************************************************************************/
Europolgaz.maps = {
    initialize: function() {
        $("div.map-canvas").each(function(index) {
            var params = jQuery.parseJSON($(this).html());
            $(this).html("");
            var latlng = new google.maps.LatLng(params.lat, params.lng);
            var myOptions = {
                zoom: 12,
                center: latlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            var map = new google.maps.Map($(this).get(0), myOptions);
            var marker = new google.maps.Marker({
                position: latlng, 
                map: map, 
                title: params.title
            });              
        });
    }
};
/*************************************************************************************************/
Europolgaz.mapAnimationSmall = {
	frame: 1,
	time: 1000,
    initialize: function() {
		if($("div.top-menu-map div.top-menu-map-animation").length == 0) {
			return false;
		}
		window.setTimeout(Europolgaz.mapAnimationSmall.nextFrame, Europolgaz.mapAnimationSmall.time);
	},
	nextFrame: function() {
		if(Europolgaz.mapAnimationSmall.frame > 1) {
		    $("div.top-menu-map div.f" + Europolgaz.mapAnimationSmall.frame).fadeOut(Europolgaz.mapAnimationSmall.time, function(){Europolgaz.mapAnimationSmall.nextFrame();});
		}
		Europolgaz.mapAnimationSmall.frame++;
		if(Europolgaz.mapAnimationSmall.frame > 4)
			Europolgaz.mapAnimationSmall.frame = 1;
		if(Europolgaz.mapAnimationSmall.frame <= 4) {
			if(Europolgaz.mapAnimationSmall.frame==2) {
			    $("div.top-menu-map div.f" + (Europolgaz.mapAnimationSmall.frame)).fadeIn(Europolgaz.mapAnimationSmall.time, function(){Europolgaz.mapAnimationSmall.nextFrame();});
			} else {
			    $("div.top-menu-map div.f" + (Europolgaz.mapAnimationSmall.frame)).fadeIn(Europolgaz.mapAnimationSmall.time);
			}
		}
	}
}

Function.prototype.createDelegate = function(obj, args, appendArgs){
        var method = this;
        return function() {
            var callArgs = args || arguments;
            if (appendArgs === true){
                callArgs = Array.prototype.slice.call(arguments, 0);
                callArgs = callArgs.concat(args);
            }else if (typeof v === 'number' && isFinite(v)){
                callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
                var applyArgs = [appendArgs, 0].concat(args); // create method call params
                Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
            }
            return method.apply(obj || window, callArgs);
        };
    };
