/**
 * @author zsolthorvath
 */

if (!this.storelist) {
	storelist = {};
	storelist.mainmenu = new slMenuItem();
	
	//Add data
	var americas = storelist.mainmenu.addMenuItem('americas', 'Americas');
	var la = americas.addMenuItem('losangeles', 'Los Angeles');	
	la.addStore({name: 'Bleu', address: '454 S La Brea Ave<br/>Los Angeles CA 90036', phone: '+1.323.939.2228', url:'http://www.bleuclothing.com'});
	la.addStore({name: 'Bleu', address: '960-10 S Westlake<br/>Westlake Village, CA 91361', phone: '+1.805.496.4578', url:'http://www.bleuclothing.com'});
	//la.addStore({name: 'Dewey', address: '1108 Abbot Kinney Blvd<br/>Venice, CA 90291', phone: '+1.310.392.2974'});
	la.addStore({name: 'Diane Merrick', address: '7407 Beverly Blvd<br/>Los Angeles, CA 90036', phone: '+1.323.930.0400', url:'http://www.dianemerrickshop.com'});
	la.addStore({name: 'Diavolina', address: '156 S Robertson Blvd<br/>Los Angeles, CA 90048', phone: '+1.310.550.1341'});
	la.addStore({name: 'Evolve 108', address: '108 Webster St<br/>Monterrey, CA 93940', phone: '+1.831.648.1080', url:'http://www.evolve108.com'});
	la.addStore({name: 'Milk &amp; Honey', address: '348 South Cedros St<br/>Solana Beach, CA 92075', phone: '+1.858.350.1300 '});
	la.addStore({name: 'Oou', address: '1764 N. Vermont Ave<br/>Los Angeles, CA 90027', phone: '+1.323.665.6263'});
	la.addStore({name: 'Picket Fences', address: '214 N. Larchmont Blvd<br/>Los Angeles, CA 90004', phone: '+1.323.467.2140'});
	la.addStore({name: 'Planet Blue', address: '800 14th St<br/>Santa Monica, CA 90403', phone: '+1.310.394.0135', url:'http://www.shopplanetblue.com'});
	la.addStore({name: 'Planet Blue', address: '409 N. Beverly Dr <br/>Beverly Hills, CA 90210', phone: '+1.310.385.0557', url:'http://www.shopplanetblue.com'});
	la.addStore({name: 'Planet Blue', address: '2940 Main St<br/>Venice, CA 90405', phone: '+1.310.396.1767', url:'http://www.shopplanetblue.com'});
	la.addStore({name: 'Planet Blue', address: '3835 Cross Creek Road<br/>Malibu, CA 90265', phone: '+1.310.317.9975', url:'http://www.shopplanetblue.com'});
	la.addStore({name: 'Satine', address: '8117 West 3rd St<br/>Los Angeles, CA 90048', phone: '+1.323.655.2142', url:'http://www.satineboutique.com'});
	la.addStore({name: 'Sweet as Honey', address: '645 Barrell Road<br/>Hillsborough, CA 94010', phone: '+1.650.281.9685'});
	la.addStore({name: 'Sydney Michelle', address: '2492 Beverly Glen Circle<br/>Los Angeles, CA 90077', phone: '+1.310.470.8781', url:'http://www.sydneymichelle.com'});
	la.addStore({name: 'Sydney Michelle', address: 'Marine Ave<br/>Balboa Island, CA 92662', phone: '+1.949.673.2150', url:'http://www.sydneymichelle.com'});
	//la.addStore({name: 'Vamp', address: '2841 Hyperion Ave<br/>Los Angeles CA 90039', phone: '+1.323.662.1150'});
	var ny = americas.addMenuItem('newyork', 'New York');	
	ny.addStore({name: 'Blue Bag', address: '266 Elizabeth St #A<br/>New York, NY 10012', phone: '+1.212.966.8566'});
	//ny.addStore({name: 'Takashimaya New York', address: ' 693 Fifth Avenue<br/>New York, NY 10022', phone: '+1.800.753.2038 and +1.212.350.0100', url:'http://www.takashimaya.co.jp'});
	ny.addStore({name: 'Intermix Boutique', url: 'http://www.intermixonline.com'});

	var seattle = americas.addMenuItem('seattle', 'Seattle');	
	seattle.addStore({name: 'Strut Store', address: '7511 Greenwood Ave<br/>N. Seattle, WA 98103', phone: '+1.206.2971907', url:'http://www.strutseattle.com'});

	var europe = storelist.mainmenu.addMenuItem('europe', 'Europe');
	var spain = europe.addMenuItem('spain', 'Spain');
	var valencia = spain.addMenuItem('valencia','Valencia');
	valencia.addStore({name: 'Don Carlos', address: 'Avda. Marques de Sotelo 4<br/>46002 Valencia', phone: '+96.351.6550'})
	valencia.addStore({name: 'Mania', address: 'Calle Roger de Lauria 14<br/>46002 Valencia', phone: '+96.352.2013'})
	var madrid = spain.addMenuItem('madrid','Madrid');
	madrid.addStore({name: 'Mania', address: 'Calle Serrano 92<br/>28006 Madrid', phone: '+91.435.8267'})

	var middleeast = storelist.mainmenu.addMenuItem('middleeast', 'Middle East');
	var saudi = middleeast.addMenuItem('saudiarabia','Saudi Arabia');
	var riyahd = saudi.addMenuItem('riyahd','Riyahd');
	riyahd.addStore({name: 'The Guest Room', address: 'Prince Mamdooh Bin AbdulAziz Street<br/>Riyadh, Saudi Arabia', phone: '+966.504.206.916'});

	var asia = storelist.mainmenu.addMenuItem('asia', 'Asia');
	var japan = asia.addMenuItem('japan','Japan');
	var nationwidejapan = japan.addMenuItem('nationwidejapan','Nation wide');
	nationwidejapan.addStore({name: 'Takashimaya', url: 'http://www.takashimaya.co.jp'});
	var taiwan = asia.addMenuItem('taiwan','Taiwan');
	var taipei = taiwan.addMenuItem('taipei','Taipei');
	taipei.addStore({name: 'Dazzling Store', address: '4F, No 133, Da An Road, Sec 1<br/>Taipei, Taiwan 10685', phone: '+886.2.6617.6888'});
}

