$(document).ready(function() {
	//function plsWait() { alert('Loading data. Please be patient:)'); }
	$("a.iframe").fancybox({
		'width' : '100%',
		'height': '100%',
		'autoScale': true,
		'transitionIn': 'fade',
		'transitionOut': 'none',
		'type': 'iframe',
		'titleShow': false,
		'margin':'20px'
		//'onStart': plsWait
    });
    $("a.iframetitle").fancybox({
        'width': 840,
        'height': 420,
        'autoScale': false,
        'transitionIn': 'fade',
        'transitionOut': 'none',
        'type': 'iframe',
        'titleShow': true,
        'margin': '5px'
        //'onStart': plsWait
    });
	$("#accordion").accordion({ collapsible: true, autoHeight: false});
    var rotatei = 0;
    $("#brotate").rotate();
    $(function() {
	    $(window).scroll(function() {
		    if($(this).scrollTop() != 0) {
			$('#toTop').fadeIn();
		    } else {
			$('#toTop').fadeOut();
		    }
		});
 
	    $('#toTop').click(function() {
		    $('body,html').animate({scrollTop:0},700);
		});
	});
});

$(function () {

    $("#brotate").hover(
		function () {
		    clearInterval(rotatei);
		    $(this).css("cursor", "default");
		},
		function () {
		    $(this).css("cursor", "default");
		    $(this).rotate();
		}
	);

    $("#msgclose").click(
		function () {
		    $("#msgtr").animate({ opacity: 'hide' }, 'fast');
		}
	);

    $("#texty").hover(
		function () {
		    $("#fram").html("&nbsp;&nbsp;click to follow RateAustin!");
		},
		function () {
		    $("#fram").html("");
		}
	);

    $("#textyout").hover(
		function () {
		    $("#textyout").html(":|");
		    $("#textyout").css("cursor", "help");
		    $("#fraq").html("&nbsp;&nbsp;sign in to reply, RT, follow, etc.");
		},
		function () {
		    $("#fraq").html("");
		    $("#textyout").css("cursor", "default");
		}
	);

    $("#textyh").hover(
		function () {
		    $("#textyh").css("cursor", "help");
		    $("#frah").html("&nbsp;&nbsp;thank you for following RateAustin!");
		},
		function () {
		    $("#frah").html("");
		    $("#textyh").css("cursor", "help");
		}
	);

    $("#siwt").hover(
		function () {
		    $("#textyout").html(":)");
		    $("#fraq").html("&nbsp;&nbsp;no spam guarantee");
		},
		function () {
		    $("#textyout").html(":|");
		    $("#fraq").html("");
		}
	);

    $("#siwt").click(
	    function () {
	        $("#fraq").html("&nbsp;&nbsp;Woo hoo!!!");
	    }
	);

    $("#fra").click(
		function () {
		    $("#texty").animate({ opacity: '.1' }, 'slow');

		    $.get("twitter-follow/17249/14366053/",
				function (data) {
				    if (data == 0) {
				        $("#texty").html(":)");
				        $("#texty").animate({ opacity: '1' }, 'fast');
				        $("#fram").html("&nbsp;&nbsp;thank you!");
				        $("#texty").unbind("click mouseenter mouseleave");
				    } else {
				        $("#texty").html(":|");
				        $("#texty").animate({ opacity: '1' }, 'fast');
				        $("#fram").html("&nbsp;&nbsp;something ain't right, please reload...");
				        $("#texty").unbind("click mouseenter mouseleave");
				    }
				}
			);
		}
	);

    $("img.retweet").hover(
		function () {
		    $(this).attr('src', 'images/retweet.png');
		},
		function () {
		    $(this).attr('src', 'images/retweet_off.png');
		}
	);

    $("img.retweet").click(
		function () {
		    var sid = $(this).attr("sid");
		    var sn = $(this).attr("sn");
		    var osrc = $(this).attr("src");
		    var img = $(this);
		    if ($('#reply_' + sid).html().length) {
		        $('#reply_' + sid).slideToggle();
		        return;
		    }
		    var replyhtml = "<div style=width:100%;text-align:right><div class=replytxtdiv>";
		    replyhtml += "<textarea id=reply_text_" + sid + " style=height:50px;width:100% onkeyup=\"len=$(this).val().length;$(reply_text_count_" + sid + ").html(len);(len>160 || len==0)?$(reply_button_" + sid + ").attr('disabled',true):$(reply_button_" + sid + ").attr('disabled',false);\">" + sn + "</textarea>";
		    replyhtml += "<input rid=" + sid + " sn=" + sn + " type=button value=\"Copy RT Quote\" class=trtq>";
		    replyhtml += " Tweet length: <span id=reply_text_count_" + sid + ">" + sn.length + "</span>&nbsp; ";
		    replyhtml += "<div style=float:right;padding-bottom:5px>";
		    replyhtml += "<input rid=" + sid + " type=button value=reply class=trp id=reply_button_" + sid + "> or ";
		    replyhtml += "<input rid=" + sid + " class=trt type=button value=retweet onmouseover=\"$(reply_text_" + sid + ").fadeTo('slow',.1);\" onmouseout=\"$(reply_text_" + sid + ").fadeTo('fast',1);\">";
		    replyhtml += "</div></div></div>";
		    $('#reply_' + sid).html(replyhtml);
		    $('#reply_' + sid).slideToggle();

		    $("input.trtq").click(
				function () {
				    var rid = $(this).attr("rid");
				    var sn = $(this).attr("sn");
				    var rtt = "RT " + sn + " " + $("#status_" + rid).text();
				    $("#reply_text_" + rid).val(rtt);
				    len = $("#reply_text_" + rid).val().length;
				    $("#reply_text_count_" + rid).html(len);
				    (len > 160 || len == 0) ? $("#reply_button_" + rid).attr('disabled', true) : $("#reply_button_" + rid).attr('disabled', false);
				}
			);

		    $("input.trt").click(
				function () {
				    $(this).attr("disabled", true);
				    var button = $(this);
				    var rid = $(this).attr("rid");
				    img.attr("src", "images/spinner.gif");
				    $.get("twitter-retweet/" + rid + "/",
						function (data) {
						    if (data == 0) {
						        img.attr("src", "images/retweet_success.png");
						        img.attr("title", "retweet successful!");
						        img.unbind("click mouseenter mouseleave");
						        $("#reply_" + rid).slideToggle("slow");
						    } else {
						        //alert(data);
						        alert("error talking to twitter...please try again");
						        img.attr("src", osrc);
						        button.attr("disabled", false);
						    }
						}
					);
				}
			);

		    $("input.trp").click(
				function () {
				    $(this).attr("disabled", true);
				    var button = $(this);
				    var rid = $(this).attr("rid");
				    var rtext = $("#reply_text_" + rid).val();
				    img.attr("src", "images/spinner.gif");
				    $.post("tt.php",
						{ action: 2024, sid: rid, status: rtext },
						function (data) {
						    if (data == 0) {
						        img.attr("src", "images/retweet_success.png");
						        img.attr("title", "reply successful!");
						        img.unbind("click mouseenter mouseleave");
						        $("#reply_" + rid).slideToggle("slow");
						    } else {
						        //alert(data); //DEBUG
						        alert("error talking to twitter...please try again");
						        img.attr("src", osrc);
						        button.attr("disabled", false);
						    }
						}
					);
				}
			);

		}
	);

    $("img.follow").hover(
		function () {
		    $(this).attr('src', 'images/follow.png');
		},
		function () {
		    $(this).attr('src', 'images/follow_off.png');
		}
	);

    $("img.follow").click(
		function () {
		    var bid = $(this).attr("bid");
		    var tid = $(this).attr("tid");
		    var osrc = $(this).attr("src");
		    var img = $(this);
		    var c = $(this).attr("c");
		    $(this).unbind("mouseenter mouseleave");
		    $(this).attr("src", "images/spinner.gif");
		    $.get("twitter-follow/" + bid + "/" + tid + "/",
				function (data) {
				    if (data == 0) {
				        img.attr("src", "images/following.png");
				        img.attr("title", "follow successful!");
				        img.removeClass("follow").addClass("following");
				        img.unbind("click");

				        if (c > 0) $(fcount).html(c);
				    } else {
				        img.attr("src", osrc);
				        alert("follow did not work or already following, please reload and try again");
				    }
				}
			);
		}
	);

    $("img.followticon").click(
		function () {
		    var bid = $(this).attr("bid");
		    var tid = $(this).attr("tid");
		    var osrc = $(this).attr("src");
		    var nw = $(this).attr("nw");
		    var img = $(this);
		    var c = $(this).attr("c");
		    $(this).unbind("mouseenter mouseleave");
		    $(this).attr("src", "images/loading.gif");
		    $.get("twitter-follow/" + bid + "/" + tid + "/",
				function (data) {
				    if (data == 0) {
				        img.unbind("click");
				        img.attr("src", "images/following48.png");
				        img.attr("title", "go to " + nw);
				        img.click(function () { location.href = nw + ".atx" });
				    } else {
				        img.attr("src", osrc);
				        alert("follow did not work or already following, please reload and try again");
				    }
				}
			);
		}
	);

    $("img.unfollowticon").click(
		function () {
		    if (confirm("Are you sure?")) {
		        var bid = $(this).attr("bid");
		        var tid = $(this).attr("tid");
		        var osrc = $(this).attr("src");
		        var img = $(this);
		        var c = $(this).attr("c");
		        $(this).unbind("mouseenter mouseleave");
		        $(this).attr("src", "images/loading.gif");
		        $.get("twitter-unfollow/" + bid + "/" + tid + "/",
				    function (data) {
				        if (data == 0) {
				            img.unbind("click");
				            img.attr("src", "images/bwx48.png");
				            img.attr("title", "unfollow successful!");
				            img.css("cursor", "default");

				        } else {
				            img.attr("src", osrc);
				            alert("unfollow did not work or not following, please reload and try again");
				        }
				    }
			    );
		    }
		}
	);

    $("img.referticon").click(
		function () {
		    var img = $(this);
		    img.attr("src", "images/bwplus48.png");
		}
	);

    $("div.showdown").toggle(
		function () {
		    var showme = $(this).attr("stid");
		    $('.' + showme).slideToggle();
		    $(this).removeClass("showdown").addClass("showup");
		},
		function () {
		    var showme = $(this).attr("stid");
		    $('.' + showme).slideToggle();
		    $(this).removeClass("showup").addClass("showdown");
		}
	);

    $("div.showdown").hover(
		function () {
		    $(this).addClass("showhover");
		},
		function () {
		    $(this).removeClass("showhover");
		}
	);

    $("div.showdownwide").toggle(
		function () {
		    var showme = $(this).attr("stid");
		    $('#' + showme).slideToggle();
		    $(this).removeClass("showdownwide").addClass("showup");
		},
		function () {
		    var showme = $(this).attr("stid");
		    $('#' + showme).slideToggle();
		    $(this).removeClass("showup").addClass("showdownwide");
		}
	);

    $("div.showdownwide").hover(
		function () {
		    $(this).addClass("showhoverwide");
		},
		function () {
		    $(this).removeClass("showhoverwide");
		}
	);

    $("img.tpic").mouseover(
        function () {
            var src = $(this).attr("rsc");
            if (src) {
                $(this).attr("src", src);
            }
        }
    );

    $("img.tpicrt").mouseover(
        function () {
            var src = $(this).attr("rsc");
            if (src) {
                $(this).attr("src", src);
            }
        }
    );

    $("img.tpicmore").hover(
		function () {
		    $(this).removeClass("tpicmore").addClass("tpic");
		},
		function () {
		    $(this).removeClass("tpic").addClass("tpicmore");
		}
	);

    $("img.tpicinreply").hover(
		function () {
		    $(this).removeClass("tpicinreply").addClass("tpicinreplyhover");
		},
		function () {
		    $(this).removeClass("tpicinreplyhover").addClass("tpicinreply");
		}
	);

    $("span.showcatdown").toggle(
		function () {
		    $(homecatdiv).slideToggle();
		    $(this).removeClass("showcatdown").addClass("showcatup");
		    return false;
		},
		function () {
		    $(homecatdiv).slideToggle();
		    $(this).removeClass("showcatup").addClass("showcatdown");
		    return false;
		}
	);

    $("img.tpicrt").click(
		function () {
		    var iconimg = $(this);
		    var sid = iconimg.attr("sid");
		    var tid = iconimg.attr("tid");
		    if (!sid) {
		        return;
		    }
		    var img = $("#rimg_" + sid);
		    var sn = img.attr("sn");
		    var osrc = img.attr("src");
		    /*    
		    if ($('div.hidden_' + tid + ':visible').length === 0)
		    $('div.hidden_' + tid).slideToggle();
		    */
		    if ($('#reply_' + sid).html().length) {
		        $('#reply_' + sid).slideToggle();
		        return;
		    }

		    var replyhtml = "<div style=width:100%;text-align:right><div class=replytxtdiv>";
		    replyhtml += "<textarea id=reply_text_" + sid + " style=height:50px;width:100% onkeyup=\"len=$(this).val().length;$(reply_text_count_" + sid + ").html(len);(len>160 || len==0)?$(reply_button_" + sid + ").attr('disabled',true):$(reply_button_" + sid + ").attr('disabled',false);\">" + sn + "</textarea>";
		    replyhtml += "<input rid=" + sid + " sn=" + sn + " type=button value=\"Copy RT Quote\" class=trtq>";
		    replyhtml += " Tweet length: <span id=reply_text_count_" + sid + ">" + sn.length + "&nbsp;</span>";
		    replyhtml += "<div style=float:right;padding-bottom:5px>";
		    replyhtml += "<input rid=" + sid + " type=button value=reply class=trp id=reply_button_" + sid + "> or ";
		    replyhtml += "<input rid=" + sid + " class=trt type=button value=retweet onmouseover=\"$(reply_text_" + sid + ").fadeTo('slow',.1);\" onmouseout=\"$(reply_text_" + sid + ").fadeTo('fast',1);\">";
		    replyhtml += "</div></div></div>";
		    $('#reply_' + sid).html(replyhtml);
		    $('#reply_' + sid).slideToggle();

		    $("input.trtq").click(
				function () {
				    var rid = $(this).attr("rid");
				    var sn = $(this).attr("sn");
				    var rtt = "RT " + sn + " " + $("#status_" + rid).text();
				    $("#reply_text_" + rid).val(rtt);
				    len = $("#reply_text_" + rid).val().length;
				    $("#reply_text_count_" + rid).html(len);
				    (len > 160 || len == 0) ? $("#reply_button_" + rid).attr('disabled', true) : $("#reply_button_" + rid).attr('disabled', false);
				}
			);

		    $("input.trt").click(
				function () {
				    $(this).attr("disabled", true);
				    var button = $(this);
				    var rid = $(this).attr("rid");
				    img.attr("src", "images/spinner.gif");
				    $.get("twitter-retweet/" + rid + "/",
						function (data) {
						    if (data == 0) {
						        img.attr("src", "images/retweet_success.png");
						        img.attr("title", "retweet successful!");
						        img.unbind("click mouseenter mouseleave");
						        $("#reply_" + rid).slideToggle("slow");
						    } else {
						        //alert(data);
						        alert("error talking to twitter...please try again");
						        img.attr("src", osrc);
						        button.attr("disabled", false);
						    }
						}
					);
				}
			);

		    $("input.trp").click(
				function () {
				    $(this).attr("disabled", true);
				    var button = $(this);
				    var rid = $(this).attr("rid");
				    var rtext = $("#reply_text_" + rid).val();
				    img.attr("src", "images/spinner.gif");
				    $.post("tt.php",
						{ action: 2024, sid: rid, status: rtext },
						function (data) {
						    if (data == 0) {
						        img.attr("src", "images/retweet_success.png");
						        img.attr("title", "reply successful!");
						        img.unbind("click mouseenter mouseleave");
						        $("#reply_" + rid).slideToggle("slow");
						    } else {
						        //alert(data); //DEBUG
						        alert("error talking to twitter...please try again");
						        img.attr("src", osrc);
						        button.attr("disabled", false);
						    }
						}
					);
				}
			);
		}
	);

});
 
(function($) {
    $.fn.rotate = function() {
	    var container = $(this);
		var divlength = container.children("div").size();
		var current = 0;
	    rotatei = setInterval(function() {
			if (current >= divlength) current = 0;
			container.children("div").filter(":eq("+current+")").show().end().not(":eq("+current+")").hide();
			current++;
		}, 5555);
		return container;
	};
})(jQuery);

