/* catch response and show it in Response layer */
function newsletterResponseHandler(response){
	var msg;
    if (response.status === 'ok') {
        $("#NewsletterResponse p").replaceWith("<p>Your email has been sent successfully.</p>");
    }
    else {
		if (response.status === "") {
			msg = "Server error";
		} else {
			msg = response.status;
		}
        $("#NewsletterResponse p").replaceWith("<p>" + response.status + "</p>");
    }
    $("#NewsletterResponse").show();
    $("#NewsletterAccept").hide();
}

/* subscribe to newsletter and check if email is correct */
function subscribeNewsletter(){
    var email = $('input[name=newsletterInput]').val();
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    var email_valid = (reg.test(email) === true);
    var i_accept = $('input[name=I_Accept]').is(':checked');
    if (!(email_valid && i_accept)) {
        if (i_accept) {
            $("#NewsletterResponse p").replaceWith("<p>Email address is not valid.</p>");
        }
        else {
            $("#NewsletterResponse p").replaceWith("<p>To subscribe you must accept Moovida's Privacy Policy.</p>");
        }
        $("#NewsletterLayout").show();
        $("#NewsletterResponse").show();
        $("#NewsletterAccept").hide();
    }
    else {
        $.post("/newsletter/ajax/add/", 
		       {email: email},
			   newsletterResponseHandler,
			   "json"
			  );
    }
}
/* catch response and show it in Response layer */
function usergroupResponseHandler(response){
	var msg;
    if (response.status === 'ok') {
        $("#UsergroupResponse p").replaceWith("<p>Your email has been sent successfully.</p>");
    }
    else {
		if (response.status === "") {
			msg = "Unknown server error";
		} else {
			msg = response.status;
		}
        $("#UsergroupResponse p").replaceWith("<p>" + msg + "</p>");
    }
    $("#UsergroupResponse").show();
    $("#UsergroupAccept").hide();
}
/* subscribe to User Group and check if email is correct */
function subscribeUsergroup(){
    var email = $('input[name=usergroupInput]').val();
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    var email_valid = (reg.test(email) === true);
    var i_accept = $('input[name=I_Accept]').is(':checked');
    if (!(email_valid && i_accept)) {
        if (i_accept) {
            $("#UsergroupResponse p").replaceWith("<p>Email address is not valid.</p>");
        }
        else {
            $("#UsergroupResponse p").replaceWith("<p>To subscribe you must accept Moovida's Privacy Policy.</p>");
        }
        $("#UsergroupLayout").show();
        $("#UsergroupResponse").show();
        $("#UsergroupAccept").hide();
    }
    else {
		jQuery.ajax({
            type: 'PUT',
            url: "/usergroup",
            data: {email: email},
            success: usergroupResponseHandler,
            dataType: "json"
            });
    }
}

$(document).ready(function(){

    // Funcio acordeon per la FAQ. Recull l'anchor passat per url
    // tanquem totes les opcions
    $('#faqs div.contentFAQ').hide();
    // si a la url tenimn #xxxx, obrim la pregunta que toca
    var question = document.location.hash;
    if (question && question.length > 0) {
        $(question).next('div').slideDown();
        $(question).addClass('active');
    }
    // a cada h3 li asignem el onclick, que les tancarà totes i obrirà la que hem clicat
    $('#faqs h3').click(function(){
        if ($(this).hasClass('active')) {
            $(this).next('div').slideUp();
            $(this).removeClass('active');
        }
        else {
            $('#faqs div.contentFAQ').slideUp();
            $('#faqs h3').removeClass('active');
            $(this).next('div').slideDown();
            $(this).addClass('active');
        }
        
    });
    
	$("a.boxed").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600,
		'speedOut'		:	200,
		'overlayOpacity' : 0.75,
		'overlayColor' : '#000',
		'cyclic' : true,
		'autoDimensions' : false,
		'width' : 955,
		'height' : 533,
		'hideOnContentClick': false
	});

    $("a.video").fancybox({
        'hideOnContentClick': false,
        'zoomSpeedIn': 100,
        'zoomSpeedOut': 50,
        'width': 912,
        'height': 536,
		'autoDimensions' : true,
        'overlayShow': true,
        'overlayOpacity': 0.75,
		'type': 'iframe',
		'titleShow': false,
        'callbackOnClose': function(){
            $("#fancy_content").empty();
        }
    });

    // Funció per evitar el _target blank
    $(function(){
        $('a.externo').click(function(){
            window.open(this.href);
            return false;
        });
    });
    
    $("a#permanentDowloadLinkPro").fancybox({
        'overlayColor': '#000',
        'padding': 0,
        'showCloseButton': false
    });
    
    $("a.buynow").fancybox({
        'overlayColor': '#000',
        'padding': 0,
        'showCloseButton': false
    });
    
    $("a.buyDownload").fancybox({
        'overlayColor': '#000',
        'padding': 0,
        'showCloseButton': false
    });
    
    $("a.promotional").fancybox({
        'overlayColor': '#000',
        'padding': 0,
        'showCloseButton': false
    });

	$("#submitNewsletter").click(function() {
		$('#NewsletterAccept').show();
		$('#NewsletterLayout').show();
	});
	$("#NewsletterLayout").click(function () {
		$("#NewsletterAccept").hide();
		$("#NewsletterLayout").hide();
		$("#NewsletterResponse").hide();
	});
	$("#NewsletterResponse").click(function () {
		$("#NewsletterAccept").hide();
		$("#NewsletterLayout").hide();
		$("#NewsletterResponse").hide();
	});
    $("#subscribeInput").click(function () {
		subscribeNewsletter();
	});
	// User Group
	$("#usergroup").click(function() {
		$('#UsergroupAccept').show();
		$('#UsergroupLayout').show();
	});
	$("#UsergroupLayout,#UsergroupAccept .close").click(function () {
		$("#UsergroupAccept").hide();
		$("#UsergroupLayout").hide();
		$("#UsergroupResponse").hide();
	});
	$("#UsergroupResponse").click(function () {
		$("#UsergroupAccept").hide();
		$("#UsergroupLayout").hide();
		$("#UsergroupResponse").hide();
	});
    $("#UGsubscribeInput").click(function () {
		subscribeUsergroup();
	});
	
	$('#usergroupInput').watermark('Enter your email');
	$('#moovidadb_q').watermark("Search your movies");

