﻿// HANDLE POPUPS -----------------------------------------------------------

addOnLoad('init_popup_tipFriend()');

function init_popup_tipFriend() {
    if (document.getElementById('popup_tip_friend')) {
        document.getElementById('popup_tip_friend').onclick = popup_tipFriend;
    }
}

function popup_tipFriend() {
    var wrapper = document.getElementById("wrapper");
    var popup_overlay = document.createElement('DIV');
    popup_overlay.id = 'popup_overlay';
    popup_overlay.style.height = (wrapper.offsetHeight - 130) + 'px';
    popup_overlay.style.width = wrapper.offsetWidth + 'px';
    wrapper.appendChild(popup_overlay);

    var popup_frame = document.createElement('DIV');
    popup_frame.className = 'popup_frame';
    popup_overlay.appendChild(popup_frame);

    var popup_frame2 = document.createElement('DIV');
    popup_frame2.className = 'popup_frame';
    popup_overlay.appendChild(popup_frame2);

    var popup_container = document.createElement('DIV');
    popup_container.id = 'popup_container';
    popup_container.style.top = '211px';
    wrapper.appendChild(popup_container);
    
    $.ajax(
	{
	    type: "POST",
	    url: "/popups/tip_friend.aspx",
	    data: "currentPage="+$('#currentPage').val()+"",

	    // Error!
	    success: function(data) {
	        $('#popup_container').append(data);
	    }
	});

    return false;
}

function popup_tipFriendClose() {
    var popup_overlay = document.getElementById("popup_overlay");
    var popup_container = document.getElementById("popup_container");
    popup_overlay.parentNode.removeChild(popup_overlay);
    popup_container.parentNode.removeChild(popup_container);
    if (document.getElementById('popup_frame')) {
        popup_frame = document.getElementById("popup_frame");
        popup_frame.parentNode.removeChild(popup_frame);
    };
}

function popup_tipFriendSend() {
    var tip_friendName = document.getElementById('tip_friendName').value;
    var tip_friendMail = document.getElementById('tip_friendMail').value;
    var tip_myName = document.getElementById('tip_myName').value;
    var tip_message = document.getElementById('tip_message').value;
    
    if (form_errorCheck('', 'popup_container', 'tip_friendName,tip_friendMail,tip_myName') == 0) {
        var pars = 'tip_friendName=' + tip_friendName;
        pars += '&tip_friendMail=' + tip_friendMail;
        pars += '&tip_myName=' + tip_myName;
        pars += '&street=' + $('#currentPage').val();
        pars += '&link=http://www.vasakronan.se' + $('#currentLink').val();
        pars += '&message=' + tip_message;

        if (tip_action != null && typeof(tip_action) != undefined)
            pars += '&action=' + tip_action;
        
        $.ajax(
	{
	    type: "POST",
	    url: "/popups/tip_friend_thanks.aspx",
	    data: pars,
	    // Error!
	    success: function(data) {
	        $('#popup_container').html(data);
	    }
	});
      
      
 
    }
}


// HANDLE MENU POPUP ---------------------------------------------------------

addOnLoad('init_popup_restMenu()');

function init_popup_restMenu() {
    if (document.getElementById('popup_rest_menu')) {
        document.getElementById('popup_rest_menu').onclick = popup_openRestMenu;
    }
}

function popup_openRestMenu() {
    var ajax = new Ajax.Request(
		this.href,
		{
		    method: 'post',
		    parameters: '',
		    onSuccess: popup_loadRestMenu,
		    onFailure: popup_failRestMenu
		});
    return false;
}


function popup_closeRestMenu() {
    var popup_overlay = document.getElementById('popup_overlay');
    popup_overlay.parentNode.removeChild(popup_overlay);
    var popup_frame = document.getElementById('popup_frame');
    popup_frame.parentNode.removeChild(popup_frame);
    var popup_container = document.getElementById('popup_container');
    popup_container.parentNode.removeChild(popup_container);
}

