// JavaScript Document

//global variable to allow console inspection of tree:
var tree;
var nodes = [];
var nodeIndex;
var treemenu = new Array();
var subnode = new Array();
treemenu[0] = "Advertisers";
treemenu[1] = "Publishers";
treemenu[2] = "Resources";

//anonymous function wraps the remainder of the logic:
(function() {

	//function to initialize the tree:
    function treeInit() {
        buildRandomTextNodeTree();
    }
    
	//Function  creates the tree and 
	//builds between 3 and 7 children of the root node:
    function buildRandomTextNodeTree() {
	
		//instantiate the tree:
        tree = new YAHOO.widget.TreeView("treeDiv1");
		var expand=false;
		for (var i = 0; i < treemenu.length ; i++) {
			//setting which tree should be expanded
		 if ((expandPublisher==true) && (i==1))
		 	expand=true;
		else if ((expandAdvertiser==true) && (i==0))
		 	expand=true;
		else if ((expandResources==true) && (i==2))
		 	expand=true;
		else
			expand=false;
			
			
			
	 
            var tmpNode = new YAHOO.widget.TextNode(treemenu[i], tree.getRoot(), expand);
            // tmpNode.collapse();
            // tmpNode.expand();
            // buildRandomTextBranch(tmpNode);
            buildLargeBranch(tmpNode);
        }

       // Expand and collapse happen prior to the actual expand/collapse,
       // and can be used to cancel the operation
       tree.subscribe("expand", function(node) {
              YAHOO.log(node.index + " was expanded", "info", "example");
              // return false; // return false to cancel the expand
           });

       tree.subscribe("collapse", function(node) {
              YAHOO.log(node.index + " was collapsed", "info", "example");
           });

       // Trees with TextNodes will fire an event for when the label is clicked:
       tree.subscribe("labelClick", function(node) {
              
              YAHOO.log(node.index + " label was clicked", "info", "example");
             		if(node.label=='Introduction' && node.index==2)
					{
						location.href='/static/advertiser';
					}
             		else if(node.label=='WAP Advertising' && node.index==3)
					{
						location.href='/static/wap_advertising';
					}
					else if(node.label=='SMS Advertising' && node.index==4)
					{
						location.href='/static/sms_advertising';
					}
					else if(node.label=='Targeting' && node.index==5)
					{
						location.href='/static/targeting';
					}
					else if(node.label=='Why ZestADZ?' && node.index==6)
					{
						location.href='/static/why_zestadz_advertiser';
					}

					else if(node.label=='Analytics & Reporting' && node.index==7)
					{
						location.href='/static/analytics_reporting';
					}

					else if(node.label=='Landing Page Tool' && node.index==8)
					{
						location.href='/static/landing_pages_tool';
					}
					else if(node.label=='Case Studies' && node.index==9)
					{
						location.href='/static/case_studies';
					}
					else if(node.label=='Managed Campaigns' && node.index==10)
					{
						location.href='/static/managed_campaigns';
					}
					else if(node.label=='Ad Formats' && node.index==11)
					{
						location.href='/static/ad_formats';
					}
					else if(node.label=='Advertiser Analytics' && node.index==12)
					{
						location.href='/static/advertiser_analytics';
					}

					else if(node.label=='FAQs' && node.index==13)
					{
						location.href='/static/advertiser_faqs';
					}
				//pubisher setion link
					else if(node.label=='Introduction' && node.index==15)
					{
						location.href='/static/publisher';
					}
					else if(node.label=='Mobile Site Monetization' && node.index==16)
					{
						location.href='/static/mobile_site_monetization';
					}
					else if(node.label=='Mobile App Monetization' && node.index==17)
					{
						location.href='/static/mobile_app_monetization';
					}
					else if(node.label=='Facebook Mobile' && node.index==18)
					{
						location.href='/static/facebook';
					}
					else if(node.label=='Static Pages Integration' && node.index==19)
					{
						location.href='/static/static_pages_integration';
					}
					else if(node.label=='XML AD Integration' && node.index==20)
					{
						location.href='/static/xml_ad_integration';
					}
					else if(node.label=='XML Reports' && node.index==21)
					{
						location.href='/static/xml_reports';
					}
					else if(node.label=='List of Ads as XML' && node.index==22)
					{
						location.href='/static/list_ads_xml';
					}
					else if(node.label=='Multiple Ads' && node.index==23)
					{
						location.href='/static/multiple_ads';
					}

					else if(node.label=='Zest Points' && node.index==24)
					{
						location.href='/static/zest_points';
					}
					else if(node.label=='AD Control' && node.index==25)
					{
						location.href='/static/ad_control';
					}
					else if(node.label=='Email Alerts' && node.index==26)
					{
						location.href='/static/email_alerts';
					}
					else if(node.label=='Why ZestADZ?' && node.index==27)
					{
						location.href='/static/why_zestadz_publisher';
					}
					else if(node.label=='Reporting' && node.index==28)
					{
						location.href='/static/reporting';
					}
					else if(node.label=='Publisher Analytics' && node.index==29)
					{
						location.href='/static/publisher_analytics';
					}
					else if(node.label=='Android SDK' && node.index==30)
					{
						location.href='/static/android_sdk';
					}
					else if(node.label=='iPhone SDK' && node.index==31)
					{
						location.href='/static/iphone_sdk';
					}
					else if(node.label=='House Campaigns' && node.index==32)
					{
						location.href='/static/house_campaigns';
					}

					else if(node.label=='FAQs' && node.index==33)
					{
						location.href='/static/publisher_faqs';
					}
					// Resources links 
					else if(node.label=='Blog' && node.index==35)
					{
						location.href='/static/blogs';
					} 			 
					else if(node.label=='Presentations' && node.index==36)
					{
						location.href='/static/presentation';
					} 				  
					else if(node.label=='Content Guidelines' && node.index==37)
					{
						location.href='/static/content_guidelines';
					}
					else if(node.label=='Downloads' && node.index==38)
					{
						location.href='/static/downloads';
					}
           });
	   
	
	   
		//The tree is not created in the DOM until this method is called:
        tree.draw();
    }

	//function builds 10 children for the node you pass in:
    function buildLargeBranch(node) {
        if (node.depth < 25) {
            YAHOO.log("buildRandomTextBranch: " + node.index, "info", "example");
			if(node.label=='Advertisers'){
				subnode=["Introduction", "WAP Advertising", "SMS Advertising", "Targeting", "Why ZestADZ?", "Analytics & Reporting", "Landing Page Tool", "Case Studies", "Managed Campaigns", "Ad Formats", "Advertiser Analytics", "FAQs"];
			}
			else if(node.label=='Publishers'){
			subnode=["Introduction", "Mobile Site Monetization", "Mobile App Monetization", "Facebook Mobile", "Static Pages Integration", "XML AD Integration", "XML Reports", "List of Ads as XML", "Multiple Ads", "Zest Points", "AD Control", "Email Alerts", "Why ZestADZ?", "Reporting", "Publisher Analytics", "Android SDK", "iPhone SDK", "House Campaigns", "FAQs"];
			}
			else if(node.label=='Resources'){
				subnode=["Blog", "Presentations", "Content Guidelines", "Downloads"];
			}				
			
            for ( var i = 0; i < 25; i++ ) {
			
                new YAHOO.widget.TextNode(subnode[i], node, false);
            }
        }
    }

	//Add an onDOMReady handler to build the tree when the document is ready
    YAHOO.util.Event.onDOMReady(treeInit);

})();