//	$('.jqTransform').jqTransform({imgPath:'/images/'});

}); //end document.ready

//Funcions per sustituïr Fonts amb el Cufon
Cufon.replace('h1', {
    color: '-linear-gradient(#45494d, #000)',
    textShadow: '1px 1px #80828b'
});
Cufon.replace('.download h2', {
    color: '-linear-gradient(#45494d, #000)',
    textShadow: '1px 1px #80828b'
});
Cufon.replace('.features h2', {
    color: '-linear-gradient(#45494d, #000)',
    textShadow: '1px 1px #80828b'
});
Cufon.replace('.immersed h2', {
    color: '-linear-gradient(#45494d, #000)',
    textShadow: '1px 1px #80828b'
});
Cufon.replace('.core h2', {
    color: '-linear-gradient(#45494d, #000)',
    textShadow: '1px 1px #80828b'
});
Cufon.replace('.contact h2', {
    color: '-linear-gradient(#45494d, #000)',
    textShadow: '1px 1px #80828b'
});
Cufon.replace('.infoProductLeft h1', {
    color: '#fff',
    textShadow: '1px 1px #80828b'
});
Cufon.replace('#coreVideo h2 a', {
    color: '#fff'
});
Cufon.replace('#infoHelp h1', {
    color: '#fff',
    textShadow: '1px 2px 2px #333333'
});
Cufon.replace('.help #mainContent h2', {
    color: '-linear-gradient(#45494d, #000)',
    textShadow: '1px 1px #80828b'
});
Cufon.replace('.thanks #mainContent h2', {
    color: '-linear-gradient(#45494d, #000)',
    textShadow: '1px 1px #80828b'
});
Cufon.replace('.faq h2', {
    color: '-linear-gradient(#45494d, #000)',
    textShadow: '1px 1px #80828b'
});
Cufon.replace('.home h2 span.light', {
    color: '#c7c7c9',
    textShadow: '0px 0px 0px transparent'
});
Cufon.replace('.home h2 span.normal', {
    color: '#fff',
    textShadow: '0px 0px 1px #fff'
});
Cufon.replace('.landing-promo h2 span.light', {
    color: '#c7c7c9',
    textShadow: '0px 0px 0px transparent'
});
Cufon.replace('.landing-promo h2 span.normal', {
    color: '#fff',
    textShadow: '0px 0px 1px #fff'
});
Cufon.replace('.landing-promo #mainHeaderContent h1', {
    color: '#fff',
    textShadow: '1px 1px 1px #333333'
});
Cufon.replace('.landing-promo #mainContent h2', {
    color: '-linear-gradient(#45494d, #000)',
    textShadow: '1px 1px #80828b'
});
Cufon.replace('.landing-promo table th', {
    color: '-linear-gradient(#45494d, #000)',
    textShadow: '1px 1px #80828b'
});
Cufon.replace('.landing-promo table span', {
    color: '-linear-gradient(#45494d, #000)',
    textShadow: '1px 1px #80828b'
});

/* NEW HOME */

Cufon.replace('.newhome #header a.logo', {
    color: '#fff',
    textShadow: '1px 1px 1px #606060'
}); 

Cufon.replace('.newhome #homeMainHeader h1', {
    color: '#fff',
    textShadow: '1px 2px 3px #666'
});
Cufon.replace('.newhome #homeMainHeader h2', {
    color: '#fff',
    textShadow: '1px 2px 3px #666'
});
Cufon.replace('.newhome #mainContent h2', {
    color: '#000'
});