if (!this.wholesalelist) {
	wholesalelist = {};
	wholesalelist.mainmenu = new slMenuItem();
	
	//Add data
	var americas = wholesalelist.mainmenu.addMenuItem('w_americas', 'Americas');
	var la = americas.addMenuItem('w_losangeles', 'Los Angeles');	
	//la.addStore({name: 'ONE OCTOBER', address: '1504 McCollum Street<br>Los Angeles, CA 90026', email: 'sales@oneoctober.net', phone: '310.928.3245', fax: '310.943.2449', url:'http://www.oneoctober.net'});
	la.addStore({name: 'Noetic Showroom<br/>Melissa Cronkhite', address: '860 S. Los Angeles Street #612<br/>Los Angeles, CA 90014', email: 'melissa@noeticshowroom.com', phone: '213.614.7924', fax: '213.614.7925', url:'http://www.noeticshowroom.com'});
	var w_ny = americas.addMenuItem('w_newyork', 'New York');	
	w_ny.addStore({name: 'Noetic Showroom<br/>Dawn Stephens', address: '116 Elizabeth Street, 4th Floor<br/>NYC, NY 10013', email: 'dawn@noeticshowroom.com', phone: '212.966.0325', fax: '212.966.0326', url:'http://www.noeticshowroom.com'});

	var europe = wholesalelist.mainmenu.addMenuItem('w_europe', 'Europe');
	var spain = europe.addMenuItem('w_spain', 'Spain');
	var madrid = spain.addMenuItem('w_madrid','Madrid');
	madrid.addStore({name: 'C2SHOWROOM', address: 'Claudio Coello 46, S.1<br>28001 Madrid', email: 'info@c2showroom.es', phone: '+34 91 435 82 67', fax: '+34 91 435 54 54', url:'http://www.c2showroom.es'});

	var norway = europe.addMenuItem('w_norway', 'Norway');
	var oslo = norway.addMenuItem('w_oslo','Oslo');
	oslo.addStore({name: 'KAARE LUND AS', address: ' Kongensgate 24<br>0153 Oslo', phone: '+47 23310900', fax: '+4722334069', url:'http://www.kaarelund.no'});
}