function popup_loadRestMenu(response) {
    var wrapper = document.getElementById("wrapper");
    var popup_overlay = document.createElement('DIV');
    popup_overlay.id = 'popup_overlay';
    popup_overlay.style.height = (wrapper.offsetHeight - 120) + 'px';
    //popup_overlay.style.top = '110px';
    popup_overlay.style.width = wrapper.offsetWidth + 'px';
    wrapper.appendChild(popup_overlay);

    // Create Drop-shadow
    var popup_frame = document.createElement('DIV');
    popup_frame.id = 'popup_frame';
    popup_frame.style.left = (((wrapper.offsetWidth - 760) / 2) + 1) + 'px';
    popup_frame.style.top = '110px';

    popup_frame_top = document.createElement('DIV');
    popup_frame_top.id = 'popup_frame_top';
    popup_frame.appendChild(popup_frame_top);

    popup_frame_middle = document.createElement('DIV');
    popup_frame_middle.id = 'popup_frame_middle';
    popup_frame.appendChild(popup_frame_middle);

    popup_frame_bottom = document.createElement('DIV');
    popup_frame_bottom.id = 'popup_frame_bottom';
    popup_frame.appendChild(popup_frame_bottom);

    var popup_container = document.createElement('DIV');
    popup_container.id = 'popup_container';
    popup_container.style.width = '750px';
    popup_container.style.height = 'auto';
    popup_container.style.top = '114px';
    popup_container.style.left = ((wrapper.offsetWidth - 750) / 2) + 'px';
    wrapper.appendChild(popup_container);

    var ajaxResponse = Try.these(
		function() { return new DOMParser().parseFromString(response.responseText, 'text/xml'); },
		function() { var xmldom = new ActiveXObject('Microsoft.XMLDOM'); xmldom.loadXML(response.responseText); return xmldom; }
	);

    // Create header
    var header = document.createElement('DIV');
    header.className = 'header';
    popup_container.appendChild(header);
    h4 = document.createElement('H4');
    h4Text = document.createTextNode(ajaxResponse.getElementsByTagName('mainheader')[0].firstChild.nodeValue);
    h4.appendChild(h4Text);
    header.appendChild(h4);

    // Get Restaurant name for print-version
    var restName = document.createElement('DIV');
    restName.className = 'rest_name';
    restName.txt = document.createTextNode(document.getElementById('content').getElementsByTagName('H3')[0].firstChild.nodeValue);
    restName.appendChild(restName.txt);
    header.appendChild(restName);

    // Create close-button
    var close = document.createElement('A');
    close.className = 'close';
    close.href = '#';
    close.onclick = function() { popup_closeRestMenu(); return false }
    closeText = document.createTextNode('St\xE4ng');
    close.appendChild(closeText);
    header.appendChild(close);

    // Append content			
    var days = ajaxResponse.getElementsByTagName('day');
    var dayObjs = new Array();

    // Create the first column of text
    var column_1 = document.createElement('DIV');
    column_1.className = 'column_left';
    for (i = 0; i < 3; ++i) {
        // Create container
        dayObjs[i] = document.createElement('DIV');
        dayObjs[i].className = 'day_container';

        // Create header
        if (days[i].getElementsByTagName('header')[0].firstChild != null) {
            dayObjs[i].header = document.createElement('h5');
            dayObjs[i].header.txt = document.createTextNode(days[i].getElementsByTagName('header')[0].firstChild.nodeValue);
            dayObjs[i].header.appendChild(dayObjs[i].header.txt);
            dayObjs[i].appendChild(dayObjs[i].header);
        }

        // Create content
        if (days[i].getElementsByTagName('text')[0].firstChild != null) {
            dayObjs[i].content = document.createElement('P');
            textNode = days[i].getElementsByTagName('text')[0].firstChild.nodeValue;
            dayObjs[i].appendChild(dayObjs[i].content);
            dayObjs[i].content.innerHTML = textNode;
        }

        column_1.appendChild(dayObjs[i]);
    }
    popup_container.appendChild(column_1);
    // Create the second column of text
    var column_2 = document.createElement('DIV');
    column_2.className = 'column_left';
    for (i = 3; i < days.length; ++i) {

        // Create container
        dayObjs[i] = document.createElement('DIV');
        dayObjs[i].className = 'day_container';

        // Create header
        if (days[i].getElementsByTagName('header')[0].firstChild != null) {
            dayObjs[i].header = document.createElement('h5');
            dayObjs[i].header.txt = document.createTextNode(days[i].getElementsByTagName('header')[0].firstChild.nodeValue);
            dayObjs[i].header.appendChild(dayObjs[i].header.txt);
            dayObjs[i].appendChild(dayObjs[i].header);
        }

        // Create content
        if (days[i].getElementsByTagName('text')[0].firstChild != null) {
            dayObjs[i].content = document.createElement('P');
            textNode = days[i].getElementsByTagName('text')[0].firstChild.nodeValue;
            dayObjs[i].appendChild(dayObjs[i].content);
            dayObjs[i].content.innerHTML = textNode;
        }

        column_2.appendChild(dayObjs[i]);
    }
    popup_container.appendChild(column_2);

    // Create a HR for print-version
    var hr_1 = document.createElement('IMG');
    hr_1.src = '/images/misc/hr_rest_menu.gif';
    hr_1.className = 'hr_rest_menu';
    popup_container.appendChild(hr_1);

    // Create the bottom columns
    var bottom = document.createElement('DIV');
    bottom.className = 'bottom';

    // Bottom column 1
    var bottom_col_1 = document.createElement('DIV');
    bottom_col_1.className = 'column_left';

    // Create menuinfo
    if (ajaxResponse.getElementsByTagName('menuinfo')[0].getElementsByTagName('text')[0].firstChild != null) {
        bottom_col_1.content_2 = document.createElement('P');
        bottom_col_1.content_2.txt = ajaxResponse.getElementsByTagName('menuinfo')[0].getElementsByTagName('text')[0].firstChild.nodeValue;
        bottom_col_1.content_2.innerHTML = bottom_col_1.content_2.txt;
        bottom_col_1.appendChild(bottom_col_1.content_2);
    }
    bottom.appendChild(bottom_col_1);


    // Bottom column 2
    var bottom_col_2 = document.createElement('DIV');
    if (ajaxResponse.getElementsByTagName('open')[0].getElementsByTagName('header')[0].firstChild != null) {
        bottom_col_2.className = 'column_right';
        bottom_col_2.header = document.createElement('H6');
        bottom_col_2.header.txt = document.createTextNode(ajaxResponse.getElementsByTagName('open')[0].getElementsByTagName('header')[0].firstChild.nodeValue);
        bottom_col_2.header.appendChild(bottom_col_2.header.txt);
    }
    if (ajaxResponse.getElementsByTagName('open')[0].getElementsByTagName('text')[0].firstChild != null) {
        bottom_col_2.content = document.createElement('P');
        bottom_col_2.content.txt = ajaxResponse.getElementsByTagName('open')[0].getElementsByTagName('text')[0].firstChild.nodeValue;
        bottom_col_2.content.innerHTML = bottom_col_2.content.txt;
        bottom_col_2.appendChild(bottom_col_2.header);
        bottom_col_2.appendChild(bottom_col_2.content);
    }

    // Create address
    if (ajaxResponse.getElementsByTagName('address')[0].firstChild != null) {
        bottom_col_2.content_2 = document.createElement('P');
        bottom_col_2.content_2.className = 'address';
        bottom_col_2.content_2.txt = ajaxResponse.getElementsByTagName('address')[0].firstChild.nodeValue;
        bottom_col_2.content_2.innerHTML = bottom_col_2.content_2.txt;
        bottom_col_2.appendChild(bottom_col_2.content_2);
    }

    bottom.appendChild(bottom_col_2);

    // Create the print button
    var btn_print = document.createElement('SPAN');
    btn_print.className = 'icon btn_print';
    btn_print.onclick = popup_printRestMenu;
    btn_print.txt = document.createTextNode('Skriv ut menyn');
    btn_print.appendChild(btn_print.txt);
    bottom.appendChild(btn_print);

    popup_container.appendChild(bottom);

    // Ajust the height of the drop-shadow to the height of the content. And append the drop-shadow
    popup_frame.style.height = (popup_container.offsetHeight + 10) + 'px'; // 10 = the size of the shadow
    popup_frame_middle.style.height = (popup_container.offsetHeight - 40) + 'px';
    wrapper.appendChild(popup_frame);

    //Create logo for Service Partner (only in print-version)
    var logo_container = document.createElement('DIV');
    logo_container.className = 'logo_sp_print';
    var logo_sp = document.createElement('IMG');
    logo_sp.src = '/images/skeleton/logo_sp_print.gif';
    logo_container.appendChild(logo_sp);
    document.getElementsByTagName('BODY')[0].appendChild(logo_container);


    // Add specifik print-css for the menu, using a different way to solve this in IE6 since it's not possible with createElement
    document.getElementsByTagName('BODY')[0].className = 'print_restMenu';

    return false;
}

