
$(document).ready(function () {
	
	
	//loadDivs();
	loadMenu();
	runLoadingAnimations();
	$('.imageSwap').hover(function() {
		$(this).children(".pic").attr("src","images/top_pic_7.jpg");
			}, function() {
		$(this).children(".pic").attr("src","images/top_pic_8.jpg");
	});
	loaded = true;
});
var reviewSubmitted = false;
function runLoadingAnimations() {
	var test = location.search;
	loadTwitter();
	$("#logo").delay(50).show("clip", "slow", function() {
		$("#topImage1").delay(100).show("clip", "slow", function() {
			$("#topImage2").delay(100).show("clip", "slow", function() {
				//$("#sideBar").accordion("destroy").accordion({ header: "h3" });
				//$("#sideContainer").delay(100).show("drop", "slow", function() {
					
					$("#loadContent").delay(100).show("drop", "slow");
					if (test == "?=contact") {
						$("#loadContent").load("contact.php", function() {
							$("#tabs").tabs();
						});
					}
					if (test == "?=refer") {
						$("#loadContent").load("refer.php", function() {
							$("#tabs").tabs();
						});
					}
					if (test == "?=repair") {
						$("#loadContent").load("repair.php", function() {
							$("#repairAccord").accordion({ 
								header: "h3",
								autoHeight: false,
								active: false
							});
							$("#tabs").tabs();
						});
					}
					if (test == "?=testimonials") {
						$("#loadContent").load("testimonials.php", function() {
							$("#tabs").tabs();
							getAllReviews();
							
							$("#reviewBottom button").button().click(function() {
								$("#ratingRadio").buttonset();
								$(".ratingRadio").button("option", "icons", {secondary:'ui-icon-star'})
								$("#reviewFormDialog").dialog("destroy");
								$("#reviewFormDialog").dialog({
									modal: true,
									autoOpen: false,
									resizable: false,
									show: 'blind',
									hide: 'slide',
									width: '500px',
									height: 'auto',
									buttons: {
									'Submit/Send': function() {
										sendReview();
									},
									'Clear': function() {
										
										$("#customerNameTxt").val(' ');
										$("#customerNumber").val(' ');
										$("#reviewText").val(' ');
									},
									'Exit': function() {
										$(this).dialog('close');
									}
								},
								close: function() {
									$(this).dialog("destroy");
								}
								});
								$("#reviewFormDialog").dialog('open');
							});
						});
					}
					if (test == "?=why") {
						$("#loadContent").load("why.php", function() {
							$("#tabs").tabs();
							$("#why_1").delay(50).show("fold", "slow", function() {
								$("#why_2"). delay(150).show("fold", "slow", function() {
									$("#why_3").delay(100).show("fold", "slow", function() {
										$("#why_4").delay(100).show("fold", "slow", function() {
											$("#why_5").delay(100).show("fold", "slow", function() {
												$("#why_6").delay(100).show("fold", "slow");
											});
										});
									});
								});
							});
						});
					}
					if (test == "?=work") {
						$("#loadContent").load("work.php", function() {
							$("#tabs").tabs();
						});
					}
				//});		
			});
		});
	});
}

function loadTwitter() {
	$.jTwitter("CampusBodySalon", 4, function(data) {
		$("#posts").empty();
		
		$.each(data, function(i, post) {
			//post.text.replace("http://", "");
			$("#posts").append(
				"<div class='post'>" + post.text.replace("http://", "") + "</div>"
			);	
		});
		
	});
	$("#twitterTop").click(function() {
		window.open("http://www.twitter.com/campusbodysalon");
		return false;
	})
	
}