function slMenuItem() {
	this.id = null;
	this.name = null;
	this.level = -1;
	
	this.children = new Array();
	
	this.addMenuItem = function(id, name) {
		var menuItem = new slMenuItem();
		menuItem.id = id;
		menuItem.name = name;
		menuItem.level = this.level +1;
		this.children.push(menuItem);
		
		return menuItem;
	}
	
	this.addStore = function(obj) {
		var store = new storeAddress();
		
		store.name = obj.name;
		store.address = obj.address;
		store.phone = obj.phone;
		store.fax = obj.fax;
		store.email = obj.email;
		store.url = obj.url;
		store.level = this.level+1;
		
		this.children.push(store);
	}
	
	this.hasChildren = function() {
		return this.children.length>0;
	}

	this.getMenuItem = function(id) {
		if (id == null)
			return null;
		else {
			//look for id
			var obj = null;
			for (var i=0; i<this.children.length; i++) {
				if (this.children[i].id == id)
					obj = this.children[i];
					
				if (obj == null && this.children[i].hasChildren()) {
					//look for children of children
					obj = this.children[i].getMenuItem(id);
				}
			}
			
			return obj;
		}
	}
}

function storeAddress() {
	this.name = null;
	this.address = null;
	this.phone = null;
	this.fax = null;
	this.email = null;
	this.url = null;
	this.level = -1;
	
	this.hasChildren = function() {
		return false;
	}

	
}

function writeStoreList(id) {
	//alert('Writing');
	var content;
	if (id == null) {
		var storelistElement = dojo.byId('storelist');
		//var storeList = document.getElementById('storelist');
		storelistElement.style.height = 500+'px';
		storelistElement.innerHTML = '\
		  <div align="center">\
		    <table width="650" cellpadding="0" cellspacing="8" class="tablazat">\
	          <tr align="left">\
	            <td>&nbsp;</td>\
	            <td align="right"><a href="javascript:closePopup();" class="tablazat-2" style="text-decoration:none "><strong>X</strong></a></td>\
	          </tr>\
	          <tr align="left">\
			  	<td colspan="2"><div id="slmenu"/></td>\
	          </tr>\
	          <tr align="left">\
	            <td width="197"><div style="display: hidden;vertical-align:top;height:400px;" class="whitebold" id="slsubmenu"/></td>\
	            <td width="524"><div style="display: hidden;vertical-align:top;height:400px;width:480px;overflow-x:hidden;overflow-y:auto;" id="sllist"/></td>\
	          </tr>\
	        </table>\
	      </div>\
		';
	
		//nothing selected
		content = '<table><tr>';
		for (var i=0; i<storelist.mainmenu.children.length; i++) {
			var item = storelist.mainmenu.children[i];
			content += '<td style="padding-right: 20px;"><div id="'+item.id+'" style="cursor:pointer;" onclick="writeStoreList(\''+item.id+'\')" >'+item.name+'</div></td>';
		}
		content += '</tr></table>';
		dojo.byId('slmenu').innerHTML = content;
		
		writeStoreList('americas');
		writeStoreList('losangeles');
	} else {
		var menuItem = storelist.mainmenu.getMenuItem(id);
		var listElement = dojo.byId('sllist');
		var subMenuElement = dojo.byId('slsubmenu');
		if (menuItem != null) {
			if (menuItem.level == 0) {
				listElement.style.display = 'none';
				subMenuElement.style.display = 'none';
				
				content = '<table>';
				var firstItemId = null;
				for (var i=0; i<menuItem.children.length; i++) {
					var item = menuItem.children[i];
					content += '<tr><td><div id="'+item.id+'" style="cursor:pointer;" onclick="writeStoreList(\''+item.id+'\')" >'+item.name+'</div></td></tr>';
					
					if (firstItemId == null)
					firstItemId = item.id;
				}
				content += '</table>';
				subMenuElement.innerHTML = content;
				
				subMenuElement.style.display = 'block';
				
				if (firstItemId != null)
					writeStoreList(firstItemId);
			} else {
				//Vertical list, but it is possible, that there are submenu items - at the moment supports up till 4 levels
				listElement.style.display = 'none';
				
				content = '<table>';
				for (var i=0; i<menuItem.children.length; i++) {
					var item = menuItem.children[i];
					content += '<tr><td><div>'; 
					
					if (item.id && item.hasChildren()) {
						//Menuitem
						content += '<div style="padding-bottom: 10px;" class="whitebold">'+item.name+'</div>';
						for (var j = 0; j < item.children.length; j++) {
							content += writeStoreAddress(item.children[j]);
						}
						
					} else {
							content += writeStoreAddress(item);
					}
					
					content += '</div></td></tr>';
				}
				content += '</table>';
				listElement.innerHTML = content;
				
				listElement.style.display = 'block';
				
			}	
		}
	}
}

