jQuery.root = $(document);

function login()
{
	$('button').attr("disabled", "disabled");

    $('#formLogin').ajaxSubmit({
        dataType: 'xml',
        success:function(xmlResponse) {
            response = $('response', $(xmlResponse)).eq(0);
            code = response.find('code').text();
            msg = response.find('msg').text();
            url = response.find('url').text();

            switch(code)
            {
                case '1':
                    if (url != null)
                        window.location = url;
                    else
                        window.location = "/";
                    break;
                case '-2':
                case '-1':
                case '-9':
                    // Destaco los campos erroneos

                    alert(msg);

                    break;
                default:
                    //showFaceboxError(msg);
                    //break;
            }
        }
    });

    $('button').attr("disabled", "");
}


function logout()
{
    if (confirm("Are you sure you want logout?"))
    {
        $.ajax({
            url: "do/logout.php",
            dataType: "xml",
            success: function(xml) {
                window.location.reload();
            }
        });
    }
}


function gotojoin(edition)
{
    window.location.href="subscriptors/join.php?edition=" +edition;
}


function join()
{
    $('button').attr("disabled", "disabled");

    $("label").removeClass("error");

    $('#form1').ajaxSubmit({
        dataType: 'xml',
        success:function(xmlResponse) {
            response = $('response', $(xmlResponse)).eq(0);
            code = response.find('code').text();
            msg = response.find('msg').text();
            url = response.find('url').text();

            switch(code)
            {
                case '1':
                    // Usuario registrado, le informamos del envío de su email de bienvenida y continuamos con la
                    // subscripcion.
                    if (url != null)
                        window.location = url;
                    else
                        window.location = "/";
                    break;
                    break;
                case '-1':
                case '-2':
                case '-3':
                case '-9':
                    // Destaco los campos erroneos
                    errors = response.find('errors').eq(0);
                    $("error", errors).each(function() {
                        $('label[for=' +$(this).attr("field")+ ']').addClass("error");
                    });

                    alert(msg);

                    break;
                default:
                    //showFaceboxError(msg);
                    //break;
            }
        }
    });

    $('button').attr("disabled", "");
}

function active()
{
    $('button').attr("disabled", "disabled");

    $("label").removeClass("error");

    $('#form1').ajaxSubmit({
        dataType: 'xml',
        success:function(xmlResponse) {
            response = $('response', $(xmlResponse)).eq(0);
            code = response.find('code').text();
            msg = response.find('msg').text();

            switch(code)
            {
                case '1':
                    window.location.reload();
                    break;
                case '-1':
                case '-9':
                    // Destaco los campos erroneos
                    errors = response.find('errors').eq(0);
                    $("error", errors).each(function() {
                        $('label[for=' +$(this).attr("field")+ ']').addClass("error");
                    });

                    alert(msg);

                    break;
                default:
                    //showFaceboxError(msg);
                    //break;
            }
        }
    });

    $('button').attr("disabled", "");
}

function forgotpassword()
{
    $('button').attr("disabled", "disabled");

    $("label").removeClass("error");

    $('#form1').ajaxSubmit({
        dataType: 'xml',
        success:function(xmlResponse) {
            response = $('response', $(xmlResponse)).eq(0);
            code = response.find('code').text();
            msg = response.find('msg').text();
            url = response.find('url').text();

            switch(code)
            {
                case '1':
                     if (url != null)
                        window.location = url;
                    else
                        window.location = "/";
                    break;
                case '-1':
                case '-9':
                    // Destaco los campos erroneos
                    errors = response.find('errors').eq(0);
                    $("error", errors).each(function() {
                        $('label[for=' +$(this).attr("field")+ ']').addClass("error");
                    });

                    alert(msg);

                    break;
                default:
                    //showFaceboxError(msg);
                    //break;
            }
        }
    });

    $('button').attr("disabled", "");
}

function restorepassword()
{
    $('button').attr("disabled", "disabled");

    $("label").removeClass("error");

    $('#form1').ajaxSubmit({
        dataType: 'xml',
        success:function(xmlResponse) {
            response = $('response', $(xmlResponse)).eq(0);
            code = response.find('code').text();
            msg = response.find('msg').text();
            url = response.find('url').text();

            switch(code)
            {
                case '1':
                    if (url != null)
                        window.location = url;
                    else
                        window.location = "/";
                    break;
                case '-1':
                case '-9':
                    // Destaco los campos erroneos
                    errors = response.find('errors').eq(0);
                    $("error", errors).each(function() {
                        $('label[for=' +$(this).attr("field")+ ']').addClass("error");
                    });

                    alert(msg);

                    break;
                default:
                    //showFaceboxError(msg);
                    //break;
            }
        }
    });

    $('button').attr("disabled", "");
}

