(function(a){function b(){var c=this;this.baseUrl=(document.location.protocol=="https:"?"https://app.bugmuncher.com/":"http://app.bugmuncher.com/");this.jsUriFile=this.baseUrl+"js/URI.js";this.cssFile=this.baseUrl+"css/bugMuncher.css";this.lang;this.htmlCache;this.htmlB64;this.packetLength=500;this.packets;this.hightLightCount=0;this.progressBar;this.options={language:"en",position:"right",showIntro:true,backgroundColor:"#F9F9F9",borderColor:"#222",showPreview:true};this.active=false;this.firstUse=true;this.highLighting=true;this.dragStart=function(d){if(!c.active){return}c.closeAlert();if(d.which!=1){return}if(c.highLighting){a(".bugMuncher-highlight, .bugMuncher-overlay").remove();c.createFullPageOverlay();a("body").prepend('<div class="bugMuncher-highlight bugMuncher-drag-current" />');var f="#fff"}else{a("body").prepend('<div class="bugMuncher-censor bugMuncher-drag-current" />');var f="#000"}a(".bugMuncher-drag-current").css({background:f,top:d.pageY,left:d.pageX,opacity:0.5});a(".bugMuncher-drag-current").get(0).x=d.pageX;a(".bugMuncher-drag-current").get(0).y=d.pageY;a(document).mousemove(c.drag);a(document).mouseup(c.dragEnd)};this.drag=function(d){width=d.pageX-a(".bugMuncher-drag-current").get(0).x;height=d.pageY-a(".bugMuncher-drag-current").get(0).y;if(width<0){width=width*-1;a(".bugMuncher-drag-current").css({left:d.pageX})}if(height<0){height=height*-1;a(".bugMuncher-drag-current").css({top:d.pageY})}a(".bugMuncher-drag-current").css({width:width,height:height})};this.dragEnd=function(d){if(a(".bugMuncher-drag-current").height()*a(".bugMuncher-drag-current").width()<20){a(".bugMuncher-drag-current").remove()}a(document).unbind();if(c.highLighting){c.reSlice();a(".bugMuncher-drag-current").css("background","none")}a(".bugMuncher-drag-current").css("opacity",1);a(document).mousedown(c.dragStart);a(".bugMuncher-drag-current").removeClass("bugMuncher-drag-current");c.assignBoxListners()};this.findHighlightedElements=function(f,e){var d=false;var g=false;a(f).children().each(function(){if(this.id=="bugMuncher-options"){return true}d=c.getCoords(this);var i=false;if(d.top>e.top){if(d.left>e.left){if(d.right<e.right){if(d.bottom<e.bottom){var h=a(this);h.addClass("bugMuncher-highlighted-element bugMuncher-highlighted-element-current")}}}}c.findHighlightedElements(this,e)})};this.calculateHighlightArea=function(){var d={top:0,left:0,right:0,bottom:0};a(".bugMuncher-highlighted-element-current").each(function(e){elementCoords=c.getCoords(this);if(e==0){d.top=elementCoords.top;d.left=elementCoords.left;d.right=elementCoords.right;d.bottom=elementCoords.bottom}else{d.top=elementCoords.top<d.top?elementCoords.top:d.top;d.left=elementCoords.left<d.left?elementCoords.left:d.left;d.right=elementCoords.right>d.right?elementCoords.right:d.right;d.bottom=elementCoords.bottom>d.bottom?elementCoords.bottom:d.bottom}});a(".bugMuncher-highlighted-element-current").removeClass("bugMuncher-highlighted-element-current");return d};this.reSlice=function(){if(a(".bugMuncher-highlight").size()==0){a(".bugMuncher-overlay").remove();c.createFullPageOverlay();return}var e=c.getCoords(".bugMuncher-drag-current");c.findHighlightedElements("body",e);var d=c.calculateHighlightArea();var f={top:d.top-e.top,left:d.left-e.left,bottom:e.bottom-d.bottom,right:e.right-d.right};a(".bugMuncher-overlay").each(function(){overlayCoords=c.getCoords(this);if(overlayCoords.top<0){topOffBy=-overlayCoords.top;overlayCoords.bottom+=topOffBy;e.top+=topOffBy;e.bottom+=topOffBy;overlayCoords.top=0}if(overlayCoords.left<0){leftOffBy=-overlayCoords.left;overlayCoords.right+=leftOffBy;e.left+=leftOffBy;e.right+=leftOffBy;overlayCoords.left=0}if(c.checkForIntersection(e,overlayCoords)){newOverlayHeight=e.top-overlayCoords.top;if(newOverlayHeight>0){a(this).css({height:newOverlayHeight})}newOverlayWidth=e.left-overlayCoords.left;if(newOverlayWidth>0){newTop=Math.max(e.top,overlayCoords.top);c.createOverlay(overlayCoords.left,newTop,newOverlayWidth,Math.min(e.bottom,overlayCoords.bottom)-newTop)}newOverlayWidth=overlayCoords.right-e.right;if(newOverlayWidth>0){newTop=Math.max(e.top,overlayCoords.top);c.createOverlay(e.right,newTop,newOverlayWidth,Math.min(e.bottom,overlayCoords.bottom)-newTop)}newOverlayHeight=overlayCoords.bottom-e.bottom;if(newOverlayHeight>0){c.createOverlay(overlayCoords.left,e.bottom,overlayCoords.right-overlayCoords.left,newOverlayHeight)}}})};this.getCoords=function(d){var g=a(d).offset();var f=g.top;var e=g.left;obj={top:f,left:e,bottom:f+a(d).height(),right:e+a(d).width()};return obj};this.checkForIntersection=function(d,e){if(d.top>e.top&&d.top<e.bottom&&d.left>e.left&&d.left<e.right){return true}if(d.top>e.top&&d.top<e.bottom&&d.right>e.left&&d.right<e.right){return true}if(d.bottom>e.top&&d.bottom<e.bottom&&d.left>e.left&&d.left<e.right){return true}if(d.bottom>e.top&&d.bottom<e.bottom&&d.right>e.left&&d.right<e.right){return true}return false};this.createOverlay=function(g,f,e,d){a("body").prepend('<div class="bugMuncher-overlay bugMuncher-overlay-current" style="opacity:0.65" />');a(".bugMuncher-overlay-current").css({width:e,height:d,top:f,left:g,opacity:0.65}).removeClass("bugMuncher-overlay-current")};this.createFullPageOverlay=function(){c.createOverlay(0,0,c.docWidth(),c.docHeight())};this.assignBoxListners=function(){a(".bugMuncher-highlight, .bugMuncher-censor").mouseenter(c.showCloseButton);a(".bugMuncher-highlight, .bugMuncher-censor").mouseleave(c.hideCloseButton)};this.showCloseButton=function(d){a(".bugMuncher-close-button").remove();a(this).addClass("bugMuncher-box-hover");a(this).append('<a href="#" class="bugMuncher-close-button">X</a>');a(".bugMuncher-close-button").css({top:-10,left:a(this).outerWidth(true)-16});a(".bugMuncher-close-button").click(function(f){var e=a(this).parent().hasClass("bugMuncher-highlight");a(this).parent().remove();a(this).remove();if(e){c.reSlice()}});a(".bugMuncher-close-button").mousedown(function(e){e.stopPropagation()});a(".bugMuncher-close-button").click(function(){return false})};this.hideCloseButton=function(){a(this).removeClass("bugMuncher-box-hover");a(this).children(".bugMuncher-close-button").remove()};this.removeBugMuncherFromHtml=function(d){var e=/<!-- StartBugMuncher -->[\s\S]+?<!-- EndBugMuncher -->/gi;return d.replace(e,"")};this.sendForPreview=function(){if(a(".bugMuncher-highlight").size()==0){c.alert(c.lang.noHighlightError,false,3000);return false}if(a("#bugMuncher-description").val()==""){c.alert(c.lang.noDescriptionError,false,3000);return false}a("#bugMuncher-top-marker").remove();a("body").append('<div id="bugMuncher-top-marker" />');a("#bugMuncher-top-marker").css({position:"absolute",top:a(window).scrollTop()});a("#bugMuncher-page-html").val("");var i=false;if(typeof document.all!="undefined"){if(typeof document.all[0].text!="undefined"){i=document.all[0].text}}if(i==false){i="<!DOCTYPE ";i+=document.doctype.name;i+=(document.doctype.publicId?' PUBLIC "'+document.doctype.publicId+'"':"");i+=(document.doctype.systemId?' "'+document.doctype.systemId+'"':"")+">"}var h=i+"\r\n"+c.outerHTML(a("html").get(0));var l=/<script[\s\S]+?<\/script>/gi;h=h.replace(l,"");h=h.replace("<!--[endif]---->","<![endif]-->");var d=/<!--.*?<[\s\S]+?><!--<!\[endif\]-->/gi;var f=/<!--.*?(<[\s\S]+?>)<!--<!\[endif\]-->/i;h=h.replace(d,function(n){var m=n.match(f);return m[1]});var g=new BugMuncherURI(location.href);var j=/\s(href|src)="[\s\S]+?"/gi;var e=/\s(href|src)="([\s\S]+?)"/i;h=h.replace(j,function(m){var n=m.match(e);var o=n[1];var p=new BugMuncherURI(n[2]);return" "+o+'="'+p.resolve(g)+'"'});var h=c.removeBugMuncherFromHtml(h);c.htmlB64=c.base64encode(h);h=null;var k=Math.ceil(c.htmlB64.length/c.packetLength);a("body").append('<img src="'+c.baseUrl+'img/ajax-loader.gif" style="position:absolute !important; top:-10000px !important" />');c.createProgressBar();c.loadHeadFile("script",c.baseUrl+"reports/create/"+location.hostname+"/"+k)};this.createProgressBar=function(){c.progressBar={progress:0,timer:false};a("#bugMuncher-alert").remove();a("body").append('<div id="bugMuncher-progress-bar"><h3>'+c.lang.analysingPage+'</h3><div><div id="bugMuncher-progress-bar-shell"><div id="bugMuncher-progress-bar-inner"></div></div></div>');var d=(a(window).height()/2)-a("#bugMuncher-progress-bar").outerHeight(true);a("#bugMuncher-progress-bar").css("top",d)};this.updateProgressBar=function(h,f,e){var d=328;c.progressBar.progress+=f;var g=Math.round(d*(c.progressBar.progress/100));if(h){a("#bugMuncher-progress-bar h3").text(h)}if(e){a("#bugMuncher-progress-bar-inner").animate({width:g},100)}else{a("#bugMuncher-progress-bar-inner").css({width:g})}};this.makePackets=function(){c.packets=new Array();for(start=0;start<c.htmlB64.length;start+=c.packetLength){c.packets.push({data:c.htmlB64.substr(start,c.packetLength),attempts:0,received:false})}c.htmlB64=null;a.each(c.packets,function(d,e){c.sendPacket(d)})};this.sendPacket=function(e){var d=c.packets[e];if(d.attempts>=3){c.alert(c.lang.generalError,false,3000);c.cleanUp()}else{d.attempts++;c.loadHeadFile("script",c.baseUrl+"reports/add_packet/"+c.reportId+"/"+e+"/"+d.data)}};this.finaliseReport=function(){var f=a(window).width()+"x"+a(window).height();var d=c.base64encode(document.location.href);var e=c.base64encode(a("#bugMuncher-description").val());c.loadHeadFile("script",c.baseUrl+"reports/finalise/"+c.reportId+"/"+f+"/"+d+"/"+e);$progressH3=a("#bugMuncher-progress-bar h3");$progressH3.next().remove();$progressH3.after('<img src="'+c.baseUrl+'img/ajax-loader.gif" id="bugMuncher-screenshot-load" />')};this.isWebsiteActive=function(d){if(d){a.BugMuncher.setUp()}};this.setReportId=function(d){if(d){c.reportId=d;c.updateProgressBar(false,10,true);c.makePackets()}else{c.alert(c.lang.generalError,false,3000);c.cleanUp()}};this.packetRecieved=function(f,h){if(!f){c.sendPacket(h)}else{c.packets[h].received=true;var e=0;a.each(c.packets,function(j,k){if(!k.received){e++}});var d=c.packets.length;var i=90/d;var g=d-e;c.updateProgressBar(false,i,false);if(e==0){c.updateProgressBar(c.lang.creatingScreenshot,0,false);c.finaliseReport()}}};this.reportReady=function(e){if(!e){c.alert(c.lang.generalError,false,3000);c.cleanUp()}else{c.cleanUp();if(c.options.showPreview){var d=c.baseUrl+"reports/preview/"+c.options.language+"/"+c.reportId}else{var d=c.baseUrl+"reports/complete/"+c.options.language+"/"+c.reportId}a("body").append('<iframe id="bugMuncher-preview-frame" width="800" height="500" scrolling="no" frameborder="0" src="'+d+'" />');a("body").append('<a href="#" id="bugMuncher-iframe-close-button">X</a>');a("#bugMuncher-iframe-close-button").mousedown(c.closePreviewFrame)}};this.error=function(){c.alert(c.lang.generalError,false,3000);c.cleanUp()};this.closePreviewFrame=function(d){if(typeof d!="undefined"){d.stopPropagation()}var e=a("#bugMuncher-preview-frame");e.remove();a("#bugMuncher-iframe-close-button").remove();return false};this.base64encode=function(f){keyStr="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";var d="";var n,l,j,m,k,h,g;var e=0;f=c.utf8_encode(f);while(e<f.length){n=f.charCodeAt(e++);l=f.charCodeAt(e++);j=f.charCodeAt(e++);m=n>>2;k=((n&3)<<4)|(l>>4);h=((l&15)<<2)|(j>>6);g=j&63;if(isNaN(l)){h=g=64}else{if(isNaN(j)){g=64}}d=d+keyStr.charAt(m)+keyStr.charAt(k)+keyStr.charAt(h)+keyStr.charAt(g)}d=d.replace(/\//g,"-");d=d.replace(/=/g,"");return d};this.utf8_encode=function(e){e=e.replace(/\r\n/g,"\n");var d="";for(var g=0;g<e.length;g++){var f=e.charCodeAt(g);if(f<128){d+=String.fromCharCode(f)}else{if((f>127)&&(f<2048)){d+=String.fromCharCode((f>>6)|192);d+=String.fromCharCode((f&63)|128)}else{d+=String.fromCharCode((f>>12)|224);d+=String.fromCharCode(((f>>6)&63)|128);d+=String.fromCharCode((f&63)|128)}}}return d};this.cleanUp=function(){a("#bugMuncher-progress-bar").remove();a("script").each(function(){if(this.src.indexOf(c.baseUrl+"reports/")==0){a(this).remove()}})};this.loadHeadFile=function(d,e,g){var f=document.createElement(d);if(d=="link"){f.setAttribute("rel","stylesheet");type="text/css";urlAttribute="href"}else{type="text/javascript";urlAttribute="src";f.setAttribute("charset","utf-8")}f.setAttribute("type",type);f.setAttribute(urlAttribute,e);document.getElementsByTagName("head")[0].appendChild(f);if(g){a(f).load(g)}};this.outerHTML=function(d){return d.outerHTML||(function(g){var f=document.createElement("div"),e;f.appendChild(g.cloneNode(true));e=f.innerHTML;f=null;return e})(d)};this.docHeight=function(){var d=document;return Math.max(Math.max(d.body.scrollHeight,d.documentElement.scrollHeight),Math.max(d.body.offsetHeight,d.documentElement.offsetHeight),Math.max(d.body.clientHeight,d.documentElement.clientHeight))};this.docWidth=function(){var d=document;return Math.max(Math.max(d.body.scrollWidth,d.documentElement.scrollWidth),Math.max(d.body.offsetWidth,d.documentElement.offsetWidth),Math.max(d.body.clientWidth,d.documentElement.clientWidth))};this.alert=function(g,f,e){a("#bugMuncher-alert").remove();if(!f){f=c.lang.errorMessageTitle}a("body").append('<div id="bugMuncher-alert"><h3><a href="#">X</a>'+f+"</h3><div><p>"+g+"</p></div></div>");var d=(a(window).height()/2)-a("#bugMuncher-alert").outerHeight(true);a("#bugMuncher-alert").css("top",d);if(e>0){a.alertTimer=setTimeout(function(){c.closeAlert()},e)}a("#bugMuncher-alert h3 a").mousedown(function(h){c.closeAlert();h.stopPropagation();return false}).click(function(){return false})};this.closeAlert=function(){clearTimeout(a.alertTimer);a("#bugMuncher-alert").fadeOut(200,function(){a("#bugMuncher-alert").remove()})};this.showHelp=function(){var e=c.lang;var d="";a.each(e.usageInstructions,function(f,g){d+=g+"</p><p>"});c.alert(d+'<a href="'+c.baseUrl+'page/help" target="_blank">'+e.fullUsageInstructionsLink+"</a>",e.usageInstructionsTitle,0)};this.changeMode=function(){if(a(this).attr("id")=="bugMuncher-option-highlight"){c.highLighting=true;a(this).addClass("bugMuncher-mode-button-on");a("#bugMuncher-option-censor").removeClass("bugMuncher-mode-button-on")}else{c.highLighting=false;a(this).addClass("bugMuncher-mode-button-on");a("#bugMuncher-option-highlight").removeClass("bugMuncher-mode-button-on")}return false};this.preparePage=function(){function d(e){a(e).contents().each(function(){if(this.id=="bugMuncher-options"){return true}if(this.nodeType==3){nodeVal=this.data;var f=/\s+/i;nodeVal=nodeVal.replace(f,"");if(nodeVal.length){words=this.data.split(f);wrappedWords="";a.each(words,function(g,h){wrappedWords+='<span class="bugMuncher-word">'+h+"</span> "});a(this).replaceWith(wrappedWords)}}d(this)})}};this.start=function(){if(c.active){return false}if(c.firstUse){if(c.options.showIntro){c.showHelp()}c.firstUse=false}var d=a("#bugMuncher-options").get(0);d.oldWidth=a("#bugMuncher-options").width();d.oldHeight=a("#bugMuncher-options").height();c.createFullPageOverlay();c.active=true;a(document).mousedown(c.dragStart);a(".bugMuncher-mode-button").click(c.changeMode);a("#bugMuncher-cancel-button").click(c.end);a("#bugMuncher-close-button").click(c.end);a("#bugMuncher-preview-button").click(c.sendForPreview);a("#bugMuncher-help").click(c.showHelp);a("#bugMuncher-options").mousedown(function(e){e.stopPropagation()});a(window).resize(c.end);a("#bugMuncher-options").animate({width:200,height:250,paddingLeft:5},300,function(){a("#bugMuncher-close-button").show()})};this.resetPage=function(){a(".bugMuncher-word").contents().each(function(){if(this.nodeType==3){a(this).unwrap()}})};this.end=function(){if(!c.active){return}c.closeAlert();a(".bugMuncher-overlay, .bugMuncher-highlight, .bugMuncher-censor").remove();a(".bugMuncher-highlighted-element").removeClass("bugMuncher-highlighted-element");c.cleanUp();c.closePreviewFrame();c.active=false;var d=a("#bugMuncher-options").get(0);a("#bugMuncher-close-button").hide();a("#bugMuncher-options").animate({width:d.oldWidth,height:d.oldHeight},300);a(document).unbind("mousedown",c.dragStart);a(".bugMuncher-mode-button").unbind("click");a("#bugMuncher-cancel-button, #bugMuncher-close-button").unbind("click");a("#bugMuncher-preview-button").unbind("click");a("#bugMuncher-help").unbind("click");a("#bugMuncher-options").unbind("mousedown");a(window).unbind("resize",c.end);return false};this.construct=function(){c.loadHeadFile("link",c.cssFile);c.loadHeadFile("script",c.jsUriFile)};this.setUp=function(){if(typeof BugMuncherOptions!="undefined"){c.options=a.extend(c.options,BugMuncherOptions)}c.loadHeadFile("script",this.baseUrl+"js/lang/"+c.options.language+".js")};this.createPanel=function(){var f=BugMuncherLanguage;c.lang=f;if(typeof c.options.labelText!="undefined"){f.labelText=c.options.labelText}a("body").append('<!-- StartBugMuncher --><div id="bugMuncher-options" /><!-- EndBugMuncher -->');var d=a("#bugMuncher-options");d.append('<a href="#" id="bugMuncher-close-button">X</a><h2>'+f.labelText+"</h2>");d.append('<form action="#" method="post" id="bugMuncher-form" target="_blank"></form>');a("#bugMuncher-form").append('<a class="bugMuncher-mode-button bugMuncher-mode-button-on bugMuncher-gradient" id="bugMuncher-option-highlight">'+f.highlightButtonLabel+"</a>");a("#bugMuncher-form").append('<a class="bugMuncher-mode-button bugMuncher-gradient" id="bugMuncher-option-censor">'+f.blackoutButtonLabel+"</a>");a("#bugMuncher-form").append('<label id="bugMuncher-describe-label">'+f.descriptionInstruction+":</label>");a("#bugMuncher-form").append('<textarea rows="5" cols="20" name="description" id="bugMuncher-description"></textarea>');a("#bugMuncher-form").append('<p id="bugMuncher-buttons" />');a("#bugMuncher-buttons").append('<a class="bugMuncher-submit-button bugMuncher-gradient" id="bugMuncher-preview-button">'+(c.options.showPreview?f.previewButtonLabel:f.submitButtonLabel)+"</a>");a("#bugMuncher-buttons").append('<a class="bugMuncher-submit-button bugMuncher-gradient" id="bugMuncher-cancel-button">'+f.cancelButtonLabel+"</a>");a("#bugMuncher-form").append('<input type="hidden" name="pagehtml" id="bugMuncher-page-html" />');a("#bugMuncher-form").append('<input type="hidden" name="pageheight" id="bugMuncher-page-height" />');a("#bugMuncher-form").append('<input type="hidden" name="pagewidth" id="bugMuncher-page-width" />');a("#bugMuncher-form").append('<input type="hidden" name="domain" id="bugMuncher-domain" />');a("#bugMuncher-form").append('<input type="hidden" name="url" id="bugMuncher-url" />');d.append('<p id="bugMuncher-powered-by"><a id="bugMuncher-help" href="'+c.baseUrl+'page/help" target="_blank">'+f.helpLink+"</a>"+f.poweredBy+' <a id="bugMuncher-home-link" href="http://www.bugmuncher.com">BugMuncher</a></p>');a("body").append('<div id="bugMuncher-text-width">'+f.labelText+"</div>");a("#bugMuncher-text-width").css({position:"absolute",visibility:"hidden",height:"auto",width:"auto",fontSize:"14px",fontWeight:"bold",fontFamily:"helvetica, arial, sans-serif"});var e=a("#bugMuncher-text-width").get(0).clientWidth+10;a("#bugMuncher-text-width").remove();d.css({width:e,backgroundColor:c.options.backgroundColor,border:"solid 2px "+c.options.borderColor});d.css(c.options.position,"-2px");a("#bugMuncher-close-button").hide();a("#bugMuncher-options h2").click(c.start)};this.construct()}a.BugMuncher=new b();a(function(){if(a.browser.msie&&(a.browser.version<=6)){return false}else{a.BugMuncher.loadHeadFile("script",a.BugMuncher.baseUrl+"reports/check_active/"+location.hostname)}})})(jQuery);
