function clog(message) {
	// if(console) console.log(message);
}

var g_RevealStoreUrlCount = 1; // Global var for /winery/edit page
function MoreStoreUrlFields(){if(g_RevealStoreUrlCount < 6){g_RevealStoreUrlCount++;}if(g_RevealStoreUrlCount==5){$("#btnShowMoreStoreUrlFields").addClass('hide');}HideShowStoreUrlFields();}
function HideShowStoreUrlFields(){
	var fields = $('.store_url_fields');
	var prevFieldHasData = false;
	var showCount = 0;
	$.each(fields,function(idx,itm){
		if(g_RevealStoreUrlCount > idx || idx==0 || prevFieldHasData ){$(itm).removeClass('hide'); showCount = idx;}else{$(itm).addClass('hide');}
		prevFieldHasData = ($('#store_url_id_'+idx).val().length);
	});
	g_RevealStoreUrlCount = showCount+1;
}
var g_RevealStoreAddressCount = 1; // Global var for /winery/edit page
function MoreStoreAddressFields(){if(g_RevealStoreAddressCount < 6){g_RevealStoreAddressCount++;}if(g_RevealStoreAddressCount==5){$("#btnShowMoreStoreAddressFields").addClass('hide');}HideShowStoreAddressFields();}
function HideShowStoreAddressFields(){
	var fields = $('.store_address_fields');
	var prevFieldHasData = false;
	var showCount = 0;
	$.each(fields,function(idx,itm){
		if(g_RevealStoreAddressCount > idx || idx==0 || prevFieldHasData ){$(itm).removeClass('hide'); showCount = idx;}else{$(itm).addClass('hide');}
		prevFieldHasData = ($('#store_address_id_'+idx).val().length);
	});
	g_RevealStoreAddressCount = showCount+1;
}


