// Case studies ------------

var MMCase = {
    blocks: [],
    contents_table: null,
    contents_tbody: null,
    init: function () {
        var case_blocks = MMWCommon.getElementsByTagNameClass(document,"div","caseBlock");
        if (case_blocks.length > 1) {
            MMWCommon.addCssClass(document.getElementById("caseFull"), "thumbnails");
            MMCase.contents_table = document.createElement('table');
            MMCase.contents_tbody = document.createElement('tbody');
            MMCase.contents_table.appendChild(MMCase.contents_tbody);
            MMCase.contents_table.setAttribute("id","caseContents");
            for ( var i = 0, j = case_blocks.length; i < j; ++i ) {
                var block = new MMCase.CaseBlock( case_blocks[i] );
                MMCase.blocks.push ( block );
                MMCase.blocks[i].block_index = i;
            }
            MMCase.blocks[0].open();
        } else if ( case_blocks.length > 0) {
            var paras = case_blocks[0].getElementsByTagName('p');
        }
    }
};

MMCase.CaseBlock = function( _container) {
    this.container = _container;
    this.block_index = null;
    if(this.container.getElementsByTagName('h3')[0]) {
        this.title = this.container.getElementsByTagName('h3')[0].firstChild.nodeValue + "\u00a0\u00bb";
    }
    if (MMWCommon.getElementsByTagNameClass(document,"*","caseImage").length > 1) {
        this.image = this.container.getElementsByTagName('img')[0];
        this.is_open = MMWCommon.hasCssClass(this.container, 'opened') ? true : false;
        MMCase.contents_tbody.appendChild( this.createContentRow() );
    }
    
};

MMCase.CaseBlock.prototype = {
    is_open: false,
    open: function() {
        this.is_open = true;
        this.close();
        MMWCommon.addCssClass(this.container, "opened");
        MMWCommon.removeCssClass(this.container, "closed");
        this.positionContents();
    },
    close: function() {
        for ( var i = 0, j = MMCase.blocks.length; i < j; ++i ) {
            this.is_open = false;
            if (MMWCommon.hasCssClass(MMCase.blocks[i].container,"closed")) {
            } else {
                MMWCommon.addCssClass(MMCase.blocks[i].container, 'closed');
                MMWCommon.removeCssClass(MMCase.blocks[i].container, 'opened');
            }
        }
    },
    createContentRow: function() {
        var me = this;
        var tr = document.createElement("tr");
        var td = [];
        var anchor = [];
        var img = document.createElement('img');
        var text = document.createTextNode(this.title);
        for ( var i = 0, j = 2; i < j; ++i ) {
            anchor[i] = document.createElement("a");
            anchor[i].href = "http://" + window.location.host + window.location.pathname + "#show";
            img.src = this.image.src;
            img.alt = "Screenshot Thumbnail";
            i == 0 ? anchor[i].appendChild(img) : anchor[i].appendChild(text);
            td[i] = document.createElement("td");
            td[i].appendChild(anchor[i]);
        }
        MMWCommon.addCssClass(td[0],"thumb");
        tr.appendChild(td[0]);
        this.title != undefined ? tr.appendChild(td[1]) : null;
        tr.onclick = function() { 
            MMCase.blocks[me.block_index].open();
        };
        return tr;
    },
    positionContents: function() {
        if(this.container.getElementsByTagName('h3')[0]) {
            this.container.insertBefore(MMCase.contents_table, this.image.nextSibling);
        } else if (MMWCommon.getElementsByTagNameClass(this.container.parentNode,"div","caseImage").length > 1) {
            this.container.insertBefore(MMCase.contents_table, MMWCommon.getElementsByTagNameClass(this.container,"div","caseImage")[0].nextSibling);
        }
    }
};

