$(document).ready(function() {

		
	//DEFINITION LISTS (FAQs)
    $(".toggle dt").hover(
            function() {
                $(this).addClass("hover");
                $(this).css("cursor", "pointer");
            }, function() {
               $(this).removeClass("hover");
            }
      );
 
    $(".toggle dt").toggle(
            function() {
               $(this).next("dd").slideDown();
                $(this).addClass("open");
            }, function() {
               $(this).next("dd").slideUp();
               $(this).removeClass("open");
            }
      ); 





    //TEXT RESIZING
    var defaultText = "12"; //pixels
    var largeText = "15";
    var largerText = "18";


    $("#text-size ul a").click(
		function() {
		    var textSize = $(this).attr("id");

		    switch (textSize) {
		        case "text-default":
		            setTextSize(defaultText);
		            break;

		        case "text-large":
		            setTextSize(largeText);
		            break;

		        case "text-larger":
		            setTextSize(largerText);
		            break;
		    }

		    return false;
		}
	);

    //FORUM SLIDESHOW
    var slideshow_count = $(".forum-show .images li").length;
    var slideshow_current = 1;

    $(".forum-show .images li:first").show();

    function slideshow_run() {

        $("#forum-caption-" + slideshow_current).fadeOut(1500);
        $("#forum-caption-nav-" + slideshow_current).removeClass("current");
        $("#forum-caption-nav-" + slideshow_current).addClass("previous");

        slideshow_current++;

        if (slideshow_current > slideshow_count) {
            slideshow_current = 1;
            $(".forum-show ul.nav li").removeClass("previous");
        }

        $("#forum-caption-" + slideshow_current).fadeIn(1500);
        $("#forum-caption-nav-" + slideshow_current).addClass("current");

    }

    if ($(".forum-show ul.images").length > 0) {
        $(".forum-show ul.images").everyTime(8000, slideshow_run);
    }

    $(".forum-show ul.nav li a").click(
		function() {
		    if ($(this).parent("li").attr("class") != "current") {

		        $(".forum-show ul.images").stopTime();

		        var slideshow_target = $(this).attr("href");

		        $("#forum-caption-" + slideshow_current).fadeOut(1500);
		        $(slideshow_target).fadeIn(1500);

		        slideshow_current = $(this).attr("href");
		        slideshow_current = slideshow_current.substr(slideshow_current.lastIndexOf("-") + 1);

		        $(".forum-show ul.nav li").removeClass("current");
		        $(".forum-show ul.nav li").removeClass("previous");

		        for (i = 1; i < slideshow_current; i++) {

		            $("#forum-caption-nav-" + i).addClass("previous");
		        }

		        $("#forum-caption-nav-" + slideshow_current).addClass("current");

		        $(".forum-show ul.images").everyTime(4000, slideshow_run);
		    }

		    return false;
		}
	);

    //PRODUCT ITEM HOVER
    $(".product").hover(
		function() {

		    originalOpacity = $(this).css("opacity");
		    $(this).css("opacity", 1);

		}, function() {

		    $(this).css("opacity", originalOpacity)

		}
	);

    //TABBED NAVIGATION
    var current_tab = "tab-1";
    var tab_i = 1;
    $("#content-tab-1").show();

    $(".featured-nav li a").click(
		function() {
		    var tab_id = $(this).attr("id");
		    var this_range = $(this).attr("rel");

		    if (current_tab != tab_id) {

		        $(".featured-nav li").each(
					function() {
					    var this_tab_id = $(this).children("a").attr("id");

					    if (current_tab == this_tab_id) {
					        $("#content-" + current_tab).fadeOut(300);
					        $("#content-" + tab_id).animate({ marginLeft: 0 }, 300).fadeIn(300);
					        $("#" + current_tab).removeClass("current");
					        current_tab = tab_id;
					        $("#" + tab_id).addClass("current");

					        return false;
					    }
					}
				);

		    }

		    return false;
		}
	);

    //SOCIAL BOOKMARKING
    $("ul.social li a").each(
		function() {

		    var socialLink = $(this).attr("href");
		    socialLink += document.URL;
		    $(this).attr("href", socialLink);

		}
	);

    //SHOW MORE INTRO TEXT
    $("#show-more").toggle(
		function() {

		    $("#intro-ext").slideDown(400);
		    $(this).addClass("open");
		    $(this).text("Less");

		}, function() {

		    $("#intro-ext").slideUp(400);
		    $(this).removeClass("open");
		    $(this).text("More");

		}
	);

    $("#show").toggle(
		function() {

		    $("#intro-cap-ext").slideDown(400);
		    $(this).addClass("open");
		    $(this).text("Less");

		}, function() {

		    $("#intro-cap-ext").slideUp(400);
		    $(this).removeClass("open");
		    $(this).text("More");

		}
	);

	$("#show-ext").toggle(
		function() {

		    $("#intro-more").slideDown(400);
		    $(this).addClass("open");
		    $(this).text("Less");

		}, function() {

		    $("#intro-more").slideUp(400);
		    $(this).removeClass("open");
		    $(this).text("More");

		}
	);






    //STAR RATINGS
		$(".rating:not(.ext)").each(
		function() {

		    $(this).unbind("click");

		    var currentRating = $("p", this).attr("class");
		    var votedRating = "";

		    $("ul a", this).hover(
				function() {

				    var setRating = "hover-" + $(this).attr("class");
				    $(this).parent().parent().siblings("p").attr("class", setRating);

				}, function() {

				    if (votedRating == "") {
				        $(this).parent().parent().siblings("p").attr("class", currentRating);
				    } else {
				        $(this).parent().parent().siblings("p").attr("class", votedRating);
				    }

				}
			);

		    $("ul a", this).click(
				function() {
				    votedRating = "select-" + $(this).attr("class");
				    $(this).parent().parent().siblings("p").attr("class", votedRating);

				    var voteRating = $(this).text();
				    var voteArticle = $(this).parent("li").parent("ul").parent(".rating").siblings("h3").children("a").text();

				    $.post(site_root + "ajax/PostRating.aspx",
				        {
				            voteRating: voteRating,
				            voteArticle: voteArticle
				        },
					        function(data) {
					        }
				        );

				    return false;

				}
			);

		}

	);

    $(".rating.ext").each(
		function() {

		    $(this).unbind("click");

		    var currentRating = $("p", this).attr("class");
		    var votedRating = "";

		    $("ul a", this).hover(
				function() {

				    var setRating = "hover-" + $(this).attr("class");
				    $(this).parent().parent().siblings("p").attr("class", setRating);

				}, function() {

				    if (votedRating == "") {
				        $(this).parent().parent().siblings("p").attr("class", currentRating);
				    } else {
				        $(this).parent().parent().siblings("p").attr("class", votedRating);
				    }

				}
			);

		    $("ul a", this).click(
				function() {
				    votedRating = "select-" + $(this).attr("class");
				    $(this).parent().parent().siblings("p").attr("class", votedRating);

				    var voteRating = $(this).text();
				    var voteArticle = $(this).parent("li").parent("ul").parent(".rating.ext").parent(".open").siblings("h1").text();

				    $.post(site_root + "ajax/PostRating.aspx",
				        {
				            voteRating: voteRating,
				            voteArticle: voteArticle
				        },
					        function(data) {
					        }
				        );

				    return false;

				}
			);

		}

	);



    //OVERLAY CLICKS

    $(".email-signup-link").click(
        function() {
            overlay_email_signup();
            return false;
        }
	);

    $(".sign-in-link").click(
        function() {
            if ($(".sign-in-link.master").text() == "Log in") {
                if ($(this).hasClass("forum")) {
                    overlay_sign_in("forum");
                } else {
                    overlay_sign_in();
                }

            } else {
                sign_out();
            }

            return false;
        }
	);

    $(".profile-link").click(
        function() {
            overlay_profile(this.rel);
            return false;
        }
	);

    $(".contrib-link").click(
        function() {
            overlay_contributor_profile(GetPostIDFromPage());
            return false;
        }
	);

    $(".pro-profile-link").click(

        function() {

            overlay_professional_profile(this.rel);
            return false;
        }
	);

});