$(document).ready(function() {
	function hasAttr(element, attr) {
		if(element.filter("*[" + attr + "]").length > 0) {
			return true;
		} else {
			return false;
		}
	}
	
	$("#change_zipcode").click(function() {
		var newzip = $("#zipcode").val();
		alert("Zip code changed to " + newzip);
	});
	$("input[type=hidden]").hide();
	
	if($("span.ajax").length > 0) {
		$("span.ajax").each(function() {
			var ajax = $(this);
			if(hasAttr(ajax, "ajax")) {
				ajax.click(function() {
					var a = $(this);
					var good = true;
					if(hasAttr(a, "confirm")) {
						good = confirm(a.attr("confirm"));
					}
					if(good) {
						$.getJSON(a.attr("ajax"), function(result) {
							// clog(result.status);
							if(result.status == "OK") {
								if(hasAttr(a, "redirect")) {
									// clog("Redirect found!");
									window.location = ajax.attr("redirect");
								} else {
									// somehow do something?
									// clog("No redirect found.");
								}
							} else {
								// it didn't work!
							}
						});
					}
				});
			}
		});
	}
	/*
	$("span.rating-collapsed").live("mouseover", function() {
		$(this).removeClass("rating-collapsed");
	});
	*/
	
	var Ratings = function(val){
		switch(val){
/*			case "": return 0;
			case "Not For Me": return 70;
			case "Like It": return 80;
			case "Love It": return 90;
			case "Wow Good": return 100;
			case 0: return "";
			case 70: return "Not For Me";
			case 80: return "Like It";
			case 90: return "Love It";
			case 100: return "Wow Good";
*/			
//			case (!isNaN(val)) && val >= 70 && val <= 100: return 70-val+100;
			case "100": return 70;
			case "99": return 71;
			case "98": return 72;
			case "97": return 73;
			case "96": return 74;
			case "95": return 75;
			case "94": return 76;
			case "93": return 77;
			case "92": return 78;
			case "91": return 79;
			case "90": return 80;
			case "89": return 81;
			case "88": return 82;
			case "87": return 83;
			case "86": return 84;
			case "85": return 85;
			case "84": return 86;
			case "83": return 87;
			case "82": return 88;
			case "81": return 89;
			case "80": return 90;
			case "79": return 91;
			case "78": return 92;
			case "77": return 93;
			case "76": return 94;
			case "75": return 95;
			case "74": return 96;
			case "73": return 97;
			case "72": return 98;
			case "71": return 99;
			case "70": return 100;

			case 100: return 70;
			case 99: return 71;
			case 98: return 72;
			case 97: return 73;
			case 96: return 74;
			case 95: return 75;
			case 94: return 76;
			case 93: return 77;
			case 92: return 78;
			case 91: return 79;
			case 90: return 80;
			case 89: return 81;
			case 88: return 82;
			case 87: return 83;
			case 86: return 84;
			case 85: return 85;
			case 84: return 86;
			case 83: return 87;
			case 82: return 88;
			case 81: return 89;
			case 80: return 90;
			case 79: return 91;
			case 78: return 92;
			case 77: return 93;
			case 76: return 94;
			case 75: return 95;
			case 74: return 96;
			case 73: return 97;
			case 72: return 98;
			case 71: return 99;
			case 70: return 100;

			case "": return "";
			case 69: return "";
			default: return val;
		}
	}

	$.extend($.ui.slider.defaults, {
			min: 69,
			max: 100,
			step: 1,
			animate: true,
			slide: function(event, ui) {
				$(this).siblings(".rating").val(Ratings(ui.value));
//				var c = $(this).closest("div.ratingbox");
//				var table = c.attr("table");
//				var row = c.attr("row");
				var rating = ui.value;
				if(isNaN(rating) || rating < 70 || rating > 100){
					rating = "";
				}
				$(this).children("a.ui-slider-handle").html(Ratings(rating));
			},
			stop: function(event, ui) {
				$(this).siblings(".rating").val(Ratings(ui.value));
				var c = $(this).closest("div.ratingbox");
				var table = c.attr("table");
				var row = c.attr("row");
				var rating = Ratings(ui.value);
				if(isNaN(rating) || rating < 70 || rating > 100){
					rating = "";
				}
				$.getJSON("/ajax/vote/" + table + "/" + row + "/" + rating + "/", function(result, textStatus) {
					if(textStatus == "success") {
						if(result.status == "OK") { //alert("table: "+table+" row: "+row+" rating: "+rating);
//							c.html("<span class=\"result\">" + result.message + "</span>");
						} else {
							// console.log(result);
							if(result) {
								alert("Error:" + result.message);
							} else {
								alert("Unknown error!");
							}
						}
					} else {
						alert("Couldn't access rating system!");
					}
				});
			}
			
		});
	$("div.ratingbox .slider").each(function() {
//		$(this).empty();
		var value = Ratings($(this).siblings(".rating").val());
		$(this).slider({
			value: value
		})
	});

/*
	$("div.ratingbox .slider").slider({
			value:Ratings("Not For Me"), //$(this).siblings(".rating").val()
			min: 0,
			max: 4,
			step: 1,
			slide: function(event, ui) {
//				alert(Ratings(ui.value));
//alert($(this).siblings(".rating").attr("class"));
				$(this).siblings(".rating").val(Ratings(ui.value));
//				$("#amount").val('$' + ui.value);
			}
	});
*/	
	$("span.rating").live("click", function() {
		var t = $(this);
		var c = t.closest("div.ratingbox");
		var table = c.attr("table");
		var row = c.attr("row");
		var rating = ""; var ratingText = "";
		if(t.hasClass("rating-wow")) {
			rating = "3";
			ratingText = "Wow Good";
		}
		if(t.hasClass("rating-love")) {
			rating = "2";
			ratingText = "Love It";
		}
		if(t.hasClass("rating-like")) {
			rating = "1";
			ratingText = "Like It";
		}
		if(t.hasClass("rating-not")) {
			rating = "0";
			ratingText = "Not For Me";
		}

		if(rating == "" || table == "") {
			alert("This rating button is not configured correctly!");
		} else {
			if(table == "example") alert("This is an example rating! You chose " + ratingText);
			$.getJSON("/ajax/vote/" + table + "/" + row + "/" + rating + "/", function(result, textStatus) {
				if(textStatus == "success") {
					if(result.status == "OK") {
						c.html("<span class=\"result\">" + result.message + "</span>");
					} else {
						// console.log(result);
						if(result) {
							alert("Error:" + result.message);
						} else {
							alert("Unknown error!");
						}
					}
				} else {
					alert("Couldn't access rating system!");
				}
			});
		}
	});
	
	$("a.flag_link").live("click", function() {
		var t = $(this);
		var link_id = "";
		var temp_link_id = t.attr('id');
		if(temp_link_id && temp_link_id.length && temp_link_id.length >= 1){
			link_id = temp_link_id.replace(/flag_link_/,"");
		}
		if(link_id == ""){
			alert("Error: Unable to locate the link");
		}else{
			if(confirm("Would you like to flag this link as inappropriate?")){
				//YES - flag as inappropriate
				$.getJSON("/ajax/link/flag/" + link_id + "/", function(result, textStatus) {
					if(textStatus == "success") {
						if(result.status == "OK") {
							alert("Your request to flag this link as inappropriate has been submitted.");
						} else {
							// console.log(result);
							if(result) {
								alert("Error:" + result.message);
							} else {
								alert("Unknown error!");
							}
						}
					} else {
						alert("Couldn't access flagging system!");
					}
				});
			}else{
				//NO - do nothing
			}
		}
	});
	
	$("a.flag_suggestion").live("click", function() {
		var t = $(this);
		var sugg_id = "";
		var temp_sugg_id = t.attr('id');
		if(temp_sugg_id && temp_sugg_id.length && temp_sugg_id.length >= 1){
			sugg_id = temp_sugg_id.replace(/flag_suggestion_/,"");
		}
		if(sugg_id == ""){
			alert("Error: Unable to locate the suggestion");
		}else{
			if(confirm("Would you like to flag this suggestion as inappropriate?")){
				//YES - flag as inappropriate
				$.getJSON("/ajax/suggestion/flag/" + sugg_id + "/", function(result, textStatus) {
					if(textStatus == "success") {
						if(result.status == "OK") {
							alert("Your request to flag this suggestion as inappropriate has been submitted.");
						} else {
							// console.log(result);
							if(result) {
								alert("Error:" + result.message);
							} else {
								alert("Unknown error!");
							}
						}
					} else {
						alert("Couldn't access flagging system!");
					}
				});
			}else{
				//NO - do nothing
			}
		}
	});
	
	$("a.flag_wine_review").live("click", function() {
		var t = $(this);
		var review_id = "";
		var temp_review_id = t.attr('id');
		if(temp_review_id && temp_review_id.length && temp_review_id.length >= 1){
			review_id = temp_sugg_id.replace(/flag_wine_review_/,"");
		}
		if(sugg_id == ""){
			alert("Error: Unable to locate the review");
		}else{
			if(confirm("Would you like to flag this review as inappropriate?")){
				//YES - flag as inappropriate
				$.getJSON("/ajax/suggestion/flag/" + review_id + "/", function(result, textStatus) {
					if(textStatus == "success") {
						if(result.status == "OK") {
							alert("Your request to flag this review as inappropriate has been submitted.");
						} else {
							// console.log(result);
							if(result) {
								alert("Error:" + result.message);
							} else {
								alert("Unknown error!");
							}
						}
					} else {
						alert("Couldn't access flagging system!");
					}
				});
			}else{
				//NO - do nothing
			}
		}
	});
	
	/*
	$("span.rating").live("mouseout", function() {
	});
	*/
	$("div.bubbleBox span.trigger").live("mouseover", function() {
		$(this).siblings("div.bubble").show();
	});
	$("div.bubbleBox").live("mouseout", function() {
		$(this).find("div.bubble").hide();
	});
	try{HideShowStoreUrlFields();}catch(e){};
	try{MoreStoreAddressFields();}catch(e){};
	
/*	
	$('#search_wine_form button').live("click", function() { 
		var target = $('#search_wine_form').attr('target') + $('#s').val() + '/';
		window.location = target;
	});
	$('#search_wine_form input').live("keypress", function(e) {
		if(e.keyCode == 13){ // Enter pressed
			var target = $('#search_wine_form').attr('target') + escape($('#s').val()) + '/';
			window.location = target;
		}
	});
*/

	$(".share_fb").live("click", function(){
		$.getJSON("/ajax/wine/share/facebook/", function(result, textStatus) {
			if(textStatus == "success") {
				if(result.status == "OK") {
					alert("Your request to share this post has been submitted.");
				} else {
					// console.log(result);
					if(result) {
						alert("Error:" + result.message);
					} else {
						alert("Unknown error!");
					}
				}
			} else {
				alert("Couldn't access sharing system!");
			}
		});
	});
	
	$(".share_tw").live("click", function(){
		$.getJSON("/ajax/wine/share/twitter/", function(result, textStatus) {
			if(textStatus == "success") {
				if(result.status == "OK") {
					alert("Your request to share this post has been submitted.");
				} else {
					// console.log(result);
					if(result) {
						alert("Error:" + result.message);
					} else {
						alert("Unknown error!");
					}
				}
			} else {
				alert("Couldn't access sharing system!");
			}
		});
	});
});

function Page(pageNum,id){
	if($("div").hasClass(".winelist_box")){
		if(id=='wlb1'){
			$("#"+id).load("/ajax/wine/search_page/"+pageNum+"/"+id+"/");
		}else{
			$("#"+id).load("/ajax/celler/search_page/"+pageNum+"/"+id+"/");
		}
//		$.post("/ajax/wine_search/"+pageNum + "/", null, null, "html");
	}
}