// FAQ ------------
var MMHelp = {
    sections: null,
    selected_id: null,
    clicked: null,
    init: function() {
        if (!document.getElementById("help")) { return false; }
        MMHelp.sections = MMWCommon.getElementsByTagNameClass(document,"dl","FAQsection");
        var show_all_link = document.getElementById("showAll").firstChild;
        show_all_link.parentNode.style.display = "block";
        show_all_link.onclick = function() {
            MMWCommon.hasCssClass(this, "selected") ? MMWCommon.removeCssClass(this,"selected") : MMWCommon.setCssClass(this,"selected");
            MMHelp.showAll();
            return false;
        };
        if (MMHelp.sections.length > 0) {
            var questions, answers;
            for (var i=0, j=MMHelp.sections.length; i < j; ++i) {
                questions = MMHelp.sections[i].getElementsByTagName("dt");
                answers = MMHelp.sections[i].getElementsByTagName("dd");
                for (var m = 0, n = questions.length; m < n; ++m ) {
                    MMWCommon.setCssClass(answers[m],"hide");
                    questions[m].firstChild.onclick = function() {
                        MMHelp.selected_id = this.href.split("#")[1].substr(2);
                        MMHelp.clicked = "list";
                        MMHelp.open();
                    };
                }
            }
        }
        if (window.location.hash && !MMHelp.clicked) {
            MMHelp.selected_id = window.location.hash.substr(3);
            MMHelp.open();
        }
        MMHelp.topFAQ();
    },
    showAll: function() {
        for ( var i = 0, j = this.sections.length; i < j; ++i ) {
            questions = this.sections[i].getElementsByTagName("dt");
            answers = this.sections[i].getElementsByTagName("dd");
            var show_all = MMWCommon.hasCssClass(document.getElementById("showAll").firstChild,"selected");
            for (var m = 0, n = questions.length; m < n; ++m ) {
                if (show_all == true) { 
                    MMWCommon.addCssClass(answers[m],"selected");
                    MMWCommon.addCssClass(questions[m],"selected");
                } else {
                    MMWCommon.addCssClass(answers[m],"hide");
                    MMWCommon.removeCssClass(answers[m],"selected");
                    MMWCommon.addCssClass(questions[m],"hide");
                    MMWCommon.removeCssClass(questions[m],"selected");
                }
            }
        }
    },
    topFAQ: function() {
        var topFAQbox = document.getElementById("addNav");
        var topFAQlinks = topFAQbox.getElementsByTagName("a");
        for (var i = 0, j = topFAQlinks.length; i < j; ++i) {
            topFAQlinks[i].onclick = function() {
                MMHelp.selected_id = this.href.split("#")[1].substr(2);
                MMHelp.clicked = "faq";
                MMHelp.open();
                window.location.hash = 'q' + MMHelp.selected_id;
                return false;
            };
        }
    },
    open: function() {
        var question = document.getElementById("q"+MMHelp.selected_id);
        var answer = document.getElementById("a"+MMHelp.selected_id);
        if (MMWCommon.hasCssClass(answer,"hide")) {
            MMWCommon.setCssClass(question,"selected");
            MMWCommon.setCssClass(answer,"selected");
        } else if (MMHelp.clicked == "list") {
            MMWCommon.setCssClass(question,"hide");
            MMWCommon.setCssClass(answer,"hide");
        }
        if(!MMHelp.clicked) {   // on load
            if (window.location.hash.indexOf('#aq') != -1) {
                window.location.hash = 'q' + MMHelp.selected_id;
            }
            if (navigator.userAgent.toLowerCase().indexOf("safari") == -1) {
                window.location.hash = 'aq' + MMHelp.selected_id;
            }
        }
    }
};

// Open all external links in a new window
function newWindow() { 
    var links;
    links = document.getElementsByTagName("a");
    for ( i = 0, j = links.length; i < j; ++i ) {
        if(links[i].href.indexOf("multimap.com") == -1) {
            links[i].title = "Opens in a new window";
            links[i].onclick = function() { window.open(this.href); return false; };
        }
    }
}

// Set focus on the element which has class 'focus'
function setFocus() { 
    var elements = MMWCommon.getElementsByTagNames(document, "input,select,textarea");
    for ( i = 0, j = elements.length; i < j; ++i ) {
        if ( MMWCommon.hasCssClass(elements[i], "focus") ) {
               elements[i].focus();
               break; 
        }
    }
}

