// basic variables
//var reporturl = '';
var insert = '';
var reports = [];
var reviewF = '';
var ar = '';
var hd = '';
var rw = '';
var rt = '';
var eid = '';
var endaction = true;



$(document).ready(function(){
	eid = getID($("h1.summary").attr('title'));
	if($("form#reviewff").length > 0) {	
		activateReview();
		activateFavourite();
	} else {
		activateLoginF();
		activateLoginR();
	}
	
	activateReporting();
});

function activateReview() {
		reviewF = document.forms.reviewff;
		/*$("a#rown").click(function(me){
			me.preventDefault();
			clickAddR();
			return false;
		});*/
	
		// add a review
		$("form#reviewff").append("<br class='cb' />");
		$("form#reviewff").bind("submit",function(me){
			me.preventDefault();
			$("h3#reviewhl").html("Add your opinion<img style='margin:5px 0 0 10px;' src='/layout/img/loaderw.gif' />");
			if($("div#reviewf") != 0) {
				$("div#reviewf").remove();
			}
			$(this).after('<div id="reviewf"></div>');
			loadPreview();		
			return false;
		
		});
}

function clickAddR() {
	//$("h3#reviewhl").ScrollTo(400);
}

function activateLoginR() {
	$("div.nonmember").find("a").click(function(me){
		me.preventDefault();
		makeActionP(2);
		endaction = false;
		loadLoginF();
			
	});
	
}

function makeActionP(which) {
	if($("div#actionpanel").length == 0) {
			if (which == 2) {
				$("div#reviewsl").before("<div id='actionpanel' class='clearfix' style='width:445px;margin-left:0px;overflow:hidden;'></div>");
			} else {
				$("div.hraction").after("<div id='actionpanel' class='clearfix'></div>");
			}
	} else {
			$("div#actionpanel").hide();
			$("div#actionpanel").remove();
			if (which == 2) {
				$("div#reviewsl").before("<div id='actionpanel' class='clearfix' style='width:445px;margin-left:0px;overflow:hidden;'></div>");
			} else {
				$("div.hraction").after("<div id='actionpanel' class='clearfix'></div>");
			}
	}
	
	$("div#actionpanel").hide();
	
}

function activateLoginF() {
	$("div.ar_actions").find("a").eq(0).click(function(me){
		me.preventDefault();
		makeActionP(1);
		endaction = true;
		loadLoginF();
			
	});
	
}

function loadLoginF() {
	$("div#actionpanel").load(("/dynamic.php?page_id=21"),function(me){
		
		// if it has an alert form.. hijack it
		if($("div#actionpanel").find("form").length > 0) {
			$("div#actionpanel").find("a.cancel").click(function(me){
				me.preventDefault();
				$("div#actionpanel").slideUp('fast',function(){
					$("div#actionpanel").remove();
				});
			});
			$("div#actionpanel").find("form").bind("submit",function(me){
				me.preventDefault();
				var eml = $("input#email",this).attr("value");
				var pw = $("input#password",this).attr("value");
				$("div#actionpanel").slideUp();
				$("div#actionpanel").load(("/dynamic.php?page_id=34&event="+eid),{email: eml,password: pw},function(me){
					$("div#actionpanel").find("a").remove();
					var response = $("h2.mt40","div#actionpanel").text();
					if (response.indexOf("We could not") != -1) {
						//alert('reload form');
						$("div#actionpanel").slideDown('slow');
						setTimeout(reloadLoginF,2500);
					} else {
						activateFavourite();
						$("p.subhead","div#actionpanel").remove();
						$("div#menu").find("ul").wrap("<div id='lmenu'></div>");
						$("div#lmenu").load(("/content.php?page_id=142"),function(me){	
							//$("div#lmenu").slideDown();
						});
						
						// change link text 
						$("div.nonmember").hide();
						$("div.nonmember").after('<a href="#addreview" class="listmap" id="rown"><span>Leave your opinion</span></a>');
						$("div.nonmember").remove();

						// display review panel
						$("div.reviews").append("<br class='cb' /><div id='rpanel'></div>");
						$("div#rpanel").load(("/content.php?page_id=141"),function(me){
							$("div#rpanel").slideDown('slow');
							activateReview();
						});
						$("div#actionpanel").hide();
						$("div#actionpanel").remove();
						if (endaction) {
							clickFavourite();
						} else {
							setTimeout(clickAddR,2500);
						}

					}
					
				});					

			});
		}
		$("div#actionpanel").slideDown('slow');
		
	});
}

function clearLoginF() {
	$("div#actionpanel").slideUp();
	$("div#actionpanel").remove();
}

function reloadLoginF() {
	$("div#actionpanel").slideUp();
	loadLoginF();
}