function GetPostIDFromPage() {
    return $("#postid").val();  
}


//TEXT RESIZING
function setTextSize(textSize) {
	
	$("body").css("font-size", textSize + "px");
	
}

//SHOW/HIDE PRODUCTS
function showProducts(productSet) {
	
	$(".product").each(
		function() {
			
			if(!$(this).hasClass(productSet) && ($(this).css("opacity") == 1)) {
				$(this).animate({
					opacity: 0.3
				}, 300);
			} else if($(this).hasClass(productSet) && ($(this).css("opacity") < 1)) {
				$(this).animate({
					opacity: 1
				}, 300);
			}
			
		}
	);
	
	return false;

}

//ACTION TRACKING
function trackAction(action, page, title, media, type) {
	
	var track_path = "/" + action + "/" + page + "/" + title;
	
	if(media) track_path += "/" + media;
	if(type) track_path += "/" + type;
	
	track_path = unescape(track_path);
	pageTracker._trackPageview(track_path);

}



function sign_out() {
    
    //var baseURL = $('#urlBase').attr('value'); 
    debugger;
	$.post(site_root + "ajax/SignOut.aspx", 
	{ 
	   
	 },
		function(data) {
			window.location.reload(true);
		}
	);

    return false;

}
//FORM OVERLAYS


