(function($){  // Javascript variable scoping. Wrap your functions into this function to protect your $ variable from the global scope. $ becomes a local variable.
    $(document).ready(function(){  //executed functions when document is ready

    
    $(document).bind("contextmenu",function(e){
        return false;
    });

    
    // Expand Panel
	$("div.credits, #createAccount, #loadCredits, #register1, #register2, #freeCreditsAccount").click(function(){
            $("div#panel").slideDown("slow");
            $("#toggle a").toggle();	
	});
    
    //Check if div is visible and load page into div
    var updateConsults = function(){
        if($('#consultsData').is(":visible")){
            $('#consultsData')
            .load($(location).attr('protocol')+"//"+$(location).attr('host')+'/include_astroconsults.php')
            .fadeIn("slow");
        }

        if($('#consultsDataChat').is(":visible")){
            $('#consultsDataChat')
            .load($(location).attr('protocol')+"//"+$(location).attr('host')+'/include_astroconsults_chat.php')
            .fadeIn("slow");
        }

        //$('#result').load('http://credits.astro-consult.eu/ivr/astro/credits.php');
    };
    
    var updateCredits = function() {
        $.ajax({
            type:   "POST",
            url:    $(location).attr('protocol')+"//"+$(location).attr('host')+"/_ajaxCall.php",
            data :  {formType:'updateCredits'},
            async:  false,
            success:    function(data) {
                $('#updateCredits').html(data);
            }
        });
        return false; // don't follow the link!
    }

    //Refresh div every 30 seconds = 30000
    setInterval(function(){updateConsults();}, 30000);
    setInterval(function(){updateCredits();}, 30000);
    //var auto_refresh = setInterval(function(){updateConsults();}, 5000);
    //updateConsults();

    //Language Switch
    $('div[id^="selLang_"]').live('click', function() { //ID starts with "selLang_"
        //alert($(this).attr('ref'));

        $.ajax({
            type:   "POST",
            url:    $(location).attr('protocol')+"//"+$(location).attr('host')+"/_ajaxCall.php",
            data :  {formType:'setLanguage', lang:$(this).attr('id'), page:$(this).attr('ref')},
            async:  false,
            success:    function(data) {
                window.open($(location).attr('protocol')+"//"+$(location).attr('host')+"/"+data, '_self');
            }
        });
        return false; // don't follow the link!
    });

    //Country Switch
    $('div[id^="selCountry_"]').live('click', function() { //ID starts with "selLang_"
       // alert($(this).attr('ref'));

        $.ajax({
            type:   "POST",
            url:    $(location).attr('protocol')+"//"+$(location).attr('host')+"/_ajaxCall.php",
            data :  {formType:'setCountry', country:$(this).attr('id'), page:$(this).attr('ref')},
            async:  false,
            success:    function(data) {
                window.open($(location).attr('protocol')+"//"+$(location).attr('host')+"/"+data, '_self');
            }
        });
        return false; // don't follow the link!
    });
    
    //Display Tooltip
    //$("#countryLang div[title]").tooltip();


    //Contact Form
    //Show/Hide Consults
    $("#TRConsult").hide();

    $("#subject").live('change', function() { //ID starts with "selLang_"
        //alert($(this).val());
        if (($(this).val()) == 'reference') {
            $("#TRConsult").show();
        }
        else  $("#TRConsult").hide();
    });
 
 
     $('.rPaymentAmount').live('click', function() {

        //alert($(this).attr('id')+' '+$(this).val());
        
        /*
        $('input#paymentAmount').val($(this).val());
        $('input#merchantReference').val($(this).attr('id'));
        */
        
        $.ajax({
            type:   "POST",
            url:    $(location).attr('protocol')+"//"+$(location).attr('host')+"/_ajaxCall.php",
            data :  {formType:'creditsOrder', creditsOrder:$(this).attr('id'), creditsAmount:$(this).attr('value')},
            async:  false,
            success:    function(data) {
                var json = eval('('+data+')');
                
                //alert(data);
                //alert(json.encodedData+' '+json.md5Sign);

                $('input#data').val(json.encodedData);
                $('input#sign').val(json.md5Sign);
                
            } 
        }); 
        //return false; // don't follow the link!

    });


    //Forgot Password
    $('.forgotPass').children('a').click(function() {
        $('#form_login').hide();
        //$('div.alert').hide();
        //$('div.info').hide();
        $('#form_requestPass').show();
        $("#email").focus();
        return false;
    });
    
    $('#form_requestPass').live('submit', function() {
       //alert('form_requestPass cliked');
       $.post($(location).attr('protocol')+"//"+$(location).attr('host')+"/_ajaxCall.php", $(this).serialize(), function(data){
            
            var json = eval('('+data+')');
            //alert(data);

            if(json.message=='info') {
                $('div.err').hide();
                $('#form_requestPass').hide();
                $('#form_login').show();
                $('div.info').show().html(json.string);
            }
            
            if(json.message=='alert') {
                $('div.info').hide();
                $('div.err').show().html(json.string);
                $("input.text:first").focus();
                $("#email").focus();
            }
        });
        return false;      
    })

/*-----------------------------------------------------------------------------*/
/*--- General -----------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/

    //Set focus to first text input field
    $("input.text:first").focus();

    //set highlight on focus
    $('input[type=text], input[type=password]').focus(function() {
        $(this).addClass('textFocus');
    });

    //Remove highlight on blur
    $('input').blur(function() {
        $(this).removeClass('textFocus');
    });
    
    //Email Consult Form
    
    $('#what3').hide();
   
    
    //Radio Click Carts
    $('.showQuestions').click(function() {
        $('#moreQuestions').show();
        $('#what3').show();
        $('#what1').hide();
        $('#tr_subject').hide();
        $('#img_required').hide();
        $('#question1').removeAttr('required');
        $('#subject').attr('disabled', true);
        
        $.ajax({
            type:   "POST",
            url:    $(location).attr('protocol')+"//"+$(location).attr('host')+"/_ajaxCall.php",
            data :  {formType:'updateCredits'},
            async:  false,
            success:    function(data) {
                if(data < 55) {
                    $('.alert2').hide();
                    $('.alert').show();
                    $('#button2').attr('disabled', true);
                }
                else {
                    $('#button2').removeAttr('disabled');
                    $('.alert').hide();
                }                
            }
        });
        
    });

    //Radio Click 1 question
    $('.hideQuestions').click(function() {
        $('#moreQuestions').hide();
        $('#what3').hide();
        $('#what1').show();
        $('#tr_subject').show();
        $('#img_required').show();
        $('#question2').val('');
        $('#question3').val('');
        $('#question1').attr('required', true);
        $('#subject').removeAttr('disabled');
        
        
        $.ajax({
            type:   "POST",
            url:    $(location).attr('protocol')+"//"+$(location).attr('host')+"/_ajaxCall.php",
            data :  {formType:'updateCredits'},
            async:  false,
            success:    function(data) {
                if(data < 10) {
                    $('.alert2').hide();
                    $('.alert').show();
                    $('#button2').attr('disabled', true);
                }
                else {
                    $('#button2').removeAttr('disabled');
                    $('.alert').hide();
                } 
            }
        });
    });
    


$.tools.validator.fn("select", "Select a value", function(input, value) {
return (value == 'none') ? false : true;
});

/*-----------------------------------------------------------------------------*/
/*--- End General -------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/

    }); // end ready function
})(jQuery);

