// **********************************************
// JavaScript init file for Supplierbank
// @author  - Simon Pollard for Deckchair UK Ltd
// @version - 1.1
// @date    - June '10

// Function taken from http://www.thefutureoftheweb.com/blog/adddomloadevent
addDOMLoadEvent = (function(){
    // create event function stack
    var load_events = [],
        load_timer,
        script,
        done,
        exec,
        old_onload,
        init = function () {
            done = true;

            // kill the timer
            clearInterval(load_timer);

            // execute each function in the stack in the order they were added
            while (exec = load_events.shift())
                exec();

            if (script) script.onreadystatechange = '';
        };

    return function (func) {
        // if the init function was already ran, just run this function now and stop
        if (done) return func();

        if (!load_events[0]) {
            // for Mozilla/Opera9
            if (document.addEventListener)
                document.addEventListener("DOMContentLoaded", init, false);

            // for Internet Explorer
            /*@cc_on @*/
            /*@if (@_win32)
                document.write("<script id=__ie_onload defer src=//0><\/scr"+"ipt>");
                script = document.getElementById("__ie_onload");
                script.onreadystatechange = function() {
                    if (this.readyState == "complete")
                        init(); // call the onload handler
                };
            /*@end @*/

            // for Safari
            if (/WebKit/i.test(navigator.userAgent)) { // sniff
                load_timer = setInterval(function() {
                    if (/loaded|complete/.test(document.readyState))
                        init(); // call the onload handler
                }, 10);
            }

            // for other browsers set the window.onload, but also execute the old window.onload
            old_onload = window.onload;
            window.onload = function() {
                init();
                if (old_onload) old_onload();
            };
        }

        load_events.push(func);
    }
})();


// Once dom is loaded
addDOMLoadEvent(onReady);


//do on ready
function onReady() {
    // Get the input version of "company hq" in user nav
    var r = document.getElementById('usernav_comphq');
    // Get the paragraph that holds it
    var b = document.getElementById('company_hq_hold');

    // If the input exists
    if(b!=null) {
        // Remove the input
        b.removeChild(r);

        // Now create an anchor
        var link = document.createElement("a");
        // Give is some text
        var txt = document.createTextNode("Company HQ");
        link.appendChild(txt);
        // Add its href
        link.setAttribute('href','javascript:document.user_login_form.submit();');
        // And its title
        link.setAttribute('title','Go to Company HQ');
        // Now add it to the paragraph as refereced above
        b.appendChild(link);
    }

    // If the feedback link is in use
    if(document.getElementById('feedback_link')!=null)
    {
        // Update the feedback link to use ajax/javascript functionality
        var feedback = document.getElementById('feedback_link');
        var url = window.location.href;
        feedback.setAttribute('href','javascript:showBox(\"'+url+'\",\"380\",\"470\",\"/feedback\");');
        feedback.setAttribute('title','Test');
    }

};


// Show Box
//
// url - the url of the page the user has clicked the link from
// height - the height of the box
// width - the width of the box
// page - the page you want to display in the box
function showBox(url,height,width,page)
{
        // Set values for variables if not defined
        if (url==undefined)
	{ url = ''; }

	if (height==undefined)
	{ height = 200; }

	if (width==undefined)
	{ width = 200; }

	if (page==undefined)
	{ page = "/feedback"; }

	// Add cover and cover-content divs to the dom
	$("body").append("<div id='cover'></div><div id='cover-content'></div>");
	// Add content to cover-content

	// Now position and size the box in the middle of the page
	$("#cover").css("filter","alpha(opacity=0)");
	$("#cover-content").css("height",+height);
	$("#cover-content").css("margin-top",+-(height/2));
	$("#cover-content").css("width",+width);
	$("#cover-content").css("margin-left",+-(width/2));

	// Now fade them in, cover first then content
	$("#cover").animate({opacity: 0.75}, 600, function(){
		$('#cover-content').fadeIn('1000',function(){
			$("#cover").css("filter","alpha(opacity=75");
			$("#cover-content").load(page,function(){
                        $("#feedback_url").attr("value",url);})
                    ;});
	});

	// Add a click function to replicate hideBox()
	$("#cover").click(function () {
            // Fade out cover-content div and remove from dom
            $("#cover-content").fadeOut("1000", function(){
                    $('#cover-content').remove();
            });

            // Fade out cover div and remove from dom
            $("#cover").fadeOut("1000", function(){
                    $('#cover').remove();
            });
        });

	$("#close_map_link").click(function () {
            // Fade out cover-content div and remove from dom
            $("#cover-content").fadeOut("1000", function(){
                    $('#cover-content').remove();
            });

            // Fade out cover div and remove from dom
            $("#cover").fadeOut("1000", function(){
                    $('#cover').remove();
            });
        });
}

// Hide Box
function hideBox()
{
	// Fade out cover-content div and remove from dom
	$("#cover-content").fadeOut("1000", function(){
		$('#cover-content').remove();
	});

	// Fade out cover div and remove from dom
	$("#cover").fadeOut("1000", function(){
		$('#cover').remove();
	});
}

// jQuery Ajax function to handle form submit
//
// $form is the ID of the form you have submited
// $page is the page you want to submit the form to
// $target is the ID of the div you wish to inject the updated code into
function submitForm(form,page,target) {

       var dataArray = $('#'+form).serialize();

        // post to the right page within the div - add close functionality to div as well
	$.post(page, dataArray,
		function(data, textstatus)
		{
			$("div#"+target).html(data);
                        $("#cover-content").css("height",170);
		}
        );


        // Return flase to prevent the form actually submiting
	return false;
}