/**
 * jQuery custom file
 *
 * For    : safeski.info
 * Updated: 2009.12.11
 * Author : Dariusz Pobożniak, http://pobozniak.pl
 *
 */

$(function() {	
	createList();
	$('#newsletter #femail').focus(function() { $(this).select(); })
	contestTabs();
	makePagination();
	equalHeight();
	showLinksKinds();
	$('.date').datepicker({ dateFormat: 'yy-mm-dd' });
	$('.fancybox').fancybox();
});

function showContestArea(selector) {
	$('#contest-data > div').hide();
	$('#contest-data > form').hide();
	$('#contest-data #'+selector).show();
}

function showLinksKinds() {
	$('.pdf,.doc,.txt').addClass('document').append(' dokument');
	$('.img,.jpg,.jpeg,.gif,.png').addClass('image').append(' obraz');
	$('.rar,.zip').addClass('pack').append(' spakowany plik');
}

function contestTabs() {
	if ($('#contest-data').length > 0) {
		var first_element = $('#contest-menu li:eq(1)');
		showContestArea($(first_element).attr('class'));
		$(first_element).addClass('active');
		$('#contest-menu li a').click(function() {
			$('li.active').removeClass('active');
			showContestArea($(this).parent().attr('class'));
			$(this).parent().addClass('active');
			equalHeight();
			return false;
		});
		contestSteps();
	}
}

function contestSteps() {
    $('.btn-next').addClass('inactive').attr('disabled', 'disabled');
    $('#sendform').removeClass('inactive').removeAttr('disabled');
    $('#contest-form input[type=checkbox]').attr('checked', false).click(function() {
    	var $div = $(this).parent().parent();
        if ($(this).attr('checked')) {
            $div.find('.btn-next').removeClass('inactive').removeAttr('disabled');
        } else {
            $div.find('.btn-next').addClass('inactive').attr('disabled', 'disabled');
        }
    });

    $('#contest-form > div').not('div:eq(0)').hide();
    $('.btn-prev').click(function() {
        $(this).parent().parent().hide().prev().show();
        setDot($(this).parent().parent().prev().attr('id'));
        equalHeight();
        return false;
    })
    $('.btn-next').click(function() {
    	var $div = $(this).parent().parent();
    	var isError = false;
    	$div.find('input[type=hidden]').each(function() {
    		$inhid = $(this);
			if ($inhid.val().substr(-1) == '*') {
				$div.find('input[type=text]').each(function() {
					$intext = $(this);
					if ($inhid.attr('name').substr(1,99) == $intext.attr('name').substr(1,99)) {
						if ($intext.val() == '') {
							$intext.parent().addClass('error');
							isError = true;
						} else {
							$intext.parent().removeClass('error');
						}
					}
				})
				$div.find('textarea').each(function() {
					$intext = $(this);
					if ($inhid.attr('name').substr(1,99) == $intext.attr('name').substr(1,99)) {
						if ($intext.val() == '') {
							$intext.parent().addClass('error');
							isError = true;
						} else {
							$intext.parent().removeClass('error');
						}
					}
				})
			}
		})
		// if button is last and no error, send form
		if ($(this).attr('id') == 'sendform') {
			if (isError !== false) {
				return false;
			}
		} else { // go to next step
			if (isError === false) {
				$div.hide().next().show();
				setDot($div.next().attr('id'));
				equalHeight();
			}        
			return false;
        }
    })
}

function setDot(id) {
    $('#step-path span').removeClass().addClass(id);
}

function equalHeight() {
    $('.column').css({'height':''});
	$('.column').setEqualHeight();
}

jQuery.fn.setEqualHeight=function(o) {
    var maxHeight=0;
    var maxElement=null;
    	jQuery(this).each(function(i) {
      		if((jQuery(this).height()+parseInt(jQuery(this).css("padding-bottom"))+parseInt(jQuery(this).css("padding-top")))>maxHeight) {
    		maxHeight=jQuery(this).height()+parseInt(jQuery(this).css("padding-top"))+parseInt(jQuery(this).css("padding-bottom"));
    		maxElement=this;
    	}
    });
    //jQuery(this).not($(maxElement)).each(function() {$(this).height(maxHeight-parseInt(jQuery(this).css("padding-top"))-parseInt(jQuery(this).css("padding-bottom")))})
    jQuery(this).each(function() {$(this).height(maxHeight-parseInt(jQuery(this).css("padding-top"))-parseInt(jQuery(this).css("padding-bottom")))})
}

/*function equalHeight(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = $(this).outherHeight(true);
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}*/

function createList() {
    createDropDown();
            
    $(".dropdown dt a").click(function() {
        $(".dropdown dd ul").toggle();
        return false;
    });

    $(document).bind('click', function(e) {
        var $clicked = $(e.target);
        if (! $clicked.parents().hasClass("dropdown"))
            $(".dropdown dd ul").hide();
    });
                
    $(".dropdown dd ul li a").click(function() {
        var text = $(this).html();
        $(".dropdown dt a").html(text);
        $(".dropdown dd ul").hide();
        
        var source = $("#triplist");
        var tojest = $(this).find("span.value").html();
        source.val(tojest);
        window.location.href = tojest;
        return false;
    });

}

function createDropDown(){
    var source = $("#triplist");
    var selected = source.find("option[selected]");
    var options = $("option", source);
    
    if ($('#triplist').length) {
        $("#tripbox .b-in, #tour .box-1 .b-in").append('<dl id="target" class="dropdown"></dl>')
        $("#target").append('<dt><a href="#">' + selected.text() + 
            ' <span class="value">' + selected.val() + 
            '</span></a></dt>')
        $("#target").append('<dd><ul></ul></dd>')
    
        options.each(function(){
            $("#target dd ul").append('<li><a href="#">' + 
                $(this).text() + ' <span class="value">' + 
                $(this).val() + '</span></a></li>');
        });
        source.remove();
    }
}

function makePagination() {
	if ($('#news-list').length || $('#eventbox').length) {
		$("#news-list dl").quickpaginate({ perpage: 4, showcounter: true, pager : $("#news-counter") });
		$("#eventbox dl").quickpaginate({ perpage: 1, showcounter: true, pager : $("#event-counter") });
	}

}


