// JavaScript Document
	//reviews plugin for cosmores clients
	(function($){
		$.fn.reviews = function(options){
			var reg = /^\s+$/;
			var defaults = {
				url:"http://www.cosmores.com/reviews_json.php",
				hotel_id:"",
				overall_path:"",
				location_path:"",
				staff_path:"",
				services_path:"",
				clean_path:"",
				comfort_path:"",
				value_path:"",
				width:"",
				height:"",
				loader_path:"http://www.cosmores.com/layout/indicator_big.gif",
				loader_visible:false,
				loader_background_color:"#000000",
				loader_background_opacity:0.8,
				complete:"",
				show_total_reviews:true,
				align_total_reviews:"right",
				show_link_reviews:true,
				align_link_reviews:"left"
			}
			var options = $.fn.extend(defaults,options);
			
			if(options.hotel_id=="" || reg.test(options.hotel_id)){
				alert("hotel id cannot be empty(jQuery.reviews plugin)");
				return false;
			}
			//var finalUrl = options.url+options.hotel_id;
			
			return this.each(function(){
				obj = $(this);
				var divBackgroundLoader = null;
				var imgLoader = null;
				if(options.width!=""){
					obj.get(0).style.width=options.width+"px";
				}
				if(options.height!=""){
					obj.get(0).style.height=options.height+"px";
				}
				if(options.loader_visible){
					imgLoader = new Image();
					imgLoader.src = options.loader_path;
					
					var offset = obj.offset();
					var objTop = offset.top;
					var objLeft = offset.left;
					var objWidth = $(obj).width();
					var objHeight = $(obj).height();
					
					divBackgroundLoader = document.createElement("div");
					divBackgroundLoader.style.cssText = "position:absolute;top:"+objTop+"px;left:"+objLeft+"px;width:"+objWidth+"px;height:"+objHeight+"px;moz-opacity:"+options.loader_background_opacity+";opacity:"+options.loader_background_opacity+";filter:alpha(opacity="+(options.loader_background_opacity*100)+");z-index:1000;background-color:"+options.loader_background_color+";";
					
					imgLoader.style.cssText = "position:absolute;top:"+(objTop+5)+"px;left:"+(objLeft+5)+"px;z-index:1010;";
					document.body.appendChild(divBackgroundLoader);
					document.body.appendChild(imgLoader);
				}
				//alert(Math.ceil(Math.random()*10));
				var time = new Date().getTime()+Math.ceil((Math.random()*1000));
				$.getScript(options.url+"?hotel_id="+options.hotel_id+"&t="+time,function(){
					if(divBackgroundLoader!=null){
						document.body.removeChild(divBackgroundLoader);
						divBackgroundLoader=null;
					}
					if(imgLoader!=null){
						document.body.removeChild(imgLoader);
						imgLoader = null;
					}
					if(window['cosmores.reviews.'+time]!=null){
						var jsonResponse = window['cosmores.reviews.'+time];
						if(!$.isFunction(options.complete)){
							if(jsonResponse.answer=="true"){
								if(options.overall_path==""){
									options.overall_path = jsonResponse.overall_img;
								}
								if(options.location_path==""){
									options.location_path = jsonResponse.location_img;
								}
								if(options.staff_path==""){
									options.staff_path = jsonResponse.staff_img;
								}
								if(options.services_path==""){
									options.services_path = jsonResponse.services_img;
								}
								if(options.clean_path==""){
									options.clean_path = jsonResponse.clean_img;
								}
								if(options.comfort_path==""){
									options.comfort_path = jsonResponse.comfort_img;
								}
								if(options.value_path==""){
									options.value_path = jsonResponse.value_img;
								}
								var overallImg = new Image();
								overallImg.src = options.overall_path;
								locationImg = new Image();
								locationImg.src = options.location_path;
								var staffImg = new Image();
								staffImg.src = options.staff_path;
								var servicesImg = new Image();
								servicesImg.src = options.services_path;
								var cleanImg = new Image();
								cleanImg.src = options.clean_path;
								var comfortImg = new Image();
								comfortImg.src = options.comfort_path;
								var valueImg = new Image();
								valueImg.src = options.value_path;
								
								var outputHtml = "";
								outputHtml+="<table cellpadding='0' cellspacing='4' border='0' width='100%'>";
									outputHtml+="<tr valign='middle' align='center'>";
										outputHtml+="<td><img src='"+overallImg.src+"' border='0' alt='overall' /><\/td>";
										outputHtml+="<td align='left' class='overallpoint'>Overall&nbsp;<\/td>";
										outputHtml+="<td><img src='"+jsonResponse['point_'+jsonResponse.overall]+"' alt='overall point' border='0' /><\/td>";
										outputHtml+="<td class='overallpoint'>"+jsonResponse.overall+"<\/td>";
									outputHtml+="<\/tr>";
									outputHtml+="<tr valign='middle' align='center'>";
										outputHtml+="<td><img src='"+locationImg.src+"' border='0' alt='location' /><\/td>";
										outputHtml+="<td align='left' class'locationpoint'>Location&nbsp;<\/td>";
										outputHtml+="<td><img src='"+jsonResponse['point_'+jsonResponse.location]+"' alt='location point' border='0' /><\/td>";
										outputHtml+="<td class='locationpoint'>"+jsonResponse.location+"<\/td>";
									outputHtml+="<\/tr>";
									outputHtml+="<tr valign='middle' align='center'>";
										outputHtml+="<td><img src='"+staffImg.src+"' border='0' alt='staff' /><\/td>";
										outputHtml+="<td align='left' class='staffpoint'>Staff&nbsp;<\/td>";
										outputHtml+="<td><img src='"+jsonResponse['point_'+jsonResponse.staff]+"' alt='staff point' border='0' /><\/td>";
										outputHtml+="<td class='staffpoint'>"+jsonResponse.staff+"<\/td>";
									outputHtml+="<\/tr>";
									outputHtml+="<tr valign='middle' align='center'>";
										outputHtml+="<td><img src='"+servicesImg.src+"' border='0' alt='services' /><\/td>";
										outputHtml+="<td align='left' class='servicespoint'>Services&nbsp;<\/td>";
										outputHtml+="<td><img src='"+jsonResponse['point_'+jsonResponse.services]+"' alt='services point' border='0' /><\/td>";
										outputHtml+="<td class='servicespoint'>"+jsonResponse.services+"<\/td>";
									outputHtml+="<\/tr>";
									outputHtml+="<tr valign='middle' align='center'>";
										outputHtml+="<td><img src='"+cleanImg.src+"' border='0' alt='clean' /><\/td>";
										outputHtml+="<td align='left' class='cleanpoint'>Clean&nbsp;<\/td>";
										outputHtml+="<td><img src='"+jsonResponse['point_'+jsonResponse.clean]+"' alt='clean point' border='0' /><\/td>";
										outputHtml+="<td class='cleanpoint'>"+jsonResponse.clean+"<\/td>";
									outputHtml+="<\/tr>";
									outputHtml+="<tr valign='middle' align='center'>";
										outputHtml+="<td><img src='"+comfortImg.src+"' border='0' alt='comfort' /><\/td>";
										outputHtml+="<td align='left' class='comfortpoint'>Comfort&nbsp;<\/td>";
										outputHtml+="<td><img src='"+jsonResponse['point_'+jsonResponse.comfort]+"' alt='comfort point' border='0' /><\/td>";
										outputHtml+="<td class='comfortpoint'>"+jsonResponse.comfort+"<\/td>";
									outputHtml+="<\/tr>";
									outputHtml+="<tr valign='middle' align='center'>";
										outputHtml+="<td><img src='"+valueImg.src+"' border='0' alt='value for money' /><\/td>";
										outputHtml+="<td align='left' class='valuepoint'>Value for money&nbsp;<\/td>";
										outputHtml+="<td><img src='"+jsonResponse['point_'+jsonResponse.value]+"' alt='value point' border='0' /><\/td>";
										outputHtml+="<td class='valuepoint'>"+jsonResponse.value+"<\/td>";
									outputHtml+="<\/tr>";
								outputHtml+="<\/table>";
								
								if(options.show_total_reviews || options.show_link_reviews ){
									outputHtml+="<table cellpadding='0' cellspacing='4' border='0' width='100%'><tr>";
										if(options.show_link_reviews ){
											outputHtml+="<td align='"+options.align_link_reviews+"'><a href='https://"+jsonResponse.subdomain+".cosmores.com#reviews' target='_blank'>View All<\/a><\/td>";
										}
										if(options.show_total_reviews){
											outputHtml+="<td align='"+options.align_total_reviews+"'><span >Total: <\/span><span id='spanTotalReviews'>"+jsonResponse.total+"<\/span>";
										}
									outputHtml+="<\/tr><\/table>";
								}
								
								$(obj).html(outputHtml);
								//obj = null;
							}else if(jsonResponse.answer=="false"){
								
							}else{
								
							}
						}else{
							if(jQuery.isFunction(options.complete)){
								options.complete(jsonResponse);
							}
						}
						window['cosmores.reviews.'+time] = null;
						jsonResponse = null;
						obj = null;
					}else{
					}
				});
				
				return false;
			});
		};
	})(jQuery);