/* Login  manipulation */
var loginText      = '';
var replacedText   = '';
function hideTextLogin(el)
{
  if(loginText==''){
   loginText = el.value;
   replacedText = replacedText == '' ? loginText : replacedText;
   el.value = '';  
  }else{ 
    loginText = el.value;
    el.value = replacedText == loginText ? '' : el.value;
  }
}

function showTextLogin(el)
{ 
    el.value = el.value.replace(/[ ]/g,'') != '' ? el.value : replacedText;
}

function win(url, name, width, height) { 
  if (opener != undefined && typeof opener == 'object'){
      opener.close();
  }  
  features = 'scrollbars=1,resizable=1,menubar=0,toolbar=0,location=0,status=0,top=0,left=0,screenX=0,screenY=0,';

  if (window.screen) {
    aw = screen.availWidth;
    ah = screen.availHeight;
    features = features+'width='+(aw-20)+',height='+(ah-38);
  }
  else {
    features = features+'width='+width+',height='+height;
  }
  opener  = window.open(url, name, features);
}

/**
* Cookies
**/
var _cookies;
function _initCookies() {
  _cookies = {};
  var ca = document.cookie.split(';');
  var re = /^[\s]*([^\s]+?)$/i;
  for(var i = 0; i < ca.length; i++) {
    var c = ca[i].split("=");
    if(c.length == 2) {
     _cookies[c[0].match(re)[1]] = unescape(c[1].match(re) ? c[1].match(re)[1] : '');
    }
  }
}

function getCookie(name) {
  if(!_cookies) _initCookies();
  return _cookies[name];
}

function setCookie(name, value, days) {
  if(!_cookies) _initCookies();
  _cookies[name] = value;
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";

  var domain = location.host.match(/[^.]+\.[^.]+\.[^.]+$/);
  document.cookie = name+"="+escape(value)+expires+"; path=/" + (domain ? '; domain=.'+domain : ';');
}

/* video player  */
// before using this function swfobject.js should be included 
function startPlayer( videoSrc, image, width, height, skin )
{
    // Config
    var root_player_path = ""; 
    var defaultWidth  = 520;
    var defaultHeight = 360;
    var defaultSkin   = 'snel.swf';
    
    // Init 
    width  = typeof(width)=='undefined' ? defaultWidth : width;
    height = typeof(height)=='undefined' ? defaultHeight : height;
    skin =   typeof(skin)=='undefined' ? defaultSkin : skin;
    image =  typeof(image)=='undefined' ? '' : image; 
    
    swfobject.embedSWF(
        root_player_path + 'player.swf',
        'videoplayer',
        width,
        height,
        '9.0.0',
        null,
        {
            // Flashvars
            skin: root_player_path + skin, 
            file: videoSrc,
            image: image,
            autostart: 'false',
            duration: -1
        },
        {
            //Params
            allowfullscreen: 'true',
            allowscriptaccess: 'always'
        },
        {
            id:'player',
            name:'player'
        }
    );
}

var theme  = '';
var domain = location.href.replace(/\/[^/]+$/,'');

