/**
 * $Id: notification_alert.js 1294 2011-11-07 08:52:39Z wesley $
 * @version $Rev: 1294 $
 */

function ucFirst(string) {
	return string.substring(0, 1).toUpperCase() + string.substring(1).toLowerCase();
}

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

var handleSubscription = function(performer) {
    var email = $('#notification_alert_signup input[name="email"]').val();
    
    var filter = /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/;
    
    if(email == '') {
        $('#notification_alert_signup span.errorFld').html('E-mailadres kan niet leeg zijn.');
        return;
    } else if(filter.test(email) == false) {
        $('#notification_alert_signup span.errorFld').html('Dit e-mailadres bestaat niet. Vul een bestaand e-mailadres in.');
        return;
    } else if(performer == '') {
        $('#notification_alert_signup span.errorFld').html('Opgegeven cammer bestaat niet.');
        return;
    }
    if (typeof(domain) == 'undefined') 
       var domain = 'islive.nl';
   
    var requestLocation = typeof($.islive) == 'object' ? $.islive.custom + '/' : 'http://www.' + domain +  '/splash1/';
	var requestLocation = 'http://www.luckycams.nl/';    
    performer = ucFirst(performer);
    
    if(window.location.pathname.search("splash") <= 0) {
        var array           = window.location.hostname.split(".");
        var requestFrom = array[array.length-2]+"."+array[array.length-1]+(window.location.hostname.search("islive") >= 0 ? '/splash1' : '');
    }
    
    
    $.getJSON(requestLocation+'notifications/opt-in.php?'+$.param({performer : performer, email: email, requestFrom: requestFrom})+'&'+partnerInfo+'&callback=?', function(data) {
        var _dialogElement = $('#jqDialog');
        switch(data.status) {
            case 'verify_email':
                _dialogElement.html('Er is zojuist een berichtje naar dit e-mailadres gestuurd.<br /><br />Controleer nu je inbox en klik op de meegezonden link om dit adres te bevestigen.<br />Zodra je dit gedaan hebt, is de Camspotter direct actief. <br /><br />Deze service is helemaal gratis en blijft dat ook !');
                _dialogElement.dialog('option', 'position', 'center');
                break;
            case 'invalid_email':
                $('#notification_alert_signup span.errorFld').html('Dit e-mailadres bestaat niet. Vul een bestaand e-mailadres in.');
                break;
            case 'error':
                _dialogElement.html('Er is zojuist een onbekende fout opgetreden. Probeer het over enkele minuten opnieuw.');
                _dialogElement.dialog('option', 'position', 'center');
                break;
            case 'already_exists':
                _dialogElement.empty();
                _dialogElement.html('Je ontvangt al een berichtje als '+performer+' online komt.');
                _dialogElement.dialog('option', 'position', 'center');
                _dialogElement.css('min-height', '');
                break;
            case 'ok':
                _dialogElement.html('Je ontvangt voortaan direct een berichtje als '+performer+' online komt.');
                _dialogElement.dialog('option', 'position', 'center');
                _dialogElement.css('min-height', '');
                break;
               
        }
    });
};

$('.emailFld').live('focus', function() {
     $(this).keypress(function(e) {
        if(e.which == 13) {
            $('.register-for-notification').click();
            $(this).blur();
        }
    });
});

$(function() {
    var jqDialogElement = $('#jqDialog').length > 0 ? $('#jqDialog') : $('<div />').attr('id','jqDialog').prependTo($('body'));
    showDialog = false;
    title = '';
    var optout = gup('optout');
    var optin = gup('optin');
  
    if(optout != '') {
        jqDialogElement.html('<br/>Je ontvangt voortaan geen bericht meer als '+ucFirst(optout)+' online komt. Maar er is natuurlijk keuze genoeg! Veel chat en cam plezier !<br/><br/>');
        showDialog = true;
        title = 'Afgemeld voor de Gratis Camspotter';
    } else if(optin != '') {
        jqDialogElement.html('<br/>Vanaf nu ontvang je een berichtje als '+ucFirst(gup('m'))+' online komt.<br/>');
        title = 'Gratis Camspotter';
        showDialog = true;
    }
    
    if(showDialog) {
        if(jqDialogElement.is(':data(dialog)')) {
            jqDialogElement.dialog('destroy');
        }
    
        jqDialogElement.dialog({
            title: title,
            width: '500px',
            resizable: false,
            open: function(event, ui){$('.ui-widget-overlay').css('width','100%'); },
            close: function(event, ui){},
            modal: true
        });
        jqDialogElement.css('min-height', '');
    }
    
    $('.notification_alert,.camspotterclick').live('click', function() {
        var iconElement;
        if ( $(this).hasClass('camspotterclick') ) {
            iconElement = $(this);
        } else {
            iconElement = $('img.addnotification', $(this));
        }
        var performerName = iconElement.attr('rel');
        
        if(jqDialogElement.is(':data(dialog)')) {
            jqDialogElement.empty();
            jqDialogElement.dialog('destroy');
        }
        
        var dialogElement = $('<div />')
        .addClass('notification_alert_signup_base').attr('id', 'notification_alert_signup').append(
            $('<p />').addClass('textGroup').append(
                $('<img />').css({float:'left',margin:'0 15px 0 15px'}).addClass('performerSnapshot picsmall').attr('src',$('a[rel="'+performerName+'"] img.performerSnapshot').attr('src'))
            ).append(
                $('<span />').html('Wil jij direct een persoonlijk berichtje van <strong class="performerName">'+ucFirst(performerName)+'</strong> ontvangen als ze online komt?')
            )
        ).append(
            $('<p />').addClass('textGroup').append(
                $('<dl />').append(
                    $('<dt />').append(
                        $('<label />').attr('for','email').text('Vul dan hier je e-mailadres in:')
                    )
                ).append(
                    $('<dd />').css('margin-top','2px').append(
                        $('<input />').attr('type','text').attr('name','email').attr('id','email').addClass('emailFld text ui-widget-content ui-corner-all')
                    ).append(
                        $('<button type="submit" />').addClass('sendButton ui-state-hover ui-corner-all').text('Verstuur')
                    ).append(
                        $('<br />')
                    ).append(
                        $('<span />').addClass('errorFld')
                    )
                )
            )
        ).append(
            $('<ul />').addClass('subscriptionTerms').append(
                $('<li />').text('We sturen je alleen een bericht als ze online komt')
            ).append(
                $('<li />').text('Je kunt direct live chatten')
            ).append(
                $('<li />').text('Deze service is echt helemaal gratis !')
            )
        ).append('<br />');
        
        $('.sendButton',dialogElement).click(function() {
            handleSubscription(performerName);
        });
        
        jqDialogElement.html(dialogElement);
        
        jqDialogElement.dialog({
            title: 'Gratis Camspotter',
            width: '550px',
            resizable: false,
            open: function(event, ui){$('.ui-widget-overlay').css('width','100%'); },
            close: function(event, ui){},
            modal: true
        });
    });
});

