/* custom strip tags fn */

jQuery.fn.stripTags = function()
{
    return this.replaceWith(this.html().replace(/<\/?[^>]+>/gi, ''));
};

var clean_url = function()
{
    var str = window.location.toString();
    var res = str.substring(0,str.indexOf('#'));    
    return (res == '')?window.location:res;
}

var siteLogin = function(form, url, response_target)
{
    var data = form.serialize();

    if (response_target)
    {
        var idrasp = response_target;
    }
    else
    {
        var response_target = '#response';
    }

    //$(response_target).fadeOut();
    $(response_target).fadeIn();
    //$(response_target).html('<img src="_res/images/i-am-loading.gif" alt=""/>');
    $(response_target).html('');
    $.fancybox.showActivity();

    $.ajax(
    {
        type: 'POST',
        url: url,
        data: data,
        success: function(output)
        {
            // $(response_target).html('');
            //alert(output);

            $.fancybox.hideActivity(); //hide the loading animation

            try
            {
                var result_output = jQuery.parseJSON(output);
            }
            catch (e)
            {
                // We report an error, and show the erronous JSON string (we replace all " by ', to prevent another error)
                var result_output = '';
            }

            if (result_output.errors)
            {
                //we have errors
                $(response_target).html(result_output.errors);
            }

            if (result_output.itsok == 1)
            {
                //redirect_url = window.location;
                //var redirect_url = result_output.redirect;
                //window.location = redirect_url;
                window.location = clean_url();
            }
        }
    });
}

var siteUserPass = function(form, url, response_target)
{
    var data = form.serialize();

    if (response_target)
    {
        var idrasp = response_target;
    }
    else
    {
        var response_target = '#response';
    }

    //$(response_target).fadeOut();
    $(response_target).fadeIn();
    $(response_target).html('');
    $.fancybox.showActivity(); //do some loading animation

    $.ajax(
    {
        type: 'POST',
        url: url,
        data: data,
        success: function(output)
        {
            // $(response_target).html('');
            //alert(output);
            $.fancybox.hideActivity(); //hide the loading animation

            try
            {
                var result_output = jQuery.parseJSON(output);
            }
            catch (e)
            {
                // We report an error, and show the erronous JSON string (we replace all " by ', to prevent another error)
                var result_output = '';
            }

            if (result_output.errors)
            {
                //we have errors
                $(response_target).html(result_output.errors);
            }

            if (result_output.itsok == 1)
            {
                $("#recovery").html(result_output.response_text);
            }
        }
    });
}

var populateSportsLeagues = function()
{
    var selected_sports = [];
    var selected_leagues = [];

    $('#which_sports p').each(function()
    {
        if ($(this).hasClass('like'))
        {
            selected_sports.push($(this).attr('rel'));
        }
    });

    $('.liga ul li').each(function()
    {
        if ($(this).hasClass('like'))
        {
            selected_leagues.push($(this).attr('rel'));
        }
    });

    if($("#sports_c").val()!='-1')
        $("#sports_c").val(selected_sports.join("|"));
    
    if($("#leagues_c").val()!='-1')    
        $("#leagues_c").val(selected_leagues.join("|"));
}

var siteRegister = function(form, url, response_target, extra)
{
    var current_step = $("#step").val();    
    
    if (current_step == 2)
    {
        populateSportsLeagues();
    }

    var data = form.serialize();

    if (response_target)
    {
        var idrasp = response_target;
    }
    else
    {
        var response_target = '#response';
    }

    //$(response_target).fadeOut();
    $(response_target).fadeIn();
    //$(response_target).html('<img src="_res/images/i-am-loading.gif" alt=""/>');
    $(response_target).html('');
    $.fancybox.showActivity();

    $.ajax(
    {
        type: 'POST',
        url: url,
        data: data,
        success: function(output)
        {
            // $(response_target).html('');
            //alert(output);

            $.fancybox.hideActivity();

            try
            {
                var result_output = jQuery.parseJSON(output);
            }
            catch (e)
            {
                // We report an error, and show the erronous JSON string (we replace all " by ', to prevent another error)
                var result_output = '';
            }

            if (result_output.step_return == 2)
            {
                $(response_target).html('');
                $("#register_step_2").show();
                $("#register_step_1").hide();

                if (extra)
                {
                    do_step_2_work();
                }
            }

            $("#step").val(result_output.step_return);

            if (result_output.errors)
            {
                //we have errors
                $(response_target).html(result_output.errors);
            }

            if (result_output.itsok == 1)
            {
                $("#register").html(result_output.response_text);
            }
        }
    });
}

var do_step_2_work = function()
{
    $('table#which_sports tr td p, table.choose_sports tr td p, ul.bets li p').click(function()
    {
        $(this).toggleClass('like');
    });

    $('.select:not(.date)').each(function()
    {
        var parinte = $(this);
        var triggered = parinte.find('.to_open');
        parinte.find('a.trigger').add(parinte).click(function()
        {
            if (triggered.is(':hidden'))
            {
                parinte.addClass('open');
                triggered.slideDown(100);
            }
            else
            {
                triggered.slideUp(100, function()
                {
                    parinte.removeClass('open');
                });
            }
            return false;
        });

        triggered.find('li:not(.no)').each(function()
        {
            var asta = $(this);
            asta.bind('mouseover', function()
            {
                overLi(asta, true);
            });
            asta.bind('mouseout', function()
            {
                overLi(asta, false);
            });
            asta.click(function(event)
            {
                if (!parinte.hasClass('select_single'))
                {
                    event.stopPropagation();

                    if (!asta.hasClass('like'))
                    {
                        asta.addClass('like').unbind('mouseout', overLi).unbind('mouseover', overLi);
                    }
                    else
                    {
                        asta.removeClass('like').bind('mouseover', overLi(asta, true)).bind('mouseout', overLi(asta, false));
                    }
                }
                else
                {
                    asta.removeClass('over');
                    parinte.find('p span').text(asta.text()).attr("class", asta.attr("class"));
                }
            });
        });
    });

    var one_opened = false;
    $(document).click(function(event)
    {
        $('.to_open').each(function()
        {
            if ($(this).css('display') == 'block')
            {
                one_opened = true;
            }
        });

        if (one_opened)
        {
            if (!$(event.target).hasClass('trigger') && !$(event.target).parent().parent().parent().hasClass('select'))
            {
                $('.select').each(function()
                {
                    $(this).removeClass('open').find('.to_open').hide();
                    one_opened = false;
                });
            }
        }
    });
}