/* jquery */
if(typeof(jQuery) != undefined)
{
    jQuery.noConflict();
    (function($){
            $(function(){
                $('.config-link').click(function(){
                    $('.mmenu').toggle('slow');
                });
                
                $("#smenu1").hover(
                    function () { $('#ssmenu1').stop(true, true).fadeIn('slow'); }, 
                    function () { $('#ssmenu1').stop(true, true).delay(300).fadeOut('slow'); }
                );
                $("#smenu2").hover(
                    function () { $('#ssmenu2').stop(true, true).fadeIn('slow'); }, 
                    function () { $('#ssmenu2').stop(true, true).delay(300).fadeOut('slow'); }
                );
                $("#smenu3").hover(
                    function () { $('#ssmenu3').stop(true, true).fadeIn('slow'); }, 
                    function () { $('#ssmenu3').stop(true, true).delay(300).fadeOut('slow'); }
                );
                $("#ssmenu1").hover(
                    function () { $('#ssmenu1').stop(true, true).fadeIn('slow'); }, 
                    function () { $('#ssmenu1').stop(true, true).delay(300).fadeOut('slow'); }
                );
                $("#ssmenu2").hover(
                    function () { $('#ssmenu2').stop(true, true).fadeIn('slow'); }, 
                    function () { $('#ssmenu2').stop(true, true).delay(300).fadeOut('slow'); }
                );
                $("#ssmenu3").hover(
                    function () { $('#ssmenu3').stop(true, true).fadeIn('slow'); }, 
                    function () { $('#ssmenu3').stop(true, true).delay(300).fadeOut('slow'); }
                );
                
                /* registration form validation */
                /*
                $('#form-register').data('unlock',false).submit(function(){
                    var form = $(this);
                    var login = $('input[name="new_login"]'); 
                    var email = $('input[name="new_email"]');
                    var pass  = $('input[name="new_password"]');
                    var confirm = $('input[name="confirm_password"]');
                    $('.error').text('');
                    $.post('data.php?req=check_login',{'login':login.val()},function(data){
                        if(!$.trim(data.status) == '') {
                            login.parent().parent().find('.error').text(data.status);
                        }else{
                            var error = false;
                            if ($.trim(email.val()) == '' || email.val().indexOf('@') == -1) {
                                error = true;
                                email.parent().parent().find('.error').text('E-mailadres moet geldig zijn');
                            }
                            if($.trim(pass.val()) == '') {
                                error = true;
                                pass.parent().parent().find('.error').text('Wachtwoord te kort');
                            }
                            if($.trim(confirm.val()) == ''){
                                error = true;
                                confirm.parent().parent().find('.error').text('Bevestig wachtwoord');
                            }   
                            if(!error)setTimeout(function(){$('#form-register').data('unlock',true).submit()},500);
                        }
                    },'json');
                    if(form.data('unlock')){
                       form.data('unlock',false); 
                       return true; 
                    }                    
                    return false;
                });
                */
                
                /* contact form validation */
                $('#form-contact').find('input[name="code"]').val('');
                $('#form-contact').submit(function(){
                    var form = $(this);
                    var name = $('input[name="naam"]');
                    var email = $('input[name="email"]');
                    var error = false;
                    $('.error').text('');
                    if($.trim(name.val()) == ''){
                        error = true;
                        name.parent().parent().find('.error').text('El nombre no puede estar vacío');
                    }  
                    if ($.trim(email.val()) == '' || email.val().indexOf('@') == -1) {
                        error = true;
                        email.parent().parent().find('.error').text('Dirección de correo electrónico debe ser válida');
                    }
                    return error ? false : true;
                });
                
                /* social  users photo, blog, personal data copying form */
                var socialForm = $('#hyves_photo_form, #hyves_user_form, #hyves_blog_form, #facebook_user_form,#facebook_photo_form');
                socialForm.submit(function(){
                    if(!$('input[type="checkbox"]:checked').length)
                    {
                        alert("seleccionar los elementos para copiar");
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                });
                 /* social controls */
                 socialForm.find('.controls .select').click(function(){
                      $(this).parent().parent().find('input[type="checkbox"]').attr('checked','checked');
                 });
                 socialForm.find('.controls .deselect').click(function(){
                      $(this).parent().parent().find('input[type="checkbox"]').removeAttr('checked');
                 });
                 
                 $('<div id="test"/>').prependTo('body');
                 
                  $('*').each(function(i,v){
                        var elem = $(this); 
                        elem.data('fontSize',elem.css('fontSize'));
                        if(elem.attr('class')=='container') elem.data('width',elem.css('width')); 
                  });

                  $('input[name="enable_private"]').change(function(){
                        if(!$(this).attr('checked'))$('select[name="blocks[webcam_private]"]').attr('disabled','disabled');
                        else $('select[name="blocks[webcam_private]"]').removeAttr('disabled');          
                  }).change();
                  $('input[name="enable_visitor"]').change(function(){
                        if(!$(this).attr('checked'))$('select[name="blocks[webcam_visitor]"]').attr('disabled','disabled');
                        else $('select[name="blocks[webcam_visitor]"]').removeAttr('disabled');          
                  }).change();
                  
                  $('.ilike').live('click',function(){
                        var self = $(this);
                        var params = self.attr('id').split('-');
                        $.post('data.php?req=ilike',{'id':params[1],'s':params[2],'status':params[3]}, function(data){
                            if(data.status == undefined){
                                if (data.result == 'spam'){
                                    $('<div/>',{'id':'overlay',css:{'display':'none','width':'280px','padding':'35px','font-size':'12px','background-image':'url(images/ovrwhite.png)','height':'200px'}}).appendTo('body');

                                    var overlay = $('#overlay').overlay({
                                        expose: {color: '#fff',loadSpeed: 200,opacity: 0.5},
                                        effect: 'apple', 
                                        api   : true,
                                        top   : '30%',
                                        onClose: function(){
                                            this.getOverlay().find(':not(.close)').remove();
                                        }
                                    });
                                    var olayObj = overlay.getOverlay();
                                    $('<p/>',{css:{'margin':'20px 0 8px','text-align':'center','font-size':'16px'}}).html('Usted tiene que confirmar que sus acciones le pertenecen a usted').appendTo(olayObj);
                                    $('<div class="captcha-error" style="height:15px;text-align:center;color:#f00;"></div>').appendTo(olayObj); 
                                    $('<div style="height:15px;"></div><div style="float:left;margin-left:10px;margin-top:20px;"><label>Code: <input type="text" name="code" style="width:40px;padding:2px;" /></label></div><div style="float:left;margin-left:15px;"><img id="captcha" style="border:1px solid #ccc;" src="secpic.php" alt="captcha" /></div> <img id="refresh-captcha" src="images/refresh.png" alt="refresh"  title="refrescar" /><div style="clear:both;height:20px;"></div><div id="iconfirm" style="margin-left: 85px;cursor:pointer;" class="btn-d">Confirmar</div>').appendTo(olayObj);
                                    $('#refresh-captcha').click(function(){
                                        $('#captcha').attr('src','secpic.php?'+(Math.random()*1000000));
                                    });
                                    olayObj.find('#iconfirm').click(function(){
                                        var code = olayObj.find('input[name="code"]');
                                        if ($.trim(code.val()) == ''){
                                            olayObj.find('.captcha-error').text('Código está vacía');
                                            return;
                                        }else{
                                            $.post('data.php?req=ilike-confirm',{'id':params[1],'s':params[2],'status':params[3],'code':code.val()}, function(data){
                                                if (data.status == undefined){
                                                   olayObj.find('.close').click(); 
                                                   self.unbind('click').removeClass('ilike').addClass('ivoted'); 
                                                }else{
                                                   olayObj.find('.captcha-error').text(data.status); 
                                                }
                                            },'json');
                                        }
                                    });
                                    overlay.load();
                                }else{
                                    self.unbind('click').removeClass('ilike').addClass('ivoted');
                                }
                            }else{
                                alert('Para poder votar debes identificarte como miembro');
                            }
                        },'json');
                  });
                  
            });
            
            /* delete photo */
            $('.thumb a.del').live('click',function(){
                var msg = "¿Está seguro que desea eliminar?";
                if(confirm(msg))
                {
                    var params = $(this).attr('href').replace('?','');
                    $.post('data.php?req=del_photo', params ,function(data){
                        if ( data.status == undefined)
                        {
                            $('#thumb-'+data.id).fadeOut('slow',function(){$(this).remove()})
                        }
                    },'json');
                }
                return false;
            });
            
             /* delete album */
            $('.submit_albumverwijderen').live('click',function(){
                var msg = "¿Está seguro que desea eliminar?";
                if(confirm(msg))
                {
                    var params = $(this).attr('href').replace('?','');
                    $.post('data.php?req=del_album', params ,function(data){
                        if ( data.status == undefined)
                        {
                            $('#album-'+data.id).fadeOut('slow',function(){$(this).remove()})
                        }
                    },'json');
                }
                return false;
            });
            
            $(document).mousedown(function(){
                $('.popup').trigger('clear');
            });
            
    })(jQuery);
}
