// JavaScript Document
//var Casto={};
Casto.directory={
		active:false,
		current:0,
		data:'',
		preAction:'',
		offsets:[0,0],
		popup:false,
		data_html:'<div class="popup-saleslinks"></div>\
			<h3></h3>\
			<div class="inner-text">\
				<em></em>\
				<div class="popup-image"></div>\
				<span></span>\
				<div class="popup-eventlinks"></div>\
				<div class="popup-links"></div>\
			</div>',
		init:function() {
			this.offsets = $('directory-map').cumulativeOffset();
			
			$$('#retailerList li a').each(
				function(ob) {
					var id=ob.id.split('-')[2];
					if(ob.readAttribute('rel') && ob.readAttribute('rel')!="") {
						$(document.body).insert('<div class="pointer" id="retailer-pointer-'+id+'" rel="category-'+ob.readAttribute('rel2')+'"><div class="inner">&nbsp;</div></div>');
						var offsets = ob.readAttribute('rel').split(",");
						offsets[0]=parseInt(offsets[0])+Casto.directory.offsets[0]-6;
						offsets[1]=parseInt(offsets[1])+Casto.directory.offsets[1]-22;
						$('retailer-pointer-'+id+'').setStyle('left:'+offsets[0]+'px;top:'+offsets[1]+'px;').observe('click',ob.onclick).setOpacity(0);
						ob.observe('mouseover',
							function() {
								if(!Casto.directory.popup) {
									$$('.pointer-hold','.pointer').each(
										function(oob) {
											oob.setOpacity(0).show().className="pointer";
										}
									);
									var id=this.id.split('-')[2];
									$('retailer-pointer-'+id+'').setOpacity(1).show().className="pointer-hold";
									var oob=$('retailer-pointer-'+id);
									var obj_offsets = oob.cumulativeOffset();
									//var myinfo = $('pointer-label-'+id);
									var myinfo = $$('.directory-pointer-info')[0];
									var top = obj_offsets[1]-26;
									myinfo.update($('retailer-name-'+id).innerHTML).setStyle('top:'+(top)+'px;left:'+((obj_offsets[0]+7)-(myinfo.getWidth()/2))+'px;').show();
								}
							}
						).observe('mouseout',
							function() {
								if(!Casto.directory.popup) {
									var id=this.id.split('-')[2];
									$('retailer-pointer-'+id+'').setOpacity(0).show().className="pointer";
									//$('pointer-label-'+id).hide();
									$$('.directory-pointer-info')[0].hide();
								}
							}
						);
					}
				}
			);
			$(document.body).insert('<div class="directory-pointer-info" style="display:none;"></div>');
			$$('#categoryList .category_menu li a').each(
				function(ob) {
					var id=ob.id.split('-')[2];
					if(ob.readAttribute('rel') && ob.readAttribute('rel')!="") {
						ob.observe('mouseover',
							function() {
								if(!Casto.directory.popup) {
									$$('.pointer-hold','.pointer').each(
										function(oob) {
											oob.setOpacity(0).show().className="pointer";
										}
									);
									var id=this.id.split('-')[2];
									$('retailer-pointer-'+id+'').setOpacity(1).show().className="pointer-hold";
									var oob=$('retailer-pointer-'+id);
									var obj_offsets = oob.cumulativeOffset();
									//var myinfo = $('pointer-label-'+id);
									var myinfo = $$('.directory-pointer-info')[0];
									var top = obj_offsets[1]-26;
									myinfo.update($('retailer-name-'+id).innerHTML).setStyle('top:'+(top)+'px;left:'+((obj_offsets[0]+7)-(myinfo.getWidth()/2))+'px;').show();
								}
							}
						).observe('mouseout',
							function() {
								if(!Casto.directory.popup) {
									var id=this.id.split('-')[2];
									$('retailer-pointer-'+id+'').setOpacity(0).show().className="pointer";
									//$('pointer-label-'+id).hide();
									$$('.directory-pointer-info')[0].hide();
								}
								
							}
						);
					}
				}
			);
			$$('#categoryList li a').each(
				function(ob) { 
					ob.observe('click',
						function() {			
							if(this.id.indexOf('retailer-category-')!=-1) {
								$$('#categoryList li a').each(
									function(obb) {
										obb.className="";
									}
								);			
							
								this.className="hold";							
								var id=this.id.split('-')[2];
								$('retailer-category-'+id).className="dropdown";
								
								//Code for shops list under category
								$$('.category_menu').each(
									function(obb) {							
										//if('shop-list-'+id == obb.id) {		//alert(id+"==="+obb.id);											
										//	obb.show();
										//}else{
											obb.hide();		
											obb.up('li').hide();
										//}
									}
								);		
								$('shop-list-'+id).show();
								$('shop-list-'+id).up('li').show();
								//End of code
								
								/*$$('.pointer-hold','.pointer').each(
									function(oob) {
										oob.setOpacity(0).hide().className="pointer";
										var id=oob.id.split('-')[2];
										//$('pointer-label-'+id).hide();
									}
								);*/
								//alert('.pointer[rel=category-'+id+']');
								/*$$('.pointer[rel=category-'+id+']','.pointer-hold[rel=category-'+id+']').each(
									function(oob) {
										var id=oob.id.split('-')[2];
										
										oob.setOpacity(1).show().className="pointer-hold";
										var obj_offsets = oob.cumulativeOffset();
										var myinfo = $('pointer-label-'+id);
										var top = obj_offsets[1]-26;
										//myinfo.update($('retailer-name-'+id).innerHTML).setStyle('top:'+(top)+'px;left:'+((obj_offsets[0]+10)-(myinfo.getWidth()/2))+'px;').show();
									}
								);*/
							}
						}
					);
				}
			);
			$$('.category_menu li').each(
				function(ob) {
					ob.observe('click',
						function() {
							//alert("Hi");
						}
					);
				}
			);
			$$('.pointer','.pointer-hold').each(
				function(ob) {
					ob.observe('mouseover',
						function(event) {
							if(!Casto.directory.popup) {
								$$('.pointer-hold','.pointer').each(
									function(oob) {
										oob.setOpacity(0).show().className="pointer";
									}
								);
								var id=this.id.split('-')[2];
								//$('retailer-pointer-'+id+'').setOpacity(1).show().className="pointer-hold";
								var obj_offsets = this.cumulativeOffset();
								var myinfo = $$('.directory-pointer-info')[0];
								//var myinfo = $('pointer-label-'+id);
								var top = obj_offsets[1]-26;
								myinfo.update($('retailer-name-'+id).innerHTML).setStyle('top:'+(top)+'px;left:'+((obj_offsets[0]+7)-(myinfo.getWidth()/2))+'px;').show();
							}
						}
					).observe('mousemove',
						function(event) {
							//var top = (Event.pointerY(event)-40);
							//var myinfo = $$('.directory-pointer-info')[0];
							//myinfo.setStyle('top:'+(top)+'px;left:'+(Event.pointerX(event)-(myinfo.getWidth()/2))+'px;').show();
						}
					).observe('mouseout',
						function() {
							if(!Casto.directory.popup) {
								var id=this.id.split('-')[2];
								$$('.directory-pointer-info')[0].hide();
								$('retailer-pointer-'+id+'').setOpacity(0).show().className="pointer";
							}
							//$('pointer-label-'+id).hide();
						}
					);
				}
			);
		},	
		rearrange:function() {
			this.offsets = $('directory-map').cumulativeOffset();
			
			$$('#retailerList li a').each(
				function(ob) {
					var id=ob.id.split('-')[2];
					if(ob.readAttribute('rel') && ob.readAttribute('rel')!="") {
						var offsets = ob.readAttribute('rel').split(",");
						offsets[0]=parseInt(offsets[0])+Casto.directory.offsets[0]-6;
						offsets[1]=parseInt(offsets[1])+Casto.directory.offsets[1]-22;
						$('retailer-pointer-'+id+'').setStyle('left:'+offsets[0]+'px;top:'+offsets[1]+'px;');
					}
					//var oob=$('retailer-pointer-'+id+'');
					//var obj_offsets = oob.cumulativeOffset();
					//var myinfo = $('pointer-label-'+id);
					//var top = obj_offsets[1]-26;
					//myinfo.update($('retailer-name-'+id).innerHTML).setStyle('top:'+(top)+'px;left:'+((obj_offsets[0]+7)-(myinfo.getWidth()/2))+'px;');
				}
			);
		},
		load:function(obj,url) {
			if(!$(obj)) {
				obj = 'retailer';
			}
			if($(obj)) {
				this.popup=true;
				this.current = $(obj);	
				var id=this.current.id.split('-')[2];
				$$('.pointer-hold','.pointer').each(
					function(oob) {
						oob.setOpacity(0).show().className="pointer";
					}
				);
				$$('.directory-pointer-info')[0].hide();
				if($('retailer-pointer-'+id+'')) {
					$('retailer-pointer-'+id+'').setOpacity(1).show().className="pointer-hold";
					var oob=$('retailer-pointer-'+id);
					var obj_offsets = oob.cumulativeOffset();
					//var myinfo = $('pointer-label-'+id);
					var myinfo = $$('.directory-pointer-info')[0];
					var top = obj_offsets[1]-26;
					myinfo.update($('retailer-name-'+id).innerHTML).setStyle('top:'+(top)+'px;left:'+((obj_offsets[0]+7)-(myinfo.getWidth()/2))+'px;').show();
				}
				
				Casto.directory.data= {loader:true};
				Casto.directory.show();
				setTimeout('Casto.directory.Ajax("'+url+'");',1000);
			}
		},
		show: function() {
				var section_height = 0;
				var offsets = this.current.cumulativeOffset();
				//var offsetScroll = document.viewport.getScrollOffsets();
				var offsetScroll = $$('.directory-page .left-nav1')[0].cumulativeScrollOffset();
				//alert(innerScroll[1]);
				offsets[0]-=(offsetScroll[0]);
				offsets[1]-=(offsetScroll[1]);
				var startoffset=new Array();					
				if(this.data['loader']==true) {
					$$('.casto-popup .pop-up-text')[0].update('<div class="popup-loader">&nbsp;</div>');
					$('popup').setOpacity(0).show().setStyle("position: fixed;");
					$$('#popup .casto-popup-border')[0].setStyle('height:'+this.current.getHeight()+'px;width:'+this.current.getWidth()+'px;');
					$$('.casto-popup-close')[0].hide().setStyle("position: fixed;");
					$('popup').setStyle('top:'+(offsets[1])+'px;left:'+offsets[0]+'px;')
				} else {					
					$$('.casto-popup .pop-up-text')[0].update(this.data_html);//
					$$('.casto-popup h3')[0].update(this.data['retailer_name']);
					//alert(this.data['location']);
					if(this.data['location']==null || this.data['location'].blank()){
						var locateNow = '';
					}else {
						var locateNow = '<strong>'+this.data['location']+',</strong> ';
					}					
					$$('.casto-popup .inner-text em')[0].update(locateNow+this.data['phone']+'<br />'+this.data['hours']);
					if(this.data['image'])
						$$('.casto-popup .inner-text .popup-image')[0].update(''+this.data['image']+'');
					else $$('.casto-popup .inner-text .popup-image')[0].hide();
					$$('.casto-popup .inner-text span')[0].update('<br>'+this.data['description']);
					if(this.data['website_url'] && this.data['website_url'].trim()!="")
						$$('.casto-popup .inner-text .popup-links')[0].update('<a href="'+this.data['website_url']+'" target="_BLANK">More Info...</a>');
					else
						$$('.casto-popup .inner-text .popup-links')[0].hide();

					//alert(this.data['event_data'].length);
					//var section_height = 0;
					var events_count=0;
					var sales_count=0;
					if(this.data['event_data'].length>0) {
						var sep = "";
						for(var j=0; j<this.data['event_data'].length; j++) {
							//if(this.data['event_data'][j]['section_type']==2) {
								sales_count++;
								$$('.casto-popup .popup-saleslinks')[0].insert('<h3><a href="'+this.data['event_data'][j]['event_link']+'">'+this.data['event_data'][j]['event_title']+'</a></h3><div><em>'+this.data['event_data'][j]['event_time']+'</em></div>');	
							//} else {
							//	events_count++;
							//	$$('.casto-popup .popup-eventlinks')[0].insert(sep+'<a href="'+this.data['event_data'][j]['event_link']+'"><strong>'+this.data['event_data'][j]['event_title']+'</strong></a>');	
							//	sep = ", ";
							//}
						}
					} 
					
					if(sales_count==0){
						$$('.casto-popup .popup-saleslinks')[0].hide();
					}
					if(events_count==0){
						$$('.casto-popup .popup-eventlinks')[0].hide();
					}
					
				}
				
			
				$$('.casto-popup .pop-up-text')[0].setOpacity(0).show();
				var height = $$('.casto-popup .pop-up-text')[0].getHeight();
				
				//$('popup').hide();
				$$('#popup .pop-up-text').each(function(ob) {
					ob.show().setOpacity(0).appear({duration:0.3,delay:1});
				});
				//alert(document.viewport.getHeight());
				startoffset[0] = (document.viewport.getWidth()/2)-213;
				startoffset[1] = (document.viewport.getHeight()/2)-((height-30)/2);
				if(startoffset[1]<15) {
					startoffset[1]=15+offsetScroll[1];
					$('popup').setStyle("position: absolute;");
					$$('.casto-popup-close')[0].setStyle("position: absolute;");
				}
				if(!this.data['loader']) {
					$$('.casto-popup-close')[0].setStyle('top:'+(startoffset[1]-15)+'px;left:'+(startoffset[0]+426)+'px;').appear({duration:0.3,delay:1});
				}
				$('popup').appear({duration:0.3}).morph('top:'+(startoffset[1])+'px;left:'+(startoffset[0])+'px;',{duration:1});
				$$('#popup .casto-popup-border')[0].morph('height:'+(height-10)+'px;width:426px;',{duration:1});
			
		},
		hideInner: function() {
			$$('.casto-popup .pop-up-text')[0].fade({duration:0.3});
		},
		hide: function() {
			this.popup=false;
			$$('#popup .pop-up-text','.casto-popup-close').each(function(ob) {
				ob.fade({duration:0.2});
			});
			var offsets = this.current.cumulativeOffset();
			//var offsetScroll = document.viewport.getScrollOffsets();
			var offsetScroll = $$('.directory-page .left-nav1')[0].cumulativeScrollOffset();
			//alert('height:'+(this.current.getHeight()-5)+'px;width:'+(this.current.getWidth()-5)+'px;');
			offsets[0]-=offsetScroll[0];
			offsets[1]-=offsetScroll[1];
			$('popup').morph('top:'+(offsets[1]-5)+'px;left:'+(offsets[0]-5)+'px;',{delay:0.2,duration:0.5}).fade({delay:0.6,duration:0.3});
			$$('#popup .casto-popup-border')[0].morph('height:'+(this.current.getHeight()-5)+'px;width:'+(this.current.getWidth()-5)+'px;',{delay:0.2,duration:0.5})
		},
		Ajax: function(url) {
			callAjaxActive = true;
			//return;
			new Ajax.Request(url, {
				method:"GET",
				parameters: "",
				onSuccess: function(retailer){ 
					var responseArray = retailer.responseText.evalJSON();	
					if(responseArray['response']['status']==true) {
						if(responseArray['response']['valid']) {  
							Casto.directory.data= responseArray['response']['value'];
							Casto.directory.data.loader=false;
							Casto.directory.hideInner();
							setTimeout('Casto.directory.show();',300);
						} else {
							alert('Invalid Request! Please try again later.');
							setTimeout('Casto.directory.hide();',300);
						}
					}
				},
				onFailure: function(){ 
					
				}
			});
		},
		debug:function(str) {
			//$('debug').value+=str;
			//$('debug').scrollTop=5000;
		}
	};
function switch_tab(id) {
	if(!Casto.directory.popup) {
		if(id=='category') {
			$('retailerList').hide();
			$('categoryList').show();	
			$('retailer').removeClassName('current');	
			$('category').addClassName('current');
			$$('.category_menu').each(
				function(obb) {			
					obb.hide();	
					obb.up('li').hide();
				}
			);		
		} else  {
			$('categoryList').hide();	
			$('retailerList').show();
			//$('category').writeAttribute('class','');
			//$('retailer').writeAttribute('class','current');
			$('category').removeClassName('current');	
			$('retailer').addClassName('current');
		}
		$$('.pointer-hold','.pointer').each(
			function(oob) {
				oob.setOpacity(0).show().className="pointer";
				//var id=oob.id.split('-')[2];
				//$('pointer-label-'+id).hide();
			}
		);
		$$('.directory-pointer-info')[0].hide();
		$$('#categoryList li a').each(
			function(obb) {
				obb.className="";
			}
		);
	}
}