// Check state of confirmation checkboxes
function checkState() {
    if (document.getElementById("bkey") && document.getElementById("url")){
    // openapi update page
    	var bkey = document.getElementById("bkey");
    	var url = document.getElementById("url");
    	var agree = document.getElementById("agree");
    	MMWCommon.addToEvent( agree, "click", function() {
            if (bkey.disabled == true && agree.checked == true){
            	bkey.disabled = false;
            	url.disabled = false;
           } else {
            	bkey.disabled = true;
            	url.disabled = true;
           }
        });

    } else if (document.getElementById("agreement") && document.getElementById("openapi_agree")) {
    // registration page
    	var bkey = document.getElementById("acceptRegistration"); // Button
    	var url = document.getElementById("openapi_url"); // URL
    	var agree = document.getElementById("openapi_check"); // I want OpenAPI - checkbox
    	var agreeConfirm = document.getElementById("openapi_agree"); // I Agree OpenAPI Terms & Conditions
    	var agreement = document.getElementById("agreement"); // 1. Agree
    	var openapiWrapper = document.getElementById("openapiWrapper");
    	if (agree.checked == true){
    		MMWCommon.addToEvent( agreeConfirm, "click", function() {
					if (agreeConfirm.checked == true) {
						url.disabled = false;
						bkey.disabled = false;
					} else {
						url.disabled = true;
						bkey.disabled = true;
					}
        });		
    	}
    	MMWCommon.addToEvent( agree, "click", function() {
			if (MMWCommon.hasCssClass(openapiWrapper, "dn") == true && agree.checked == true ){
           		MMWCommon.removeCssClass(openapiWrapper, "dn");
           		bkey.disabled = true;
           		MMWCommon.addToEvent( agreeConfirm, "click", function() {
					if (agreeConfirm.checked == true) {
						url.disabled = false;
						bkey.disabled = false;
					} else {
						url.disabled = true;
						bkey.disabled = true;
					}
           		});		
           	} else {
           		MMWCommon.addCssClass(openapiWrapper, "dn");
           		bkey.disabled = false;
           		url.disabled = true;           			
           	}
        });	
    }
    
    if (document.getElementById("updateButton")){
    	MMWCommon.addToEvent( document.getElementById("updateButton"), "click", function() {
    		window.location='/openapi/signup/?act=update';
    	});
    }
    if (document.getElementById("oaSignInButton")){
    	MMWCommon.addToEvent( document.getElementById("oaSignInButton"), "click", function() {
    		window.location='https://' + window.location.hostname + '/my/signin/?bundle=openapi/signup';
    	});
    	MMWCommon.addToEvent( document.getElementById("oaSignUpButton"), "click", function() {
    		window.location='https://' + window.location.hostname + '/my/register/?openapi_create=1';
    	});
    }
}



// Tour Pages			//
function tourPageEnhancer(){
	//check if you're in a Tour page or not
	if (document.getElementById("footerSearchBox")){
		window.scrollTo(0,140);
		var tabTracker = document.createElement('img');
		
		MMWCommon.addToEvent( document.getElementById("tourTab01"), "click", function() {
			tabTracker.src = "http://switch.atdmt.com/action/Search_EN_GB_Multimap_Header_BirdsEyeView";
			document.getElementById("footerSearchBox").appendChild(tabTracker);
		});
		MMWCommon.addToEvent( document.getElementById("tourTab02"), "click", function() {
			//We're using extra SearchTheWeb tracking code for missing Directions code.
			tabTracker.src = "http://switch.atdmt.com/action/Search_EN_GB_Multimap_Header_SearchTheWeb";
			document.getElementById("footerSearchBox").appendChild(tabTracker);
		});
		MMWCommon.addToEvent( document.getElementById("tourTab03"), "click", function() {
			tabTracker.src = "http://switch.atdmt.com/action/Search_EN_GB_Multimap_Header_FindABusiness";
			document.getElementById("footerSearchBox").appendChild(tabTracker);
		});
	}
}

function onPageLoad() {
    MMCase.init();
    MMHelp.init();
    newWindow();
    MMWCommon.MinWidthForIE.init();
    tourPageEnhancer();
    setFocus();
    checkState();
}

MMWCommon.ready(function() { onPageLoad() });