function overlay_email_signup(this_id, media_path, media_title) {
    $("#overlays").load(site_root + "overlays/email-signup/default.aspx #sign-in", null, overlay_email_signup_init);
}

function overlay_sign_in(redirect) {
    $("#overlays").load(site_root + "overlays/sign-in/default.aspx #sign-in", null, function() { overlay_signin_init(redirect); });
}

function overlay_profile(userName) {
    $("#overlays").load(site_root + "overlays/profile/default.aspx?userName=" + escape(userName), null);
}
function overlay_contributor_profile(post_id) {
    $("#overlays").load(site_root + "overlays/contrib/default.aspx?postid=" + escape(post_id), null);
}
function overlay_professional_profile(userName) {
    $("#overlays").load(site_root + "overlays/professional-biography/default.aspx?username=" + escape(userName), null);
}

function overlay_email_signup_init() {
        $("table.form span.loader").hide();
        $(".form a.o-submit").click(
        function() {
            formSubmitOverlay(true);
            return false;
        }
    );
     
}

function overlay_signin_init(redirect) {
        $("table.form span.loader").hide();
        $(".form a.login-o-submit").click(
        function() {
            formSubmitOverlayLogin(true, redirect);
            return false;
        }
    );
     $(".forgot-p a").click(
		function() {
		    formSubmitOverlayForgotPassword();
		}

	);
     
}

function overlay_sign_init() {
        $("table.form span.loader").hide();
        $(".form a.o-submit").click(
        function() {
            formSubmitOverlay(true);
            return false;
        }
    );
 }
 

//VIDEO OVERLAYS

var media_path = null;
var media_title = null;
current_overlay_flashId = null;

function overlay_email(this_id, media_path, media_title) {
    $("#overlays").load(site_root + "overlays/overlays.aspx #overlay-email", null, overlay_email_init);

    current_overlay_flashId = this_id;
    this.media_path = media_path;
    this.media_title = media_title;


    return false;
}

function overlay_share(this_id, media_path, media_title) {
    $("#overlays").load(site_root + "overlays/overlays.aspx #overlay-share", null, overlay_share_init);

    current_overlay_flashId = this_id;
    this.media_path = media_path;
    this.media_title = media_title;

    return false;
}

function overlay_download(this_id, media_path, media_title) {
    $("#overlays").load(site_root + "overlays/overlays.aspx #overlay-download", null, overlay_download_init);

    current_overlay_flashId = this_id;
    this.media_path = media_path;
    this.media_title = media_title;

    return false;
}