function loadMenu() {
	$("#sideBar").accordion({ 
		header: "h3",
		autoHeight: false
	});
	$("#tabs").tabs();
	$("#home").click(function (){
		location.replace("index.php");
	});
	$("#logo").click(function() {
		location.replace("index.php");
	})
	$("#why").click(function() {
		$("#workVid").hide();
		$("#loadContent").delay(400).hide("drop", "slow", function (){
			$("#loadContent").load("why.php", function() {
				$("#tabs").tabs();
				$("#why_1").delay(50).show("fold", "slow", function() {
					$("#why_2"). delay(150).show("fold", "slow", function() {
						$("#why_3").delay(100).show("fold", "slow", function() {
							$("#why_4").delay(100).show("fold", "slow", function() {
								$("#why_5").delay(100).show("fold", "slow", function() {
									$("#why_6").delay(100).show("fold", "slow");
								});
							});
						});
					});
				});
			});
		}).delay(300).show("drop", "slow");
	});
	$("#contact").click(function() {
		$("#workVid").hide();
		$("#loadContent").delay(400).hide("drop", "slow", function (){
			$("#loadContent").load("contact.php", function() {
				
				$("#tabs").tabs();
			});
		}).delay(300).show("drop", "slow");
	});
	$("#reviews").click(function() {
		$("#workVid").hide();
		$("#loadContent").delay(400).hide("drop", "slow", function (){
			$("#loadContent").load("testimonials.php", function() {
				$("#tabs").tabs();
				
			});
		}).delay(300).show("drop", "slow");
	});
	
	
	
	
	$("#repair").click(function() {
		$("#workVid").hide();
		$("#loadContent").delay(400).hide("drop", "slow", function (){
			$("#loadContent").load("repair.php", function() {
				$("#repairAccord").accordion({ 
					header: "h3",
					autoHeight: false,
					active: false
				});
				$("#tabs").tabs();
			});
		}).delay(300).show("drop", "slow");
	});
	$("#work").click(function() {
		$("#loadContent").delay(400).hide("drop", "slow", function (){
			$("#loadContent").load("work.php", function() {
				$("#tabs").tabs();
			});
		}).delay(300).show("drop", "slow");
	});
	$("#refer").click(function() {
		$("#workVid").hide()
		$("#loadContent").delay(400).hide("drop", "slow", function (){
			$("#loadContent").load("refer.php", function() {
				$("#tabs").tabs();
			});
		}).delay(300).show("drop", "slow");
	});
	
}
function sendReview() {
	if (!reviewSubmitted) {
		customerName = $("#customerNameTxt").val();
		reviewText = $("#reviewText").val();
		reviewRating = $("#ratingRadio input[type='radio']:checked").attr("id");
		reviewRating = reviewRating.split("_").pop();
		if (customerName == "") {
			$("#sentDialog span").html("You must provide a name.");
		} else if (reviewText == "" ) {
			$("#sentDialog span").html("You must provide a review.");
		} else {
			in_data = 
			{
				reviewAdd: 1,
		        customerName: customerName,
		        reviewText: reviewText,
		        reviewRating: reviewRating
		    };
			$.ajax({
			    type: "POST",
			    url: "./php/review/review.php",
			    data: in_data,
			    dataType: "json",
			    success: function (response) {
			        try {
			            if (!response) {
			            	
			            }
			        } catch (err) {
			            alert(err.description);
			        }
			    },
			    error: function (erro, msg, test) {
			    	alert(erro+" "+msg+" " + test);
			    }
			});
		}
		reviewSubmitted = true;
	} else {
		$("#sentDialog span").html("You have already submitted a review today.");
	}
	$("#sentDialog").dialog("destroy");
	$("#sentDialog").dialog({
		modal: true,
		autoOpen: false,
		resizable: false,
		show: 'blind',
		hide: 'slide',
		width: '250px',
		height: 'auto',
		buttons: {
		'Exit': function() {
			$(this).dialog('close');
		}
	},
	close: function() {
		$(this).dialog("destroy");
	}
	});
	$("#sentDialog").dialog('open');
}

function getAllReviews() {
	out_data = 
		{
            reviewID: 0,
            splash: 1
        };
    $.ajax({
        type: "POST",
        url: "./php/review/review.php",
        data: out_data,
        dataType: "json",
        success: function (response) {
            try {
                setReviews(response);
            } catch (err) {
                alert(err.description);
            }
        },
        error: function (erro, msg, test) {
        	alert(erro+" "+msg+" " + test);
        }
    });

	
}

function getReview(review) {
	out_data = 
	{
        reviewID: review,
        splash: 0
    };
	$.ajax({
	    type: "POST",
	    url: "./php/review/review.php",
	    data: out_data,
	    dataType: "json",
	    success: function (response) {
	        try {
	            setReview(response);
	        } catch (err) {
	            alert(err.description);
	        }
	    },
	    error: function (erro, msg, test) {
	    	alert(erro+" "+msg+" " + test);
	    }
	});
}

function setReviews (response) {
	$.each(response, function () {
		html="<div class='reviewSplash' id="+this.reviewID+">" +
				"<div class='splashTitle'>" +
					"<h3>"+this.reviewTitle+"</h3>" +
				"</div>" +
				"<div id='r"+this.reviewID+"' class='rate_widget'>"+  
	    			"<div class='star_1 ratings_stars'></div>"+ 
			        "<div class='star_2 ratings_stars'></div>"+  
			        "<div class='star_3 ratings_stars'></div>"+
			        "<div class='star_4 ratings_stars'></div>"+  
			        "<div class='star_5 ratings_stars'></div>"+  
			    "</div>"+ 
			    "<div class='clear'></div>"+
			    "<div class='splashText'>"+
			    	this.reviewTitleSplash+
			    "</div>"+
			"</div>";
		$("#reviewLoad").append(html);
			
		
		$("#r" + this.reviewID).find('.star_' + this.reviewRating).prevAll().andSelf().addClass('ratings_vote');
	});
	$(".reviewSplash").click(function() {
		getReview($(this).attr('id'));
		
	});
	
}
function setReview (response){
	$("#reviewTitle").html("<h3>"+response[0].reviewTitle+"</h3>");
	$("#reviewContent").html(response[0].reviewText);
	$("#customerName").html(response[0].customerName);
	$("#reviewDialog").dialog("destroy");
	$("#reviewDialog").dialog({
		modal: true,
		autoOpen: false,
		resizable: false,
		show: 'blind',
		hide: 'slide',
		width: '500px',
		height: 'auto',
		buttons: {
		'Exit': function() {
			$(this).dialog('close');
		}
	},
	close: function() {
		$(this).dialog("destroy");
	}
	});
	$("#reviewDialog").dialog('open');
}


