﻿var newsletters = function() { 
	return {
		bindUi : function() {
		    $('a.newsletterSubmit').click(
		            function() {
		                  $('.newsletterForm').submit();
		            }
		        );
		
		    $('.newsletter > a').click(
		            function() {
		                $(this).fadeOut('fast', function() {
		                    $('.newsletterForm').fadeIn('fast');
		                });
		            }
		         );
		    $('.newsletterCancel').click(
		            function() {
		                $('.newsletterForm').fadeOut('fast', function() {
		                    $('.newsletter > a').fadeIn('fast');
		                });
		            }
		         );
		
		    $('.newsletterForm #email').click(
		        function() {
		            if ($(this).attr('value') == 'adres e-mail')
		                $(this).attr('value', '');
		        }
		        );


		}
	}
	
} ();


$(document).ready(function() {


    newsletters.bindUi();

    /* login widgets */

    $('.loginInPlace').click(
		        function() {
		            $('#menuFast ul').fadeOut('fast', function() {
		                $('form.account').fadeIn('fast', function() {
		                    $('form.account #userName').focus();
		                });
		            }
		            );
		        }
		        );

    $('form.account #loginButton').click(function() {
        $(this).parent().submit();
        return false;
    }
			);

    $('form.account').ajaxForm({
        // dataType identifies the expected content type of the server response 
        dataType: 'json',

        // success identifies the function to invoke when the server response 
        // has been received
        success: function(data) {

            if (!data.IsValid) {
                ui.flashMsg(data);
            }
            else {
                $('#menuFast').load('/controls/profile/header');
                $('#menuMain').load('/controls/menu/main', function() {
                    newsletters.bindUi();
                });
            }
        }
    }
		          );

    $('.loginCancel').click(
		                function() {
		                    var parent = $('#menuFast ul');
		                    $('form.account').fadeOut('fast', function() {
		                        parent.fadeIn('fast');
		                    }
		            		);
		                }
		        	);



    $('#menuFast form #userName').focus(
        function() {
            if ($(this).attr('value') == 'login')
                $(this).attr('value', '');
        }
        ).bind("keypress", function(e) {
            if (e.keyCode == 13) {
                $('#loginButton').click();
            }
        }
           );

    $('#menuFast form #password').focus(
        function() {
            if ($(this).attr('value') == 'hasło')
                $(this).attr('value', '');
        }
        ).bind("keypress", function(e) {
            if (e.keyCode == 13) {
                $('#loginButton').click();
            }
        }
           );

    /** Add to favorites buttons */
    $('.addToFavorites a').click(function() {

        $.post($(this).attr('href'), {}, function(data) {
            if (data.IsValid)
                data.Message = "Wybrane medium zostało dodane do ulubionych";
            else
                data.Message = "Wybrane medium znajduje się już na liście ulubionych";

            ui.flashMsg(data);
        }, "json");
        return false;
    });
    return false;
});


    /** Add user as fan to user group */
    $('#boxSeriesFans .join a').click(function() {
        $.post($(this).attr('href'), {}, function(data) {
            if (data.IsValid)
                data.Message = "Zostałeś zapisany jako fan serialu!";
            else
                data.Message = "Jesteś już fanem tego serialu.";

            ui.flashMsg(data);
        }, "json");
}
);