function popup_failRestMenu(response) { }

function popup_printRestMenu() {
    window.print();
}


// MAP POPUP --------------------------------------------------------------

addOnLoad('init_popup_premisesMap()');

function init_popup_premisesMap() {
    if (document.getElementById('btn_map')) {
        document.getElementById('btn_map').onclick = popup_premisesMap;
    }
}

function popup_premisesMap() {
    var wrapper = document.getElementById("wrapper");
    var popup_overlay = document.createElement('DIV');
    popup_overlay.id = 'popup_overlay';
    popup_overlay.style.height = (wrapper.offsetHeight - 120) + 'px';
    popup_overlay.style.width = wrapper.offsetWidth + 'px';
    wrapper.appendChild(popup_overlay);

    // Create Drop-shadow
    var popup_frame = document.createElement('DIV');
    popup_frame.id = 'popup_frame';
    popup_frame.style.left = (((wrapper.offsetWidth - 750) / 2) + 1) + 'px';
    popup_frame.style.top = '141px';

    popup_frame_top = document.createElement('DIV');
    popup_frame_top.id = 'popup_frame_top';
    popup_frame.appendChild(popup_frame_top);

    popup_frame_middle = document.createElement('DIV');
    popup_frame_middle.id = 'popup_frame_middle';
    popup_frame.appendChild(popup_frame_middle);

    popup_frame_bottom = document.createElement('DIV');
    popup_frame_bottom.id = 'popup_frame_bottom';
    popup_frame.appendChild(popup_frame_bottom);

    var popup_container = document.createElement('DIV');
    popup_container.id = 'popup_container';
    popup_container.style.width = '750px';
    popup_container.style.height = '560px';
    popup_container.style.top = '145px';
    popup_container.style.left = ((wrapper.offsetWidth - 750) / 2) + 'px';
    wrapper.appendChild(popup_container);

    // Ajust the height of the drop-shadow to the height of the content. And append the drop-shadow
    popup_frame.style.height = (popup_container.offsetHeight + 10) + 'px'; // 10 = the size of the shadow
    popup_frame_middle.style.height = (popup_container.offsetHeight) + 'px';
    wrapper.appendChild(popup_frame);

    var myAjax = new Ajax.Updater("popup_container", "../popups/map.html", { method: 'post', parameters: "" });

    return false;
}

function popup_premisesMapClose() {
    var popup_overlay = document.getElementById("popup_overlay");
    var popup_container = document.getElementById("popup_container");
    popup_overlay.parentNode.removeChild(popup_overlay);
    popup_container.parentNode.removeChild(popup_container);
    if (document.getElementById('popup_frame')) {
        popup_frame = document.getElementById("popup_frame");
        popup_frame.parentNode.removeChild(popup_frame);
    };
}



