var SYS = {};
var FUNCTIONS = {};

$(document).ready(function () {
    SYS.Init();
    $('.campaign .vote').live('click', function (e) {
        e.preventDefault();
        FUNCTIONS.VoteAndShowForm($(this).attr('title'));
    });
    $('.modal').live('click', function (e) {
        e.preventDefault();
        FUNCTIONS.Modal($(this).attr('rel'));
    });

    // replace checkboxes
    $('input').customInput();


    $('.CookieSubmitButton').click(function () {
        if ($('#chbAccept').is(':checked')) {
            // do nothing
        }
        else {
            $('.lblErrorText').show();
            return false;
        }
    });

});

SYS.Init = function(){
	this.winWidth = $(window).width();
	this.winHeight = $(window).height();
	this.docHeight = $(document).height();
	this.docWidth = $(document).width();
	this.scrollTop = $(window).scrollTop();
	$(window).resize(function(){
		SYS.winWidth = $(window).width();
		SYS.winHeight = $(window).height();
		SYS.docHeight = $(document).height();
		SYS.docWidth = $(document).width();
	});
	$(window).scroll(function(){
		SYS.scrollTop = $(window).scrollTop();
	});
};

FUNCTIONS.VoteAndShowForm = function(cookie) {
	var form = $('.campaign .campaignForm');
	$('.choosenCookie').text(cookie);
	$('.hiddenValue').val(cookie);
	$('#hdfCookie input').val(cookie);
	form.show();
	$.scrollTo('.campaign .campaignForm', 500);
};

FUNCTIONS.Modal = function(rel) {
	var contentContainer = $('.'+rel);
	var content = contentContainer.html();
	var campaign = $('.campaign').offset();
	
    if ($('#fade').length == 0) {
    	var fade = $('<div id="fade" />');
        var modal = $('<div id="modal" class="box"><span class="close"></span></div>');
        $('body').append(fade);
        fade.css({
            height: $('.pageWrap').height() + 30 + 'px'
        }).fadeTo(200, 0.3, function () {
        	$(this).after(modal);
        	modal.append(content);
	        modal.css({
				top: SYS.scrollTop + ((SYS.winHeight - modal.height()) /2) + 'px',
	            left: campaign.left + (($('.campaign').width() - modal.width()) /2) + 'px'
	        });
	        modal.fadeIn(200, function () {
	            $('#fade, #modal .close').one('click', function () {
	                $('#modal').fadeOut(200, function () {
	                    $(this).remove();
	                    $('#fade').fadeTo(200, 0, function () {
	                        $(this).remove();
	                    });
	                });
	                return false;
	            });
        	});
        });
    }
    return false;
};
