﻿var starnumber = 0;
var rating = 0;
var fadespeed = 500;
var alreadyrated = false;
var alreadycommented = false;
//Plugin just pasted into the script for now.
jQuery.cookie = function(name, value, options) {
	if (typeof value != 'undefined') { // name and value given, set cookie
		options = options || {};
		if (value === null) {
			value = '';
			options.expires = -1;
		}
		var expires = '';
		if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
			var date;
			if (typeof options.expires == 'number') {
				date = new Date();
				date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
			} else {
				date = options.expires;
			}
			expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
		}
		// CAUTION: Needed to parenthesize options.path and options.domain
		// in the following expressions, otherwise they evaluate to undefined
		// in the packed version for some reason...
		var path = options.path ? '; path=' + (options.path) : '';
		var domain = options.domain ? '; domain=' + (options.domain) : '';
		var secure = options.secure ? '; secure' : '';
		document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
	} else { // only name given, get cookie
		var cookieValue = null;
		if (document.cookie && document.cookie != '') {
			var cookies = document.cookie.split(';');
			for (var i = 0; i < cookies.length; i++) {
				var cookie = jQuery.trim(cookies[i]);
				// Does this cookie string begin with the name we want?
				if (cookie.substring(0, name.length + 1) == (name + '=')) {
					cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
					break;
				}
			}
		}
		return cookieValue;
	}
};
jQuery(document).ready(function() {

    jQuery(".RTPcomment").css("cursor", "auto");
    // Messy, will clean up later.
    if (true) {
        var SessionID2 = "";
        var SessionID = 0;
        var pageID = "";
        if (jQuery.cookie("AptuitRating") != null) {
            //Again, there will be a better way to parse/cast this
            SessionID2 = jQuery.cookie("AptuitRating");
            SessionID = parseInt(SessionID2);
            pageID = jQuery('.SCID').html();
        }
        //Set the star rating on page load
        jQuery.ajax({
            type: "POST",
            url: "/ratethispage.aspx/GetRating",
            // The data - N.B. these translate directly into parameters
            data: "{'m_SessionId':" + SessionID + ", 'pageID':'" + pageID + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            // If successful...
            success: function(msg) {
                // Set the cookie data as the returned session ID
                //s et the star rating
                jQuery('.RTPstar:lt(' + msg.d + ')').toggleClass('RTPclicked');
                if (msg.d > 0) {
                    alreadyrated = true; alreadycommented = true;
                    jQuery(".RTPcomment img").attr("src", "/aptuit60/images/RTPCommentOff.gif");
                    jQuery(".RTPcomment").css("cursor", "auto");
                }
            }
        });
    }
    //Star click behaviour
    jQuery(".RTPstar").click(
            function() {
                if (!alreadyrated) {
                    // Get the rating based on the star's index within the parent div
                    rating = jQuery(".RTPstar").index(this) + 1;
                    // Set the display of the stars
                    jQuery('.RTPstar').removeClass('RTPclicked');
                    jQuery('.RTPstar:lt(' + rating + ')').toggleClass('RTPclicked');
                    var pageID = jQuery('.SCID').html();
                    var SessionID = 0;
                    if (jQuery.cookie("AptuitRating") != null) {
                        //Again, there will be a better way to parse/cast this
                        var SessionID2 = jQuery.cookie("AptuitRating");
                        var SessionID = parseInt(SessionID2);
                    }
                    // AJAX time
                    jQuery.ajax({
                        type: "POST",
                        url: "/ratethispage.aspx/SubmitRating",
                        data: "{'m_SessionId':" + SessionID + ", 'rating':" + rating + ", 'comment':'', 'pageID':'" + pageID + "', 'URL':'" + window.location.pathname + "'}",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        // If successful...
                        success: function(msg) {
                            // Set the cookie data as the returned session ID
                            jQuery.cookie("AptuitRating", msg.d, { expires: 7 });
                            //alert(jQuery.cookie("AptuitRating"));
                            alreadyrated = true;
                            jQuery(".RTPcomment img").attr("src", "/aptuit60/images/RTPComment.gif");
                            jQuery(".RTPcomment").css("cursor", "pointer");
                        }
                    });
                }
            });

    //Submit a comment
    jQuery(".RTPsubmit").click(
            function() {
                jQuery(".RTPpopup").fadeOut(0);
                jQuery(".RTPfade").fadeOut(fadespeed,
				function() {
				    jQuery(".RTPfade").remove();
				});
                var comment = jQuery(".RTPcommentsbox").val();
                //alert(comment);
                var SessionID = 0;
                //var thisPageUrl = window.location;
                var thisPageUrl = "test";
                var pageID = jQuery('.SCID').html();
                if (jQuery.cookie("AptuitRating") != null) {
                    //Again, there will be a better way to parse/cast this
                    var SessionID2 = jQuery.cookie("AptuitRating");
                    SessionID = parseInt(SessionID2);
                    jQuery.ajax({
                        type: "POST",
                        // Method name
                        url: "/ratethispage.aspx/AddComment",
                        // The data - N.B. these translate directly into parameters
                        data: "{'SessionID':" + SessionID + ", 'Comment':'" + comment + "','pageID':'" + pageID + "'}",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        // If successful...
                        success: function(msg) {
                            // Set the cookie data as the returned session ID
                            jQuery.cookie("AptuitRating", msg.d, { expires: 7 });
                            jQuery(".RTPcomment img").attr("src", "/aptuit60/images/RTPCommentOff.gif");
                            jQuery(".RTPcomment").css("cursor", "auto");
                            alreadycommented = true;
                        }
                    });
                }
                else {
                    alert("In order to submit a comment, you must have submitted a rating and have cookies enabled.");
                }
            }
        );
    jQuery(".RTPsubmit").hover(
            function() {
                jQuery(".RTPSubmitImg").attr("src", "/aptuit60/images/RTPCommentsSubmitOn.gif");
            }, function() {
                jQuery(".RTPSubmitImg").attr("src", "/aptuit60/images/RTPCommentsSubmitOff.gif");
            }
        );

    jQuery(".RTPcomment").click(
            function() {
                if (!alreadycommented && alreadyrated) {
                    $('body').prepend('<div class="RTPfade"></div>');
                    //IE6 code to set fade height					
                    var m_Url = window.location.pathname;
                    if (m_Url.indexOf("/Case-Studies/") == -1) {
                        var m_userAgent = window.navigator.userAgent;
                        var m_isIe6 = m_userAgent.indexOf("MSIE 6");
                        if (m_isIe6 > 0) {
                            var FullHeight = parseInt(jQuery(".GeneralOuter").css("height"));
                            var PageFullHeight = FullHeight + 100;
                            jQuery(".RTPfade").css("height", "" + PageFullHeight + "px");
                        }
                    }
                    else {
                        var m_userAgent = window.navigator.userAgent;
                        var m_isIe6 = m_userAgent.indexOf("MSIE 6");
                        if (m_isIe6 > 0) {
                            var FullHeight = parseInt(jQuery(".CaseStudyLeftCol").css("height"));
                            var PageFullHeight = FullHeight;
                            jQuery(".RTPfade").css("height", "" + PageFullHeight + "px");
                            jQuery(".RTPpopup").css("bottom", "-10px");
                        }
                    }

                    jQuery(".RTPfade").fadeTo(fadespeed, 0.5, function() {
                        jQuery(".RTPpopup").show(0);
                    }
				);
                }
            }

        );
    jQuery(".RTPclosebutton").click(
            function() {
                jQuery(".RTPpopup").fadeOut(0);
                jQuery(".RTPfade").fadeOut(fadespeed,
				function() {
				    jQuery(".RTPfade").remove();
				});
            }
        );
    jQuery(".RTPcancelbutton").click(
            function() {
                jQuery(".RTPpopup").fadeOut(0);
                jQuery(".RTPfade").fadeOut(fadespeed,
				function() {
				    jQuery(".RTPfade").remove();
				});
            }
        );
    jQuery(".RTPcancelbutton").hover(
            function() {
                jQuery(".RTPCancelImg").attr("src", "/aptuit60/images/RTPCommentsCancelOn.gif");
            }, function() {
                jQuery(".RTPCancelImg").attr("src", "/aptuit60/images/RTPCommentsCancelOff.gif");
            }
        );
    jQuery(".RTPstar").hover(function() {
        if (!alreadyrated) {
            starnumber = jQuery(".RTPstar").index(this) + 1;
            jQuery('.RTPstar:lt(' + starnumber + ')').toggleClass('RTPstaron');
        }
        else { jQuery(".RTPstar").css('cursor', 'auto') }
    }, function() {
        if (!alreadyrated) {
            jQuery('.RTPstar:lt(' + starnumber + ')').toggleClass('RTPstaron');
        }
    }
			);
}); 