var url_fancybox = function(url_to_load)
{
    $.fancybox(
    {
        'transitionIn': 'fade',
        'transitionOut': 'fade',
        'centerOnScroll': 'true',
        'overlayColor': '#000',
        'overlayOpacity': '0.8',
        'padding': '3',
        'autoScale': 'true',
        'titlePosition': 'inside',
        'autoDimensions': 'true',
        'href': base_url + url_to_load
    });
}

var select_all_sports = function()
{
    var sports_c = $('input#sports_c');
    
    if(sports_c.val()=='-1')    
    {
        sports_c.val('');
        $('table#which_sports p').each(function() {        
            $(this).removeClass('like');
        }); 
    }
        
    else 
    {
        sports_c.val('-1');
        $('table#which_sports p').each(function() {        
            $(this).addClass('like');
        }); 
    }
        
        
       
}

var select_all_leagues = function()
{
    var leagues_c = $('input#leagues_c');
    
    if(leagues_c.val()=='-1')
    {
        leagues_c.val('');
        $('li.rf_option').each(function() {
            $(this).removeClass('like');
        });
        $('p.all').removeClass('like');
    }        
    else 
    {
        leagues_c.val('-1');
        $('li.rf_option').each(function() {
            $(this).addClass('like');
        });
        $('p.all').addClass('like');
    }
        
                
}

/*
$.address.init(function(event) {
                log('init: ' + serialize({
                    value: $.address.value(), 
                    path: $.address.path(),
                    pathNames: $.address.pathNames(),
                    parameterNames: $.address.parameterNames(),
                    queryString: $.address.queryString()
                }));
            });
*/

$(function()
{
    //base_url + url_to_load

    url_to_load = $.address.value();

    if (url_to_load != '/')
    {
        var check_element_exists = url_to_load.replace('/', '#');

        //if element is in html, we don't load the fancybox
        if ($(check_element_exists).length < 1)
        {
            url_fancybox(url_to_load);
        }
    }
});

var open_oferta_menu = function()
{
    $('#oferta_menu').show();
}

var close_oferta_menu = function()
{
    $('#oferta_menu').hide();
}
//load oferta menu
$.post('pages/oferta_menu', function(data)
{
    $('#oferta_menu').html(data);
});


//menu oferta
$(function()
{
    $(".header ul.menu li:not('.oferta_item')").bind("mouseenter", close_oferta_menu);
    $('.oferta_item').bind("mouseenter", open_oferta_menu);
    $('div.top').bind("mouseleave", close_oferta_menu);
    $('#oferta_menu').bind("mouseleave", close_oferta_menu);
});
//menu oferta


$(document).ready(function()
{
    $('#launch_register').mouseover(function()
    {
        $('#tooltip-homepage').stop(true, true).show();
    }).mouseout(function()
    {
        $('#tooltip-homepage').stop(true, true).hide();
    });

/*$("#virtual-ticket").makeFloat({x:"current",y:"current",spped:"fast"});*/

/*
$(window).scroll(function() {
    alert('fa');
    $('.top_login').css('top', $(this).scrollTop() + "px");
});
*/

/*--- END - VIRTUAL TICKET ---*/
});

/*--- VOUCHER ---*/
$(document).ready(function()
{
    $('a#voucher-trigger').click(function()
    {
        $.fancybox.showActivity();

        $.ajax(
        {
            type: "POST",
            cache: false,
            url: "voucher/",
            data: '',
            success: function(data)
            {
                $.fancybox(data);
            }
        });

        return false;
    });
});

/*--- hover logo ---*/
$(document).ready(function()
{
    $('map#logo_map').hover(function()
    {    
        $('img.logo').attr('src','_res/images/logo_hover.png');                    
    },
    function(){
        $('img.logo').attr('src','_res/images/logo.png');             
    });
    
    
    $("#social-twitter").hover(
      function () {          
        $(this).find('img').attr('src',base_url+'/_res/images/social_twitter_hover.png');
      }, 
      function () {
        $(this).find('img').attr('src',base_url+'/_res/images/social_twitter.png');
      }
    );
    
    $("#social-facebook").hover(
      function () {          
        $(this).find('img').attr('src',base_url+'/_res/images/social_facebook_hover.png');
      }, 
      function () {
        $(this).find('img').attr('src',base_url+'/_res/images/social_facebook.png');
      }
    );        
});

//Create account 
$(document).ready(function()
{
    var sports_c = $('input#sports_c');
    var leagues_c = $('input#leagues_c');    
    
    $('div#register.popup div#register_step_2 p.my_sports a.all').click(function() {
        sports_c.val(''); 
    });
    
    $('form#form_register div#register.popup div#register_step_2 p.all').click(function() {
        leagues_c.val('');
        $('p.all').removeClass('like');        
    });   
});



