var hegard = {
	start : function() {
		this.galerySlider();
		this.galeryView();
        this.scrollContent();
        this.formFix();
        this.navigation();
    },

    navigation: function() {
        $(".colapsed").parent().parent().css("display", "block").parent().find('.js').addClass('active');
        $(".js").bind('click', this, function(e) {
            var self = this;
            $(this).not('.active').next().slideDown(300, function() {
	            $(self).addClass('active');
            });
            $('.active').next().slideUp(200, function() {
                $(this).prev().removeClass('active');
            });
            return false;
        });
    },
    formFix: function() {
        $('#form #cv').css({opacity: 0});
        $('.subpage, .subpageIndex').append('<a id="fakeMail" href="mailto:biuro@hegard.pl">biuro@hegard.pl</a>');
        $('.removable').click(function() { $('.removable').attr('value', '').removeClass('removable'); });
        $('#regulation').css('display', 'none').parent().prepend('<div class="checkboxOver"></div>');
        $('.checkboxOver').prepend('<input type="checkbox" checked="checked" name="javascript" style="display: none;" />');
        $(".checkboxOver, label[for='regulation']").click(function(){
            $(".checkboxOver, label[for='regulation']").toggleClass('checkboxOverCheck');
            if ($(this).is('.checkboxOverCheck')) {
                $('#regulation').attr('checked', true);
            } else {
                $('#regulation').removeAttr('checked');
            }
        });
        var $select = $(this);
        var $wrapper = $('.select');
        
        $wrapper.children().hide();
		$wrapper.append('<div class="selectOver"><span>Wybierz z listy</span><ul></ul><a href="#" title="" class="selection hide">rozwiń</a></div>');
		var $ul = $('ul', $wrapper);
        $slider = $('.selectOver ul');
        var $option = $('.select select')
        $('option', $option).each(function(i){
			$ul.append('<li><a href="#" index="'+ this.value +'">'+ this.text +'</a></li>');
		});
        
        var slider = function(e) {
            if ($(e).is(".hide")) {
                $slider.slideDown('slow', function() { $('.selection').removeClass('hide'); });
                $(e).html('zwiń');
                $('.selectOver ul li:last-child').addClass('last-child');
            } else {
                $slider.slideUp('slow', function() { $('.selection').addClass('hide'); });
                $(e).html('rozwiń');
            }
        };

        $('.selection').click(function() { slider(this); });
        $('.selectOver span').click(function() { slider($('.selection')); })
        $('.select li a').click(function(e) {
            var $select = $(this);
            var elText = $select.text();
            var elValue = $select.get(0).rel;
            var elIndex = $select.attr('index');
            $("option[value='"+elIndex+"']").attr('selected', true);
            $('.select li a').removeClass('selected');
            $(this).show('slow').addClass('selected');
            $('.selectOver span').html(elText);
            slider($('.selection'));
        });

        var inputOver = document.createElement('div');
        inputOver.className = 'fileOver';
        inputOver.appendChild(document.createElement('input'));
        var href = document.createElement('div');
        href.className = 'proced';
        href.innerHTML = 'przeglądaj';
        inputOver.appendChild(href);
        var el = document.getElementsByTagName('input');
        for (var i=0;i<el.length;i++) {
            if (el[i].type != 'file') continue;
            if (el[i].parentNode.className != 'file') continue;
            el[i].className = 'file hidden';
            var clone = inputOver.cloneNode(true);
            el[i].parentNode.appendChild(clone);
            el[i].relatedElement = clone.getElementsByTagName('input')[0];
            el[i].onchange = el[i].onmouseout = function () {
                this.relatedElement.value = this.value;
            }
        }
    },
    scrollContent: function() {
            $('.page').jScrollPane({showArrows:true, scrollbarWidth: 25, dragMinHeight: 77, dragMaxHeight: 77, scrollbarMargin: 0, arrowSize: 25});
            $('.jScrollPaneContainer').prepend('<hr class="separator" />');
    },
    galerySliderSize: function(boxWidth, maxSize, minSize, info) {
        if (boxWidth >= maxSize) {
            $('#image-slice-pager .next').addClass('unactive');
        }
        if (boxWidth <= minSize) {
            $('#image-slice-pager .prev').addClass('unactive');
        }
        if (boxWidth < maxSize) {
            $('#image-slice-pager .next').removeClass('unactive');
        }
        if (boxWidth > minSize) {
            $('#image-slice-pager .prev').removeClass('unactive');
        }
    },
	galerySlider: function() {
        if (document.getElementById('image-slice')) {
            var elCount = $('#image-slice li').size();
            
            var elNaturalWidth = 74; /* something wrong with opera */
            var elWidth = elNaturalWidth * 3; /* something wrong with opera */
            var maxSize = elCount * elNaturalWidth;
            var count = 0;
            
            var minSize = 6 * elNaturalWidth;
            var boxWidth = 6 * elNaturalWidth;
            hegard.galerySliderSize(boxWidth, maxSize, minSize, 'default');
            $('#image-slice-pager a').bind('click', function(){
                if ($(this).is('.unactive')) {
                    return false;
                } else {
                    var action = $(this).get(0).className;
                    count = (action == 'prev') ? parseInt(count) + parseInt(elWidth) : parseInt(count) - parseInt(elWidth);
                    boxWidth = (action == 'prev') ? parseInt(boxWidth) - parseInt(elWidth): parseInt(boxWidth) + parseInt(elWidth);
                    hegard.galerySliderSize(boxWidth, maxSize, minSize, 'click');
                    $('#image-slice ul').animate({left: count}, 800);
                };
                return false;
            });
        };
    },
    galeryView: function(){
		$('#image-slice li a').bind('click', function() {
			$('.curent').removeClass('curent').addClass('previous').animate({opacity: 0.5}, 800);
            $(this).addClass('curent').animate({opacity: 0.2}, 800);
			var el = $(this).get(0).rel;
			$('.preview img').animate({marginTop: '286px'}, 600, function() {
				var img = $('.preview').html('').html('<img src="' + el + '" class="hide" />').find('img').bind('load', function() {
					$(".preview .hide").animate({marginTop: '0'}, 600).removeClass('hide');
				});
			});
			return false;
		});
    }
}
function initialize() {
	hegard.start();
}
$().ready(initialize);

