/*~type:text/javascript~*/
/**
 *
 * THIS FILE REQUIRES 'jquery.jBrowserBookmark.js' TO WORK(AND VICE-VERSA)
 */
function showMessage(message, txtColor) {
    var theDiv = jQuery('#page-functions-messages');
 
    if (!txtColor) {
       theDiv.css('color', 'red');
    }
    else {
        theDiv.css('color', txtColor);
    }
    
    theDiv.css('float', 'right');
    theDiv.hide();
    theDiv.html(message).slideDown(1500, function() {
        // Animation complete, faded in, show message for xxMs then fade out
        theDiv.delay('6000');
        theDiv.slideUp('slow');
        theDiv.fadeOut('slow');
    });
}


/**
 * Used for the print icon, to print a page
 * uses the <a id="print-page"> tag
 */

function reportBug() {
    jQuery("#report-bug-prompt").hide();

    jQuery("#report-bug").click(function () {

        jQuery("#page-functions-prompt").hide();

        jQuery("#report-bug-prompt").parent().css('position', 'relative');
        jQuery("#report-bug-prompt").css('position', 'absolute');
        jQuery("#report-bug-prompt").css('right', 10);
        jQuery("#report-bug-prompt").css('top', 40);
        jQuery("#report-bug-prompt").slideToggle("slow");
        jQuery(this).toggleClass("active");
        return false;
    });

    jQuery('#cancel_report_btn').click(function () {
        jQuery("#report-bug-prompt").slideToggle("slow");
        jQuery(this).toggleClass("active");
        return false;
    });

    jQuery('#report_btn').click(function () {
        var comments = jQuery(this).siblings('#report_bug_txt_area').val();
        // var reportbugUrl = jQuery('#report-bug').attr('title');

        jQuery.ajax({
            type: 'POST',
            url: "/ajax/",
            data: 'ajaxpage=report_bug.php&comments='+comments+'&pageurl='+$("#bug_report_url").val() + '&bug_page_id=' + $("#bug_page_id").val(),
            success: function(data) {
                // show us the message from emailpg_tpl which says whether the bug has been reported or not
                // see emailpg_tpl for exact data string
                if (data == 'Problem has been reported.'){
                    showMessage(data, 'green');
                }
                else {
                    showMessage(data);
                }
            }
        });

        jQuery("#report-bug-prompt").slideToggle("slow");
        jQuery(this).toggleClass("active");

        showMessage("Reporting problem on page!!...");
        //echo the message "Reporting problem on page..." and wait for error or success message
        /*var theDiv = jQuery('#page-functions-messages');
        theDiv.hide();
        theDiv.css('color', 'red');
        theDiv.css('float', 'right');
        theDiv.html("Reporting problem on page...").slideDown(1500);*/

        return false;
    });

}

function emailPage() {
    jQuery("#page-functions-prompt").hide();
    
    jQuery("#email-page").click(function () {

        jQuery("#report-bug-prompt").hide();

        jQuery("#page-functions-prompt").parent().css('position', 'relative');
        jQuery("#page-functions-prompt").css('position', 'absolute');
        jQuery("#page-functions-prompt").css('right', 10);
        jQuery("#page-functions-prompt").css('top', 40);
        jQuery("#page-functions-prompt").slideToggle("slow");
        jQuery(this).toggleClass("active");
        return false;
    });

    jQuery('#cancel_btn').click(function () {
        jQuery("#page-functions-prompt").slideToggle("slow");
        jQuery(this).toggleClass("active");
        return false;
    });

    jQuery('#mail_btn').click(function () {
        var toMail = jQuery(this).siblings('#to_address_input').val();
        var toName = jQuery(this).siblings('#to_name_input').val();
        var fromName = jQuery(this).siblings('#from_name_input').val();

        var emailpgUrl = jQuery('#email-page').attr('title');

        jQuery.ajax({
            type: 'POST',
            url: "/ajax/",
            data: 'ajaxpage=emailpage.php&pageurl='+$("#bug_report_url").val()+'&tomail='+toMail+'&toname='+toName+'&fromname='+fromName+'&pagetitle='+ escape(document.title) +'&pagedescription='+ $('meta[name=description]').attr("content"),
            success: function(data) {
                // show us the message from emailpg_tpl which says whether the page was mailed or not
                // see emailpg_tpl for exact data string
                if (data == 'Page has been mailed.'){
                    showMessage(data, 'green');
                }
                else {
                    showMessage(data);
                }
            }
        });
        
        jQuery("#page-functions-prompt").slideToggle("slow");
        jQuery(this).toggleClass("active");

        showMessage("Emailing page!!...");

        //echo the message "Emailing page..." and wait for error or success message
        /*var theDiv = jQuery('#page-functions-messages');
        theDiv.hide();
        theDiv.css('color', 'red');
        theDiv.css('float', 'right');
        theDiv.html("Emailing page...").slideDown(1500);*/

        return false;
    });

}

function searchButton(){
    jQuery(".searchbutton").click(function () {
        searchString = jQuery("#search-input").val();
        document.location = "http://www.google.com/cse?cx=013940366107060305588:_amayuyhidk&ie=UTF-8&q=" + searchString;
        return false;
    });
}

function toggleInputBoxes(inputs) {
     // remove default value for editing when input box clicked
    inputs.focus(function() {
        // only remove if the text has not changed
        if(this.value == this.defaultValue)
        {
            this.value ='';
        }
        //return false;
    });

    // put back default value when box not in focus (may also use.focusout)
    inputs.blur(function() {
        // only put back default value if nothing entered/typed
        if(this.value == '')
        {
            this.value = this.defaultValue;
        }
        //return false;
    });
}

/**
 * soon as the page is ready activate the page functions icons'
 * 
 */
jQuery(document).ready(function() {
    reportBug();
    emailPage();
    searchButton();
    toggleInputBoxes(jQuery("#page-functions-prompt").children("input"));
    toggleInputBoxes(jQuery("#report-bug-prompt").children("textarea"));
    toggleInputBoxes(jQuery("#search-input"));
    jQuery("#add-favourites").jBrowserBookmark();
    jQuery("#add-favourites").click( function() {
        jQuery("#page-functions-prompt").hide();
        jQuery("#report-bug-prompt").hide();
    });
	
	$("#print-page").live("click", function(){
		if (window.print) {
            window.print();
        } else {
            showMessage("Please press 'Ctrl + P', to print this page.");
        }
        return false;
	});
});