function activateFavourite() {
	$("div.ar_actions").find("a").eq(0).unbind();
	$("div.ar_actions").find("a").eq(0).click(function(me){
	
		me.preventDefault();
		
		clickFavourite();
		//alert($("div#actionpanel").attr("height"));
		return false;
	});
}

function clickFavourite() {
		makeActionP(1);
		$("div#actionpanel").load(("/dynamic.php?page_id=25&event="+eid),function(me){
			
			
			// if it has an alert form.. hijack it
			if($("div#actionpanel").find("form").length > 0) {
				$("div#actionpanel").find("form").find("input#alert").remove();
				$("div#actionpanel").find("form").bind("submit",function(me){
					me.preventDefault();
					
					$("div#actionpanel").slideUp();
					var temp = 'alert';
					$("div#actionpanel").load(("/dynamic.php?page_id=26&event="+eid),{alert: temp},function(me){
						$("div#actionpanel").find("a").remove();
						$("div#actionpanel").slideDown('slow');
						setTimeout(favoriteClean,2500);
					});
					
				});
				$("div#actionpanel").find("a").click(function(me){
					me.preventDefault();
					favoriteClean();
					return false;
				});
				//$("div#actionpanel").height('');
				$("div#actionpanel").slideDown('slow');
				
			} else {
				$("div#actionpanel").find("a").remove();
				//$("div#actionpanel").height('');
				$("div#actionpanel").slideDown('slow');
				
				// set timeout to hide the panel
				setTimeout(favoriteClean,2500);
				
			}
			
			
		});
}
function favoriteClean() {
	$("div#actionpanel").slideUp('slow',function(){
		$("div#actionpanel").remove();

	// find the right part of the docuemnt
		
		$("div#others").load(("/content.php?page_id=99&event="+eid),function(me){
			$("div#others").slideDown('slow');
		});

	});
	
	
}

function activateReporting() {
$("div.review").find("a.reportl").each(function(){
	var reporturl = $(this).attr('href');
	reporturl = reporturl.substr(reporturl.indexOf('&'),reporturl.length);
	$(this).click(function(me){
		me.preventDefault();
		//alert('hijacked');
		//32
		$(this).after('<img style="mmargin:5px 0 0 10px;" src="/layout/img/loaderw.gif" id="loader" />');
		if ($("div.insert").length != 0){removeInsert();}
		$(this).after('<div class="insert mt10"></div>');
		insert = $("div.insert")
		$(insert).hide();
		$(insert).addClass('bluebg');
		$(insert).load('/dynamic.php?page_id=32'+reporturl,function(){
			$("img#loader").remove();
			$(insert).show('slow');
			$(insert).find("form").each(function(){
				$(this).append('<span style="float:left;display:block;margin-top:4px;">&nbsp;&nbsp; <a href="" onclick="removeInsert();return false"> or cancel</a></span>');
				$(this).submit(function(me){
					me.preventDefault();
					$(insert).hide();
					$(insert).addClass('h30');
					var posturl = $(this).attr('action');
					posturl = posturl.substr(posturl.indexOf('?'),posturl.length);
					var reportF = document.forms.reportF;
					var message = reportF.message.value
					$(insert).load('/dynamic.php'+posturl,{message: message},function(){
						$(insert).after('<p class="alert" style="margin-left:38px;font-size:.95em;margin-top:0px;margin-bottom:10px;">You have reported this opinion</p>');
						removeInsert();
					});
					
					
					return false;
				});
			
			});
			
		});
		return false;
	});

});

}

function loadPreview() {
	var pv = "preview";
	getFormVals();
	$("div#reviewf").hide();
	$("div#reviewf").load(("/dynamic.php?page_id=27&event="+eid),({addreview: ar,headline: hd,review: rw, rating: rt,preview:pv}),function(){
		$("form#reviewff").hide('slow');//,function(){
			$("h3#reviewhl").html('Add your opinion <span id="reviewhln" class="emphasis">/ Preview</span>');
			$("div#reviewf").show('slow');	
		//});
		$("div#reviewf").find("form").each(function(){
			$(this).bind("submit",function(me){
				me.preventDefault();
				var arn = "store";
				$("span#reviewhln").remove();
				//$("h3#reviewhl").fadeOut();
				$("h3#reviewhl").html('<span class="alert">Your opinion has been added</span> ');
				/*$("div#reviewf").before('<div id="alertfav"><p class="mt10"><strong>Do you want to add this event to your favourites?</strong></p><input id="yesfavourite" class="fl btn" type="submit" value="yes please" name="yes"/><a class="fl ml10 mt5" id="nofavourite" href="#">No thanks</a><br class="cb" /></div>')
				$("input#yesfavourite").click(function(me){
					me.preventDefault();
					addFavRev();
					return false;
				});
				$("a#nofavourite").click(function(me){
					me.preventDefault();
					placeReview();
					return false;
				});*/
				
				placeReview();
				
				//$("h3#reviewhl").fadeIn('slow');
				$("div#reviewf").slideUp('slow',function(){});
				/*$("div#reviewf").slideUp('slow',function(){
					$("div#reviewf").load(("/dynamic.php?page_id=29&event="+eid),({addreview: arn,store: 1}),function(){
						$("div#reviewsl").load(("/dynamic.php?page_id=15&event="+eid),function(){	
							activateReporting();
							setTimeout(reviewClean,1500);
						});
					});
				});*/
			});
			$(this).find("a").each(function(){
				$(this).click(function(me){
					me.preventDefault();
					$("div#reviewf").hide('slow');//,function(){
					$("form#reviewff").show('slow');
					//});
					$("span#reviewhln").remove();
					return false;
				});
			});
		});
	});
}