function overlay_download_init() {
    $("span.media-title").text(media_title);
    $("p.mov a").attr("href", media_path + ".mov");
    $("p.wmv a").attr("href", media_path + ".wmv");
    $("p.m4v a").attr("href", media_path + ".m4v");
}

function overlay_share_init() {
    $("span.media-title").text(media_title);
    $("a.delicious").attr("href", "http://del.icio.us/post?url=" + document.URL + "&title=" + document.title);
    $("a.digg").attr("href", "http://digg.com/submit?url=" + document.URL + "&title=" + document.title);
    $("a.facebook").attr("href", "http://www.facebook.com/sharer.php?u=" + document.URL);
    $("a.stumbleupon").attr("href", "http://www.stumbleupon.com/submit?url=" + document.URL + "&title=" + document.title);

    trackAmountTimesShared(media_title);

    var flashvars_copyLink = {
        buttonText: "Copy Link to Clipboard",
        copyText: document.URL
    };
    var params_copyLink = {
        allowscriptaccess: "always",
        wmode: "transparent"
    };

    swfobject.embedSWF(site_root + "swf/copyToClipboard.swf", "copy-link", "200", "23", "8.0.0", site_root + "SWF/expressInstall.swf", flashvars_copyLink, params_copyLink);

    var flashvars_copyCode = {
        buttonText: "Copy Code to Clipboard",
        copyText: "Code here"
    };
    var params_copyCode = {
        allowscriptaccess: "always",
        wmode: "transparent"
    };

    // swfobject.embedSWF(site_root + "swf/copyToClipboard.swf", "copy-code", "200", "23", "8.0.0", site_root + "SWF/expressInstall.swf", flashvars_copyCode, params_copyCode);

}
function overlay_email_init() {
    $("span.media-title").text(media_title);
    $("#overlay-content textarea").text("I thought you might be interested in '" + media_title + "' on the Poligrip website.");
    $(".form a.o-submit").click(
        function() {
            formSubmitOverlay(true);
            return false;
        }
        );
}

function overlay_video(media_path, media_title, media_desc, media_votes, media_author, media_date) {
    $("#overlays").load(site_root + "overlays/overlays.aspx #overlay-video", null, overlay_video_init);

    this.media_path = media_path;
    this.media_title = media_title;
    this.media_desc = media_desc;
    this.media_votes = media_votes;
    this.media_author = media_author;
    this.media_date = media_date;

    return false;
}


function overlay_video_init() {

    $(".media-title").text(media_title);
    $(".media-desc").text(media_desc);
    $(".media-votes").text(media_votes);
    $(".media-author").text(media_author);
    $(".media-date").text(media_date);
    /*
    lmp.embedPlayer("LMP-458x258-overlay", "Video", "2009/Sport/Football/LSPL/Video/JimHenryTaster/JimHenryTaster", { title: "Jim Henry - Profile", posterFrameURL: site_root + "images/258/jim_henry.jpg", posterFrameYShift: 0});
    */
    var flashVars = {
        entryId: "4ipwnhcyms",
        kshowId: "-1",
        partnerId: "1147",
        subp_id: "114700"
    };
    var params = {
        allowscriptaccess: "always",
        allownetworking: "all",
        allowfullscreen: "true",
        bgcolor: "#000000",
        wmode: "opaque"
    };
    swfobject.embedSWF("http://www.kaltura.com/kwidget/wid/23100", "LMP-458x343-overlay", "458", "343", "9.0.0", false, flashVars, params);

}

function closeOverlay() {

    $('#overlays').empty();

    if (current_overlay_flashId) {
        var PMP_player = document.getElementById(current_overlay_flashId);
        PMP_player.overlayResume();
    }
    current_overlay_flashId = null;

    return false;
};

function trackAmountTimesShared(title) {
    $.get(site_root + "ajax/VideoShareCounter.ashx?title=" + title);
}
