/**
	COMMON FOR GOOGLE MAP ELEMENT 
	res/store-locator/common.js

**/	
	/**Function createMarker make marker at point with icon and return  GMarker
	 *@param  point -point of Marker at Map
	 *@param icon of Marker - by defaut takes  G_DEFAULT_ICON
	 *@return GMarker   
	**/
	function createMarker(point, icon){
		if (icon==null) icon = G_DEFAULT_ICON;
		var marker = new GMarker(point, icon);
		return marker;
	}

	
	
	/**Function wc_get_locator_icon create custom icon for Marker
	 *@param letter - its letter of icon "res/store-locator/icons/icon_green[letter].png"
	 *@return GIcon  
	**/
	 function wc_get_locator_icon_index (index)
	{
	
		var wc_gSmallIcon = new GIcon();
		wc_gSmallIcon.image = wc_array_Icon_letters[index];
		wc_gSmallIcon.shadow = "res/store-locator/icons/mm_20_shadow.png";
		wc_gSmallIcon.iconSize = new GSize(24, 38);
		wc_gSmallIcon.shadowSize = new GSize(22, 20);
		wc_gSmallIcon.iconAnchor = new GPoint(6, 20);
		wc_gSmallIcon.infoWindowAnchor = new GPoint(5, 1);
		
		return wc_gSmallIcon;
		
	}
	function is_zip(str) {
	var regex = new RegExp("^[0-9]{5}$", ""); 
	return 	regex.test(str);
}
function apply_css_to_head(css_file_path){
	var cssElement = document.createElement('link');
		cssElement.setAttribute('href', css_file_path);
		cssElement.setAttribute('rel', 'stylesheet');
		cssElement.setAttribute('type', 'text/css');
		document.getElementsByTagName('head')[0].appendChild(cssElement);
}

function js_direction_link (index) {
	return "javascript:wc_set_directions("+index+")";
}
function js_wc_open_markerwindow (index) {
	return "javascript:wc_open_markerwindow("+index+")";
}
function js_wc_show_showMapBlowup(index) {
	return "javascript:wc_show_showMapBlowup("+index+")";
}
	/**HTML TEMPLATES**/
	
function HTML_address_template(result){
	var address_div = new DivElement("store-locator-address");
	var sub_coma="";
	if ( result.region.length > 0)sub_coma=", ";
		var phones = "";
		var phones_len = result.phoneNumbers.length;
		
		for (var i = 0; i < phones_len; i++){
			phones = phones + result.phoneNumbers[i].number;
			if (i!= phones_len-1)phones=phones+", ";	
		}
		
		address_div.append(new DivElement("store-locator-street", result.streetAddress));
		address_div.append(new DivElement("store-locator-city", result.city+sub_coma));
		address_div.append(new DivElement("store-locator-region", result.region));
		address_div.append(new DivElement("store-locator-phone", phones));
		return address_div

}	

