// dropdown menu. (c) bom 2008

function menu_init(target, tree, base_url) {
    // creates a complete menu structure into target
    // tree is list of tuples. first is a menu root, second list of submenus
    // each root and submenu entry is tuple of page and label
    var t = document.getElementById(target);
    if (!t) return;
    var r = document.createElement('UL');
    r.className += ' menu_nav';
    for (var i=0; i < tree.length ; i++) {
	var lv = tree[i][0];
	var m = menu_create_li(lv, base_url, null);
	m.id = target+"_"+i;
	if (typeof(top_page)=='string' && top_page==((lv.length==2)?lv[1]:lv)) 
	    m.className = 'this';
	if (typeof(top_page)=='string' && top_page=='home' && lv[0]=='Home') 
	    m.className = 'this';
	if (i>0) {
	    var img = document.createElement('img');
	    img.className = 'seperator';
	    img.src = '/faraway/common/top_menu_sep.gif';
	    m.appendChild(img);
	}
	r.appendChild(m);
    }
    t.appendChild(r);
    for (var i=0; i < tree.length ; i++) {
	var items = tree[i][1];
	if (items.length) 
	    menu_create_submenus([[[target+"_"+i],items]], base_url, true);
    }
}

function menu_create_submenus(submenus, base_url, two_col) {
    // adds submenus to existing root targets
    // submenus is list of tuples. first is id of root, second list of submenus
    // each submenu entry is tuple of page and label
    for (var i=0; i < submenus.length ; i++) {
	var lv = submenus[i][0];
	var li = document.getElementById(lv);
	menu_set_mouse_action(li);
	if (!li) continue; // assert typeof(li,'LI')
	var sm = submenus[i][1];
	if (two_col) {
	    var coldiv = document.createElement('UL');
	    var lhs = menu_create_ul(sm.slice(0,sm.length/2), base_url);
	    var rhs = menu_create_ul(sm.slice(sm.length/2), base_url);
	    lhs.className += 'lhs';
	    rhs.className += 'rhs';
	    coldiv.appendChild(document.createElement('LI').appendChild(lhs));
	    coldiv.appendChild(document.createElement('LI').appendChild(rhs));
	    li.appendChild(coldiv);
	} else {
	    var ul = menu_create_ul(sm, base_url);
	    li.appendChild(ul);
	}
    }
}
function menu_create_ul(items, base_url) {
    var ul = document.createElement('UL');
    for (var s=0 ; s <items.length;s++) {
	var m = menu_create_li(items[s], base_url, null);
	ul.appendChild(m);
    }
    return ul;
}
function menu_set_mouse_action(li) {
    li.onmouseover = function () { 
	this.className += " menu_hover";
    };
    li.onmouseout = function() {
	this.className = this.className.replace(/\bmenu_hover\b/, "");
    }
}
function menu_lnk_to_lbl(lnk) {
    var lbl = lnk.replace(/(_|^)([a-z])/g,function(a) {return a.toUpperCase()}).replace(/_/g,' ');
    //var lbl = lnk.replace(/_/g,' ');
    return lbl;
}
function menu_create_li(entry, base_url, child_tree) {
    var lbl, lnk;
    if (entry.length==2) {
	lbl = entry[0];
	lnk = entry[1];
    } else {
	lnk = entry[0];
	lbl = menu_lnk_to_lbl(lnk);
    }
    var a = document.createElement('A');
    a.setAttribute('href', (lnk.indexOf('http:')==0 ? lnk : base_url + lnk+'/index.html'));
    a.innerHTML = lbl;
    var m = document.createElement('LI');
    m.appendChild(a);
    if (child_tree) {
	menu_set_mouse_action(m);
	var list = document.createElement('UL');
	for (var i=0; i<child_tree.length; i++) {
	    var sm = menu_create_li(child_tree[i], base_url, null);
	    list.appendChild(sm);
	}
	m.appendChild(list);
    }
    return m;
}

//var submenus = new Array();
//submenus[0] = ['our_resorts_menu', [['kuala_lumpur'],['taman_negara'],['langkawi']]];
//menu_create_submenus(submenus, '/malaysia/');

var tree = new Array();
tree[0] = [['Home','http://www.farawayholidays.co.uk'], []];
tree[1] = [['destinations'], [
    ['bali'],['borneo'],['cambodia'],['laos'],['malaysia'],['philippines'],
    ['hong_kong'],['singapore'],['thailand'],['myanmar'],['vietnam']
    ]];