function placeReview() {
	var arn = "store";
	$('div#alertfav').remove();
	$("h3#reviewhl").append(' <img style="margin:5px 0 0 10px;" src="/layout/img/loaderw.gif" id="loader" />');
	$("div#reviewf").load(("/dynamic.php?page_id=29&event="+eid),({addreview: arn,store: 1}),function(){
		$("div#reviewsl").load(("/dynamic.php?page_id=15&event="+eid),function(){	
			activateReporting();
			setTimeout(reviewClean,1500);
		});
	});
}


function addFavRev() {
	$("h3#reviewhl").html('');
	$("div#alertfav").load(("/dynamic.php?page_id=25&event="+eid),function(me){
		
		var heading = $("div#alertfav").find('h2')
		$("h3#reviewhl").html($(heading).html());
		$(heading).remove();
		$('div.hrwhite').remove();
		// if it has an alert form.. hijack it
		if($("div#alertfav").find("form").length > 0) {
			$("div#alertfav").find("form").find("input#alert").remove();
			$("div#alertfav").find("form").bind("submit",function(me){
				me.preventDefault();
				//console.log('form submit a');
				$("div#alertfav").slideUp();
				var temp = 'alert';
				$("div#alertfav").load(("/dynamic.php?page_id=26&event="+eid),{alert: temp},function(me){
					var heading = $("div#alertfav").find('h2')
					$("h3#reviewhl").html($(heading).html());
					$(heading).remove();
					$('div.hrwhite').remove();
					$("div#alertfav").find("a").remove();
					$("div#alertfav").slideDown('slow');
					setTimeout(placeReview,500);
					setTimeout(favoriteCleanR,1000);
				});
				
			});
			$("div#alertfav").find("a").click(function(me){
				me.preventDefault();
				//console.log('click a');
				setTimeout(placeReview,1500);
				favoriteCleanR();
				return false;
			});
			//$("div#actionpanel").height('');
			$("div#alertfav").slideDown('slow');
			
		} else {
			var heading = $("div#alertfav").find('h2')
			$("h3#reviewhl").html($(heading).html());
			$(heading).remove();
			$('div.hrwhite').remove();
			$("div#alertfav").find("a").remove();
			//$("div#actionpanel").height('');
			$("div#alertfav").slideDown('slow');
			//console.log('else');
			// set timeout to hide the panel
			setTimeout(favoriteCleanR,1000);
			setTimeout(placeReview,1500);
		}
		
		
	});
	

}

function favoriteCleanR() {
	$("div#alertfav").slideUp('slow',function(){
		$("div#alertfav").remove();

	// find the right part of the docuemnt
		
		$("div#others").load(("/content.php?page_id=99&event="+eid),function(me){
			$("div#others").slideDown('slow');
		});

	});
	
	
}


function getFormVals() {
	ar = reviewF.addreview.value;
	hd = reviewF.headline.value;
	rw = reviewF.review.value;
	rt = reviewF.rating.value;
}

function reviewClean() {
	reviewF.addreview.value = '';
	reviewF.headline.value = '';
	reviewF.review.value = '';
		//$("div#reviewf").hide('slow',function(){
			$("div#reviewf").remove();
		//});
		
		$("h3#reviewhl").fadeTo('slow',1,function(){
			setTimeout(reviewReset,1000);
		});
		
		

		//$("h3#reviewhl").hide('slow');
	//});
	
}

function reviewReset() {
	$("h3#reviewhl").html('Add your opinion');
	$("form#reviewff").slideDown('slow');
	$("h3#reviewhl").fadeIn('slow');
}

function removeInsert() {
	$(insert).hide('slow');
	$(insert).remove();
	insert = '';
}

function getID(hString) {
	return hString.substring(hString.indexOf("=")+1,hString.length); 
}