function writeWholesaleList(id) {
	//alert('Writing');
	var content;
	if (id == null) {
		var wholesaleElement = dojo.byId('sales');
		//var wholesaleList = document.getElementById('wholesalelist');
		wholesaleElement.style.height = 500+'px';
		wholesaleElement.innerHTML = '\
		  <div align="center">\
		    <table width="650" cellpadding="0" cellspacing="8" class="tablazat">\
	          <tr align="left">\
	            <td>&nbsp;</td>\
	            <td align="right"><a href="javascript:closePopup();" class="tablazat-2" style="text-decoration:none "><strong>X</strong></a></td>\
	          </tr>\
	          <tr align="left">\
			  	<td colspan="2"><div id="wslmenu"/></td>\
	          </tr>\
	          <tr align="left">\
	            <td width="197"><div style="display: hidden;vertical-align:top;height:400px;" class="whitebold" id="wslsubmenu"/></td>\
	            <td width="524"><div style="display: hidden;vertical-align:top;height:400px;width:480px;overflow-x:hidden;overflow-y:auto;" id="wsllist"/></td>\
	          </tr>\
	        </table>\
	      </div>\
		';
	
		//nothing selected
		content = '<table><tr>';
		for (var i=0; i<wholesalelist.mainmenu.children.length; i++) {
			var item = wholesalelist.mainmenu.children[i];
			content += '<td style="padding-right: 20px;"><div id="'+item.id+'" style="cursor:pointer;" onclick="writeWholesaleList(\''+item.id+'\')" >'+item.name+'</div></td>';
		}
		content += '</tr></table>';
		dojo.byId('wslmenu').innerHTML = content;
		
		writeWholesaleList('w_americas');
		writeWholesaleList('w_losangeles');
	} else {
		var menuItem = wholesalelist.mainmenu.getMenuItem(id);
		var listElement = dojo.byId('wsllist');
		var subMenuElement = dojo.byId('wslsubmenu');
		if (menuItem != null) {
			if (menuItem.level == 0) {
				listElement.style.display = 'none';
				subMenuElement.style.display = 'none';
				
				content = '<table>';
				var firstItemId = null;
				for (var i=0; i<menuItem.children.length; i++) {
					var item = menuItem.children[i];
					content += '<tr><td><div id="'+item.id+'" style="cursor:pointer;" onclick="writeWholesaleList(\''+item.id+'\')" >'+item.name+'</div></td></tr>';
					
					if (firstItemId == null)
					firstItemId = item.id;
				}
				content += '</table>';
				subMenuElement.innerHTML = content;
				
				subMenuElement.style.display = 'block';
				
				if (firstItemId != null)
					writeWholesaleList(firstItemId);
			} else {
				//Vertical list, but it is possible, that there are submenu items - at the moment supports up till 4 levels
				listElement.style.display = 'none';
				
				content = '<table>';
				for (var i=0; i<menuItem.children.length; i++) {
					var item = menuItem.children[i];
					content += '<tr><td><div>'; 
					
					if (item.id && item.hasChildren()) {
						//Menuitem
						content += '<div style="padding-bottom: 10px;" class="whitebold">'+item.name+'</div>';
						for (var j = 0; j < item.children.length; j++) {
							content += writeStoreAddress(item.children[j]);
						}
						
					} else {
							content += writeStoreAddress(item);
					}
					
					content += '</div></td></tr>';
				}
				content += '</table>';
				listElement.innerHTML = content;
				
				listElement.style.display = 'block';
				
			}	
		}
	}
}

function writeStoreAddress(storeAddress){
	var content = '<div style="padding-bottom: 5px;">';
	content += '<div style="padding-bottom: 5px;" class="tablazat-2">' + storeAddress.name + '</div>';
	var br = '';
	if (storeAddress.address != null) {
		content += storeAddress.address;
		br = '<br/>';
	}
	if (storeAddress.phone != null)
		content += br+'Phone: '+storeAddress.phone;
	if (storeAddress.fax != null)
		content += br+'Fax: '+storeAddress.fax;
	if (storeAddress.email != null)
		content += br+'Email: '+storeAddress.email;
	if (storeAddress.url != null)
		content += br+'Web: <span style="cursor:pointer;" onclick="window.open(\''+storeAddress.url+'\')">'+storeAddress.url+'</span>';
	content += '</div>';	
	
	return content;
}
