(function(g,e,i){var d=function(k){return function(){g("#create-"+k+"-form").find("input[type='text']").val("").end().find("input[type='checkbox']").removeAttr("checked").end().find("> .edit-mode").val(0).end().find("> fieldset > legend").text("Create new "+k).end().clearModelErrors().toggle();g("#"+k+"-select").toggle();return false}};var a=function(n,p,k,q,o){var m=n.find("tr:last");if(m.find("td").length===o){m=g("
");n.append(m)}var l=g(" | ").append(g("").attr({type:"checkbox",name:p,value:k,id:p+"_"+k,checked:"checked"})).append(g("").attr("for",p+"_"+k).text(q));m.append(l);n.append(m)};var f=function(){g("#create-system-link, #create-system-cancel").click(d("system"));g("#create-system-submit").click(function(){var k=false;return function(){if(k){return false}k=true;var l=g("#create-system-form");var n=g(this).find(".submit-icon");var m={SystemName:g("#SystemName").val(),SystemAbbreviation:g("#SystemAbbreviation").val(),SystemReleaseDate:g("#SystemReleaseDate").val(),SystemIcon:g("#SystemIcon").val()};var p="/system/create";var o=l.find("> .edit-mode").val();if(o>0){p="/system/edit";m.SystemId=o}g.ajax(p,{type:"POST",data:m,beforeSend:function(){l.clearModelErrors();n.toggleClass("submit-icon loading-icon")},success:function(q,r,s){if(q.Error!==null){l.applyModelErrors(q.Error,q.Data);return}if(o<=0){a(g("#system-checkbox-table"),"SystemIds",q.Data.Id,q.Data.Abbreviation,6)}else{g("#system-checkbox-table").find("input[value='"+q.Data.Id+"'] + label").text(q.Data.Abbreviation)}d("system")()},complete:function(){k=false;n.toggleClass("submit-icon loading-icon")}});return false}}());g(".edit-system-link").click(function(){var l=g(this);l.toggleClass("edit-icon loading-icon");var k=l.siblings("input[name='SystemIds']").val();g.vgquotes.getResourceById("system",k,function(m){l.toggleClass("edit-icon loading-icon");if(m===null){alert("Unable to fetch system "+k);return}d("system")();g("#create-system-form > .edit-mode").val(k);g("#create-system-form > fieldset > legend").text("Edit System");g("#SystemName").val(m.Name);g("#SystemAbbreviation").val(m.Abbreviation);g("#SystemReleaseDate").val(g.vgquotes.parseAndFormatDate(m.ReleaseDate));g("#SystemIcon").val(m.Icon)});return false});g(".delete-system-link").click(function(){var l=g(this);l.toggleClass("delete-icon loading-icon");var k=l.siblings("input[name='SystemIds']").val();g.ajax("/system/delete",{type:"POST",data:{id:k},success:function(m,n,o){if(m.Error!==null){alert(m.Error);return}l.parent().empty()},complete:function(){l.toggleClass("delete-icon loading-icon")}});return false})};var h=function(){g("#create-game-link, #create-game-cancel").click(d("game"));g("#create-game-submit").click(function(){var k=false;return function(){if(k){return false}k=true;var l=[],q=[],m=[],p={GameName:g("#GameName").val(),GameWebsite:g("#GameWebsite").val(),GameIcon:g("#GameIcon").val()};g("input:checked[name='GameRegions']").each(function(u,t){if(typeof(p.GameRegions)==="undefined"){p.GameRegions=[]}p.GameRegions.push(t.value)});g("input:checked[name='SystemIds']").each(function(u,t){if(typeof(p.SystemIds)==="undefined"){p.SystemIds=[]}p.SystemIds.push(t.value)});g("input:checked[name='PublisherIds']").each(function(u,t){if(typeof(p.PublisherIds)==="undefined"){p.PublisherIds=[]}p.PublisherIds.push(t.value)});var s=g("#create-game-form");var r=g(this).find(".submit-icon");var o="/game/create";var n=g("#create-game-form > .edit-mode").val()==1;if(n){o="/game/edit";p.GameId=g("#GameId").val()}g.ajax(o,{type:"POST",data:p,traditional:true,beforeSend:function(){s.clearModelErrors();r.toggleClass("submit-icon loading-icon")},success:function(t,u,v){if(t.Error!==null){s.applyModelErrors(t.Error,t.Data);return}if(!n){g("#GameId").append(g("").attr({value:t.Data.Id}).text(t.Data.Name)).val(t.Data.Id)}else{g("#GameId option[value='"+t.Data.Id+"']").text(t.Data.Name)}d("game")()},complete:function(){k=false;r.toggleClass("submit-icon loading-icon")}});return false}}());g("#edit-game-link").click(function(){g("#edit-game-link").toggleClass("edit-icon loading-icon");var k=g("#GameId").val();g.vgquotes.getResourceById("game",k,function(l){g("#edit-game-link").toggleClass("edit-icon loading-icon");if(l===null){alert("Unable to fetch game "+k);return}d("game")();g("#create-game-form > .edit-mode").val(1);g("#create-game-form > fieldset > legend").text("Edit Game");g("#GameName").val(l.Name);g("#GameWebsite").val(l.Website);g("#GameIcon").val(l.Icon);g.each(l.Regions,function(){g("input[name='GameRegions'][value='"+this+"']").attr("checked","checked")});g.each(l.Systems,function(){g("input[name='SystemIds'][value='"+this.Id+"']").attr("checked","checked")});g.each(l.Publishers,function(){g("input[name='PublisherIds'][value='"+this.Id+"']").attr("checked","checked")})});return false});g("#delete-game-link").click(function(){var l=g(this);var k=g("#GameId").val();g.ajax("/game/delete",{type:"POST",data:{id:k},beforeSend:function(){l.toggleClass("delete-icon loading-icon")},success:function(m,n,o){if(m.Error!==null){alert(m.Error);return}g("#GameId option[value='"+k+"']").remove()},complete:function(){l.toggleClass("delete-icon loading-icon")}});return false})};var b=function(){g("#create-publisher-link, #create-publisher-cancel").click(d("publisher"));g("#create-publisher-submit").click(function(){var k=false;return function(){if(k){return false}k=true;var l=g("#create-publisher-form");var n=g(this).find(".submit-icon");var m={PublisherName:g("#PublisherName").val(),PublisherWebsite:g("#PublisherWebsite").val()};var o="/publisher/create";var p=l.find("> .edit-mode").val();if(p>0){o="/publisher/edit";m.PublisherId=p}g.ajax(o,{type:"POST",data:m,beforeSend:function(){l.clearModelErrors();n.toggleClass("submit-icon loading-icon")},success:function(q,r,s){if(q.Error!==null){l.applyModelErrors(q.Error,q.Data);return}if(p<=0){a(g("#publisher-checkbox-table"),"PublisherIds",q.Data.Id,q.Data.Name,4)}else{g("#publisher-checkbox-table").find("input[value='"+q.Data.Id+"'] + label").text(q.Data.Name)}d("publisher")()},complete:function(){k=false;n.toggleClass("submit-icon loading-icon")}});return false}}());g(".edit-publisher-link").click(function(){var l=g(this);l.toggleClass("edit-icon loading-icon");var k=l.siblings("input[name='PublisherIds']").val();g.vgquotes.getResourceById("publisher",k,function(m){l.toggleClass("edit-icon loading-icon");if(m===null){alert("Unable to fetch publisher "+k);return}d("publisher")();g("#create-publisher-form > .edit-mode").val(k);g("#create-publisher-form > fieldset > legend").text("Edit Publisher");g("#PublisherName").val(m.Name);g("#PublisherWebsite").val(m.Website)});return false});g(".delete-publisher-link").click(function(){var k=g(this);k.toggleClass("delete-icon loading-icon");g.ajax("/publisher/delete",{type:"POST",data:{id:k.siblings("input[name='PublisherIds']").val()},success:function(l,m,n){if(l.Error!==null){alert(l.Error);return}k.parent().empty()},complete:function(){k.toggleClass("delete-icon loading-icon")}});return false})};var c=function(){var k=function(s){if(g("#new-category-name").length>0){return}var p=function(){var v=g(this);var t="/category/create";var u={CategoryName:o.val()};if(s!==i){t="/category/edit";u.CategoryId=s}g.ajax(t,{data:u,type:"POST",beforeSend:function(){v.toggleClass("submit-icon loading-icon")},success:function(w,y,z){if(w.Error!==null){alert(w.Error);return}if(s===i){var A=g("").css("display","none").attr({id:"category_"+w.Data.Id,type:"checkbox",name:"CategoryIds",checked:"checked"}).val(w.Data.Id);var x=g("").css("display","none").attr("for",A.attr("id")).text(w.Data.Name);g("#new-category-name").before(A).before(x)}else{g("#new-category-name").siblings("label").text(w.Data.Name)}},complete:function(){v.toggleClass("submit-icon loading-icon");var w=g("#new-category-name").parent();w.children(":visible").remove().end().children().show()}});return false};var r=g("#category-checkbox-table"),q;var n=g("").attr({href:"#",title:"submit"}).addClass("submit-icon").click(p);var m=g("").attr({href:"#",title:"cancel"}).addClass("cancel-icon");var o=g("").attr({id:"new-category-name",type:"text"}).bind("keyup",function(t){if(t.which===13){t.preventDefault();p.call(g(this).siblings(".submit-icon").get(0))}else{if(t.which===27){t.preventDefault();m.click()}}});if(s!==i){q=r.find("input[value='"+s+"']").parent();q.children().hide();m.click(function(){o.remove();n.remove();m.remove();q.children().show();return false});o.val(q.find("label").text());q.append(o).append(n).append(m);o.select();return}var l=r.find("tr:last");if(l.length===0){l=g("
");r.append(l)}q=g(" | ");if(l.find("td").length===5){l=g("
");r.append(l)}l.append(q);o.val("Category name");m.click(function(){q.remove();return false});q.append(o).append(n).append(m);o.select()};g("#create-category-link").click(function(){k.call(this);return false});g(".edit-category-link").click(function(){var l=g(this).siblings("input[name='CategoryIds']").val();k.call(this,l);return false});g(".delete-category-link").click(function(){var m=g(this).siblings("input[name='CategoryIds']").val();var l=g(this);g.ajax("/category/delete",{type:"POST",data:{id:m},beforeSend:function(){l.toggleClass("delete-icon loading-icon")},success:function(n){if(n.Error!==null){alert(n.Error);return}g("#category-checkbox-table").find("input[value='"+m+"']").parent().empty()},complete:function(){l.toggleClass("delete-icon loading-icon")}});return false})};var j=function(){g(".dismiss-flag-link").click(function(){var l=g(this);var k=l.parents(".quote-flag");var n=k.find(".quote-flag-id").val();var m=g("#quote-id").val();g.ajax("/dismiss-flag",{type:"POST",data:{quoteId:m,flagId:n},success:function(o,p,q){if(o.Error!==null){alert(o.Error);return}k.remove()}});return false})};(function(){g(document).ready(function(){h();f();b();c();j()})}())}(jQuery,window));