function subscription2()
{
	//$('button').attr("disabled", "disabled");

    $("label").removeClass("error");

    $('#form1').ajaxSubmit({
        dataType: 'xml',
        success:function(xmlResponse) {
            response = $('response', $(xmlResponse)).eq(0);
            code = response.find('code').text();
            msg = response.find('msg').text();
            token = response.find('token').text();

            switch(code)
            {
                case '1':
                    // Redirigimos al TPV para pagar la subscripcion y poder activarla
                    //window.location = 'do/conexion_sis.php';
                    window.location.href = "http://www.legrandmag.com/subscriptors/confirm.php?token=" +token;
                    //alert(msg);
                    break;
                case '2':
                    // Redirigimos directamente a la página de OK
                    window.location.reload();
                    break;
                case '-3':
                case '-1':
                case '-9':
                    // Destaco los campos erroneos
                    errors = response.find('errors').eq(0);
                    $("error", errors).each(function() {
                        $('label[for=' +$(this).attr("field")+ ']').addClass("error");
                    });

                    //alert(msg);

                    break;
                default:
                    //showFaceboxError(msg);
                    //break;
            }
        }
    });

    //$('button').attr("disabled", "");
}

function checkout(f)
{
    vent = window.open('','tpv','width=725,height=600,scrollbars=yes,resizable=yes,status=yes,menubar=no,location=no');
    f.submit();

}

function subscribe_newsletter()
{
	$('button').attr("disabled", "disabled");

    $('#newsletter').ajaxSubmit({
        dataType: 'xml',
        success:function(xmlResponse) {
            response = $('response', $(xmlResponse)).eq(0);
            code = response.find('code').text();
            msg = response.find('msg').text();
            token = response.find('token').text();

            switch(code)
            {
                case '1':
                    // Redirigimos al TPV para pagar la subscripcion y poder activarla
                    alert(msg);
                    break;
                case '-1':
                case '-3':
                case '-9':
                    alert(msg);

                    break;
                default:
                    //showFaceboxError(msg);
                    //break;
            }
        }
    });

    $('button').attr("disabled", "");
}

function unsubscribe(subscription)
{
    $.ajax({
        url: "../do/unsubscribe.php",
        dataType: "xml",
        data: "subscription=" +subscription,
        success:function(xmlResponse)
        {
            response = $('response', $(xmlResponse)).eq(0);
            code = response.find('code').text();
            msg = response.find('msg').text();

            switch(code)
            {
                case '1':
                     window.location.reload();
                    break;

                case '-1':
                case '-9':
                    alert(msg);
                    break;
                default:
                    //showFaceboxError(msg);
                    //break;
            }
        }
	});
}

function view_mediakit()
{
	code = $("#mediakit-code").val();
	if (code == '585858')
		window.open("http://viewer.zmags.com/publication/5d6e2a66", "mediakit");
}

//$(document).ready(function() {


//});




/**
 * Demo helpers
*/
function msg(m){
    $('#msg').append('<br />'+m);
}


// global variables
var _$d = $(document);
var _$w = $(window);
var imgToLoad = 1;

// offset of bottom to load images, on px
var offset = 50;

function imgOndemand(){
	 //calc current scroll position
    var scrollPos = _$w.height() + _$w.scrollTop();
    $('#current').html(scrollPos);

	// get imgs not loaded
	$('img.img-ondemand').each(function(){
		var $img = $(this);
		// filter imgs over scroll limit
        if($img.offset().top < scrollPos+offset){
			$img.attr('src',$img.attr('longdesc')).removeClass('img-ondemand');
        }
	});


	imgToLoad = $('img.img-ondemand').length;

}

$(window).scroll(function(){
	//if (imgToLoad){
	//	imgOndemand();
	//}
});

$(document).ready(function()
{
    //imgOndemand();

    //$("#layout").find("img[class!='nodemand']").each(function(){
    //    $(this).addClass("img-ondemand").attr("longdesc",$(this).attr("src")).attr("src","../img/px.gif");
    //})

    footer = document.getElementById("footer");
    footer.style.display = 'block';

    jQuery('.jcarousel-skin-kiosko, .jcarousel-skin-videoclub').jcarousel({

    });

    $.facebox.settings.opacity = 1
    $('a[rel*=facebox]').facebox();

});