function HTML_bubble_info(title, result, index) {

		var result_div  = new DivElement("store-locator-result");
		var title_div   = new DivElement("store-locator-title", title); 
		var directions_div  = new DivElement("store-locator-directions");
			
		result_div.append(new DivElement("store-locator-title", title));
		result_div.append(HTML_address_template(result));
		directions_div.append(new LinkElement("Get directions",
												  js_direction_link(index),
  													"store-locator-title directions"));
			result_div.append(directions_div);
		return result_div;
}
function HTML_info_address(address_store) {
	return  new DivElement("store-locator-address_div-info-box", address_store);


}
function HTML_info_box(title, mid_title, address_store, index, src) {
/* 	<div class="store-locator-info-box">
	<table class="store-locator-table">
        <tr class="store-locator-result_info_box" >	
			<td class="store-locator-dirs-info-box-left">	
				<a> <img/> </a> 
			</td>
			<td class="store-locator-dirs-info-box-mid">
				<a> From:/To: </a>
			</td>
			<td class="store-locator-dirs-info-box-right">
				<div class="store-locator-title_div-info-box"> Title </div>
				<address />
			</td>
		</tr>
	</table>
	</div>
		 */
		var result_div  = new DivElement("store-locator-info-box");
		
		var title_div  = new DivElement("store-locator-title_div-info-box", title );
		
		var info_result = new TableElement("store-locator-info-box-table");
		
		var result_info_box_tr  =  new TrElement("store-locator-result_info_box"); 
		info_result.append(result_info_box_tr);
		var left_td  =  new TdElement("store-locator-dirs-info-box-left");
		var mid_td  =  new TdElement("store-locator-dirs-info-box-mid");
		var right_td  =  new TdElement("store-locator-dirs-info-box-right");
		
		result_info_box_tr.append(left_td);
		result_info_box_tr.append(mid_td);
		result_info_box_tr.append(right_td);
		result_info_box_tr.set_onclick(wc_show_showMapBlowup, index);
		
		var link_image = new LinkElement (null,"javascript:;", 
											"store-locator-number-link", src);
		var link_mid_title = new LinkElement (mid_title,"javascript:;", 
											"store-locator-mid_title", null);
		var link_title = new LinkElement (null,"javascript:;", 
											"store-locator-link_title", null);
											
		left_td.append(link_image);
		mid_td.append(link_mid_title);
		
		link_title.append(title_div);
		link_title.append(address_store);
		right_td.append(link_title);
		result_div.append(info_result);
		
		//this.resul_div=result_div;
		return result_div;
}



function HTML_inline_info(title, result, index) {
		var info_window = new DivElement("store-locator-infowindow");
		var image_div   = new DivElement("store-locator-image-div");
		var directions_div  = new DivElement("store-locator-directions");
		var title_div_a   = new DivElement("store-locator-title-directions"); 
		var marker_a1 = new LinkElement(null, js_wc_open_markerwindow(index),"store-locator-image-link", 
										"res/store-locator/icons/icon_blue"+(index+1)+".jpg" );
		
		directions_div.append(new LinkElement("driving directions", js_direction_link(index),"store-locator-title-directions"));
			
		
		var result_div  = new DivElement("store-locator-result");
		var sep_div  = new DivElement("store-locator-separator", "-");
		var title_div   = new DivElement("store-locator-title"); 
		
		title_div_a.append(new LinkElement(title,js_wc_open_markerwindow(index), "store-locator-title"));
		title_div.append(title_div_a);
		title_div.append(sep_div);
		title_div.append(directions_div);
			
		result_div.append(title_div);
		result_div.append(HTML_address_template(result));

		image_div.append(marker_a1);
		
		info_window.append(image_div);
		info_window.append(result_div);
		
		return info_window;
		
}


function HTML_template_step (result , index) {
		/**result
			nummber


		**/
		var result_div  =  new TrElement("store-locator-dir-result"); 
		var number_div  =  new TdElement("store-locator-dir-number");
		var description_div  =  new TdElement("store-locator-dir-description");//,result.getDescriptionHtml() );
		var distance_div  =  new TdElement("store-locator-dir-distance", result.getDistance().html);
		
		var link1_ = new LinkElement (index+".","javascript:;", 
											"store-locator-number-link", null);
		var link2_ = new LinkElement (result.getDescriptionHtml(),"javascript:;", 
											"store-locator-number-link", null);
		result_div.set_onclick(wc_show_showMapBlowup, index);
	
		description_div.append(link2_);
		number_div.append(link1_);
		result_div.append(number_div);
		result_div.append(description_div);
		result_div.append(distance_div);
		return result_div;
	
	}








String.prototype.ltrim = function() {
    return this.replace(/^\s+/, ''); }

String.prototype.rtrim = function() {
    return this.replace(/\s+$/, ''); }

String.prototype.trim = function() {
    return this.ltrim().rtrim(); } 
	
function preloadImages(src) { //v3.0
						var image =  new Image();
						image.src=src;
						return image;
}