tree[2] = [['suggested_itineraries'],[]];
//, [
//    ['Adventurous','suggested_itineraries/search_results.html?style=adventurous'],
//    ['Historical', 'suggested_itineraries/search_results.html?style=historic_interest'],
//    ['Railway', 'suggested_itineraries/search_results.html?style=railway'],
//    ['Beach','suggested_itineraries/search_results.html?style=beaches'],
//    ['Relaxing','suggested_itineraries/search_results.html?style=leisurely_pace'],
//    ['Cruise','suggested_itineraries/search_results.html?style=river_cruise'],
//    ['Family','suggested_itineraries/search_results.html?style=family_friendly'],
//    ['Cultural','suggested_itineraries/search_results.html?style=local_colour'],
//    ['Scenic','suggested_itineraries/search_results.html?style=scenic'],
//   ['Gourmet', 'suggested_itineraries/search_results.html?style=gourmet'],
//    ['Nature','suggested_itineraries/search_results.html?style=nature'], 
//    ['Shopping','suggested_itineraries/search_results.html?style=shopping_and_markets']
//    ]];
tree[3] = [['Holiday style','holidays_by_style'], []];
//    ['Rest and relaxation','holidays_by_style/index.html#rest_and_relaxation_a'],
//    ['Hotel style','holidays_by_style/index.html#hotel_style_a'],
//    ['Active pursuits','holidays_by_style/index.html#active_a'],
//    ['Romantic','holidays_by_style/index.html#romantic_a'],
//    ['Wildlife','holidays_by_style/index.html#wildlife_a'],
//    ['Explore','holidays_by_style/index.html#explore_a'],
//    ['Special interest','holidays_by_style/index.html#special_interest_a'],
//    ['History and culture','holidays_by_style/index.html#history_and_culture_a']
//    ]];
tree[4] = [['special_features'],[]];
//, [
//    ['World heritage','holidays_by_style/world_heritage_sites.html'],
//    ['Orient Express','special_features/orient_express.html'],
//    ['Fly cruises','holidays_by_style/fly_cruises.html'],
//    ['Cookery classes','holidays_by_style/cookery_classes.html'],
//    ['Trekking','holidays_by_style/trekking_in_south_east_asia.html'],
//    ['Photography','special_features/photography_in_south_east_asia.html']
//    ]];
tree[5] = [['extra_information'], [
    ['about_us'],
    ['Why Faraway holidays?','why_faraway_holidays'],
    ['Wish list','quote_request'],
    ['Financial protection','bonding_financial_protection'],
    ['faraway_careers'],
    ['responsible_travel'],
    ['visa_and_passport'],
    ['quote_request'],
    ['contact_us'],
    ['airline_information'],
    ['privacy_policy'],
    ['booking_conditions'],
//    ['brochure_request'],
    ['travel_extras'],
    ['Faraway Blog','http://www.farawayholidays.co.uk/blog']
    ]];
tree[6] = [['special_offers'], []];
menu_init('menu_target', tree, '/faraway/');

/* add offer tag to header images.
Here just to avoid adding another js.
Note also .offer_tag related styling on these divs in layout.css
*/
document.observe('dom:loaded',function() {
    /*
    var now = new Date();
    var valid_till = new Date(2009, 12-1, 31, 23, 59);
    var valid_till = new Date(2010, 06-1, 31, 23, 59);
    if (now > valid_till) {
        $$('#early_booking').each(function(node){ node.style.display = 'none'});
        return;
    }
    */
    var i = document.createElement('img');
    i.className = 'offer_tag';
    i.src = '/faraway/common/2010_offer_tag.png';
    i.alt = "Special offer";
    i.title = "Click for more info";
    var divs = (top_page=="home") ? '.leafframe > .full_width_image' : '.hotel #primary_image, .resort #primary_image, .country #primary_image';
    $$(divs).each(function(node){
        var clone = i.cloneNode(false);
        node.appendChild(clone);
        // extra $ round clone forces prototype to extend it with observe on IE
        $(clone).observe('click', function () {
            document.location ='/faraway/special_offers/';
        });
    });
});


/* 20100229 alter tel num for ctm */
document.observe('dom:loaded',function() {
    var tel = "";
    var search = document.location.search || "";
    var m = search.match(/phonenumber=([0-9]{11})/);
    if (m) {
        tel = m[1];
        setCookie('nr_websales', tel);
    } else 
        tel = getCookie('nr_websales');
    if (tel) {
        $$('.addr_nr_websales').each(function (node) {
            node.update(tel);
        });
    }
});


