$(function(){
if(!("trim" in String.prototype)){
String.prototype.trim=function(){
return this.replace(/^\s+|\s+$/g,"");
};
}
window.global={iPage:1,bNavigate:false,eventTarget:null,confirmFunc:null,cart:{sCartId:null,sHMAC:null,iItems:0,nTotal:0},aCategories:["<li class='lookup' id='our_picks' ids='0143038257,0156034026,1452829535,0393337200,0805091289,1608460711,0061834785'>Featured</li>","<li class='keyword' type='browse_node' keywords='2'>Bio & Memoirs</li>","<li class='keyword' type='browse_node' keywords='4'>Children's Books</li>","<li class='keyword' type='browse_node' keywords='6'>Food & Cooking</li>","<li class='keyword' type='browse_node' keywords='9'>History</li>","<li class='keyword' type='browse_node' keywords='17'>Literature & Fiction</li>","<li class='keyword' type='browse_node' keywords='18'>Mystery & Thrillers</li>","<li class='keyword' type='browse_node' keywords='53'>Non Fiction</li>","<li class='keyword' type='browse_node' keywords='173507'>Technical</li>","<li class='keyword' type='browse_node' keywords='21'>Reference</li>","<li class='keyword' type='browse_node' keywords='22'>Religion & Spirituality</li>","<li class='keyword' type='browse_node' keywords='23'>Romance</li>","<li class='keyword' type='browse_node' keywords='75'>Science</li>","<li class='keyword' type='browse_node' keywords='26'>Sports</li>","<li class='keyword' type='browse_node' keywords='27'>Travel</li>"],aConvEntities:[{from:"Â",to:""},{from:"Ã©",to:"é"},{from:"â€™",to:"’"},{from:"â€”",to:"—"},{from:"Ã»",to:"û"}]};
dispCategories();
$("#links a[href='#about']").click(function(e){
about(e);
});
$("#links a[href='#contact']").click(function(e){
contact(e);
});
$("form").submit(function(e){
keywordSearch(e,true);
});
$.get("picks.xml",function(_1){
displayResults(_1,true);
});
$("#categories li.lookup#our_picks").click();
$("#alert_dialog").dialog({autoOpen:false,modal:true,width:500,buttons:{Ok:function(){
$(this).dialog("close");
}}});
$("#confirm_dialog").dialog({autoOpen:false,modal:true,width:500,buttons:{Ok:function(){
if(window.global.confirmFunc!=null){
window.global.confirmFunc();
}
$(this).dialog("close");
},Cancel:function(){
window.global.bConfirm=false;
$(this).dialog("close");
}}});
$("#review_dialog").dialog({autoOpen:false,width:600});
$("#cart a#back_link").click(function(e){
$("#search_browse").show();
setCartLink();
$("#cart").hide();
e.preventDefault();
});
$("#cart").hide();
});
function waitCursor(){
$("#wrapper").css("cursor","wait");
};
function autoCursor(){
$("#wrapper").css("cursor","auto");
};
function showAlertDlg(_2,_3,_4){
if(_3!==undefined){
$("#alert_dialog").dialog("option","title",_3);
}else{
$("#alert_dialog").dialog("option","title","We seem to have a situation here");
}
$("#alert_dialog #msg").html(_2);
if(_4!==undefined){
$("#alert_dialog #note").html("<p></p>"+_4);
}else{
$("#alert_dialog #note").empty();
}
$("#alert_dialog").dialog("open");
};
function showReviewDlg(_5,_6,_7){
$("#review_dialog td.image").empty();
if(_7!==undefined){
$("#review_dialog td.image").append("<img alt='medium' src='"+_7+"'></img>");
}
$("#review_dialog").dialog("option","title",_5);
$("#review_dialog td.review div").html(convertEntities(unescape(_6)));
$("#review_dialog").dialog("open");
};
function convertEntities(_8){
for(var i=0,_9=window.global.aConvEntities.length;i<_9;++i){
while(_8.search(window.global.aConvEntities[i].from)!=-1){
_8=_8.replace(window.global.aConvEntities[i].from,window.global.aConvEntities[i].to,"g");
}
}
var _a=/â€./gi;
var _b;
while((_b=_8.match(_a))!=null){
if(_b[0].charCodeAt(2)==157){
_8=_8.replace(_b[0],"”");
}else{
_8=_8.replace(_b[0]," ");
}
}
return _8;
};
function showConfirmDlg(_c,_d){
if(_d!==undefined){
$("#confirm_dialog #ask_detail").html("<p></p>"+_d);
}else{
$("#confirm_dialog #ask_detail").empty();
}
$("#confirm_dialog #ask").html(_c);
$("#confirm_dialog").dialog("open");
};
function dispCategories(){
$("#categories li").remove();
for(var i=0,_e=window.global.aCategories.length;i<_e;++i){
$("#categories ul").append(window.global.aCategories[i]);
}
$("#categories li").wrap("<span><a href=\"#\"></a></span>");
$("#categories li.lookup").click(function(e){
lookupSearch(e);
});
$("#categories li.keyword").click(function(e){
keywordSearch(e);
});
};
function lookupSearch(e){
waitCursor();
$("#status").html("Looking up "+$(e.target).html()+"...");
var _f="";
if(window.global.bNavigate){
_f="&Page="+window.global.iPage;
window.global.bNavigate=false;
}else{
window.global.iPage=1;
}
$.ajax({url:"/cgi-bin/Amazon.pl",success:function(_10){
if($("Error",_10).size()!=0){
autoCursor();
showAlertDlg($("Error Message",_10).text(),"Daisy oopsy..");
}else{
$("#status").html("Getting lookup info: done");
autoCursor();
window.global.eventTarget=e.target;
displayResults(_10);
}
},data:"Operation=ItemLookup&ItemId="+$(e.target).attr("ids")+_f,error:function(_11,_12,_13){
autoCursor();
showAlertDlg(_13,"Daisy oopsy...");
}});
e.preventDefault();
};
function keywordSearch(e,_14){
waitCursor();
var _15=_14?$("input#search-text").val().trim():$(e.target).html();
var _16=_14?$("input#search-text").val().trim():$(e.target).attr("keywords");
var _17="";
if(!_14){
var _17=$(e.target).attr("type");
if(_17=="power"){
_17="&Power=1";
}else{
if(_17=="browse_node"){
_17="&Browse=1";
}
}
}
$("#status").html("Searching for "+_15+"...");
var _18="";
if(window.global.bNavigate){
_18="&Page="+window.global.iPage;
window.global.bNavigate=false;
}else{
window.global.iPage=1;
}
$.ajax({url:"/cgi-bin/Amazon.pl",success:function(_19){
if($("Error",_19).size()!=0){
autoCursor();
showAlertDlg($("Error Message",_19).text(),"Daisy oopsy...");
}else{
$("#status").html("Getting search results: done");
autoCursor();
window.global.eventTarget=e.target;
displayResults(_19);
}
},data:"Operation=ItemSearch&Keywords="+_16+_17+_18,error:function(_1a,_1b,_1c){
autoCursor();
showAlertDlg(_1c,"Teri to...");
}});
e.preventDefault();
};
function about(e){
showAlertDlg("Kutubnuma: then and now","Change being the only constant...","11 years ago, we boasted 'the largest selection of "+"urdu books on the web'. Today we tap into the immense catalog of Amazon books to present titles from and about Pakistan.");
e.preventDefault();
};
function contact(e){
showAlertDlg("<a href='mailto:info@kutubnuma.biz'>info@kutubnuma.biz</a>","Mat ched malangaan noo","Suggest, "+"inform, illuminate, criticize or otherwise at <a href=\"mailto:info@kutubnuma.biz\">info@kutubnuma.biz</a>.");
e.preventDefault();
};
function displayResults(_1d,_1e){
$("#results").empty();
var _1f="Featured";
var _20="";
if(window.global.eventTarget!=null){
_20=$(window.global.eventTarget).attr("class");
if(_20=="lookup"||_20=="keyword"){
_1f=$(window.global.eventTarget).text();
}else{
if(_20=="search-form"){
_1f="Search Results";
}
}
}
$("Item",_1d).each(function(){
var _21=$("ASIN",this).text();
var _22=$("SmallImage:first URL",this).text();
if(_22.length==0){
_22="images/no-img-sm.gif";
}
var _23=$("MediumImage:first URL",this).text();
if(_23.length==0){
_23="images/no-img-sm.gif";
}
var _24="fetching image...";
if($("SmallImage URL",this).size()==0){
_24="image not available";
}
var _25=$("Title",this).text();
var _26="";
if($("Author",this).size()!=0){
_26=" by "+"<span class='author'>"+$("Author",this).map(function(){
return $(this).text();
}).get().join(", ")+"</span>";
}
var _27=$("PublicationDate",this).text();
var _28="";
if($("Language:first Name",this).size()!=0){
_28="<span class='lang'>"+$("Language:first Name",this).text()+"</span>; ";
}
var _29=$("Binding",this).text();
var _2a=$("OfferListing:first",this);
var _2b=$("Price FormattedPrice",_2a).text();
var _2c=escape($("OfferListingId",_2a).text());
var _2d;
if(_1e===undefined){
_2d="<div class='price'>"+_2b+"<a class='buy' offer='"+_2c+"' href='#'> Buy</a></div>";
}else{
_2d="<div class='price'>Fetching current price...<div/>";
}
var _2e="Review not available";
if($("EditorialReviews Content:first",this).size()!=0){
_2e="<div class='price'>"+$("EditorialReviews Content:first",this).text()+"</div>";
}
var _22="<a href='#' asin='"+_21+"'><img alt='"+_24+"' src='"+_22+"' review='"+escape(_2e)+"' medium='"+_23+"'></img></a>";
$("#results").append("<table class='detail'><tr><td class='image'>"+_22+"</td><td class='text'>"+"<span class='title'>"+_25+"</span>"+_26+" (<span class='binding'>"+_29+"</span> - "+_28+"<span class='date'>"+_27+"</span>)"+_2d+"</td></tr></table>");
$("#results .image a[asin='"+_21+"']").hover(function(e){
showReviewDlg(_25,$(e.target).attr("review"),$(e.target).attr("medium"));
},function(e){
$("#review_dialog").dialog("close");
});
});
$("#results .detail").wrap("<div class=\"book\"></div>");
$("#results a.buy").click(function(e){
addToCart(e,$(this).attr("offer"));
});
if($("Items TotalPages",_1d).size()>0){
var _2f=Number($("Items TotalPages",_1d).text());
var _30="<div class='pageinfo'><span class='prev'></span>page <span class='curpage'>"+window.global.iPage+"</span> of <span id='totpages'>"+_2f+"</span><span class='next'></span></div>";
$("#results").append(_30);
$("#results").prepend(_30);
if(window.global.iPage>1){
$("#results .prev").append("<a href='#'>prev</a>");
$("#results .prev").click(function(e){
window.global.iPage--;
window.global.bNavigate=true;
(_20=="search-form")?$("form").submit():$(window.global.eventTarget).click();
});
}
if(window.global.iPage<_2f){
$("#results .next").append("<a href='#'>next</a>");
$("#results .next").click(function(e){
window.global.iPage++;
window.global.bNavigate=true;
(_20=="search-form")?$("form").submit():$(window.global.eventTarget).click();
});
}
}
$("#result_title").html(_1f);
setCartLink();
};
function addToCart(e,_31){
waitCursor();
$("#status").html("Adding item to cart...");
syncCartParams();
var _32=window.global.cart.sCartId==null?"CartCreate":"CartAdd";
_32="Operation="+_32+"&OfferId="+_31+"&Quantity=1";
if(window.global.cart.sCartId!=null){
_32+="&HMAC="+window.global.cart.sHMAC+"&CartId="+window.global.cart.sCartId;
}
$.ajax({url:"/cgi-bin/Amazon.pl",success:function(_33){
if($("Error",_33).size()!=0){
autoCursor();
var _34=$("Error Message",_33).text();
if(_34.match(/already in your cart/)){
_34="You have already added this time. You can change the quantity if you want by viewing your cart.";
}
showAlertDlg(_34,"Oops...");
}else{
$("#status").html("Adding item to cart: done");
autoCursor();
showCart(_33);
}
},data:_32,error:function(_35,_36,_37){
autoCursor();
showAlertDlg(_37,"Teri to...");
}});
e.preventDefault();
};
function getCart(){
waitCursor();
$("#status").html("Fetching cart...");
syncCartParams();
$.ajax({url:"/cgi-bin/Amazon.pl",success:function(_38){
if($("Error",_38).size()!=0){
autoCursor();
showAlertDlg($("Error Message",_38).text(),"Teri to...");
}else{
$("#status").html("Fetching cart: done");
autoCursor();
showCart(_38);
}
},data:"Operation=CartGet&HMAC="+window.global.cart.sHMAC+"&CartId="+window.global.cart.sCartId,error:function(_39,_3a,_3b){
autoCursor();
showAlertDlg(_3b,"Teri to...");
}});
};
function showCart(_3c,_3d){
refreshCartParams(_3c);
$("#cart_items").empty();
$("#clear_cart").empty();
$("#checkout").empty();
$("#cart_header").text("Your Cart"+(_3d?" (directed to Amazon for final payment)":""));
var _3e="<table><thead><tr><th>Title</th><th class=\"number\">Price</th><th>Quantity</th><th class=\"number\">Total</th></tr></thead><tbody>";
$("CartItem",_3c).each(function(i){
var _3f=parseInt($("Quantity",this).text());
var _40;
if(_3d){
_40=$("Quantity",this).text();
}else{
_40="<select item_id='"+$("CartItemId",this).text()+"'>";
var _41;
for(var j=0;j<=5;++j){
_41=(_3f==j?"selected":"");
_40+="<option value='"+j+"' "+_41+">"+j+"</option>";
}
_40+="</select>";
}
_3e+="<tr>"+"<td>"+(i+1)+". "+$("Title",this).text()+"</td>"+"<td class=\"number\">"+$("Price>FormattedPrice",this).text()+"</td>"+"<td class=\"quantity\">"+_40+"</td>"+"<td class=\"number\">"+$("ItemTotal>FormattedPrice",this).text()+"</td>"+"</tr>";
});
_3e+="<tr><td colspan='3'>Total before Shipping</td><td class='number'>"+$("Cart>SubTotal FormattedPrice",_3c).text()+"</td></tr></table>";
$("#cart_items").append(_3e);
$("#cart_items select").change(function(e){
modifyCart($(this).attr("item_id"),$(e.target).attr("value"));
e.preventDefault();
});
if(!_3d){
$("#clear_cart").append("<a href='#'>Clear Cart</a>");
$("#clear_cart a").click(function(e){
clearCart(e);
});
$("#checkout").append("<a href='#'>Proceed to Checkout</a>");
$("#checkout a").click(function(e){
checkout(e,_3c);
});
}
$("#search_browse").hide();
$("#cart").show();
};
function modifyCart(_42,_43){
waitCursor();
syncCartParams();
$("#status").html("Modifying cart...");
$.ajax({url:"/cgi-bin/Amazon.pl",success:function(_44){
if($("Error",_44).size()!=0){
autoCursor();
showAlertDlg($("Error Message",_44).text(),"Teri to...");
}else{
$("#status").html("Modifying cart: done");
autoCursor();
showCart(_44);
}
},data:"Operation=CartModify&HMAC="+window.global.cart.sHMAC+"&CartId="+window.global.cart.sCartId+"&ItemId="+_42+"&Quantity="+_43,error:function(_45,_46,_47){
autoCursor();
showAlertDlg(_47,"Teri to...");
}});
};
function clearCart(e){
waitCursor();
syncCartParams();
$("#status").html("Clearing cart...");
$.ajax({url:"/cgi-bin/Amazon.pl",success:function(_48){
if($("Error",_48).size()!=0){
autoCursor();
showAlertDlg($("Error Message",_48).text(),"Teri to...");
}else{
$("#status").html("Clearing cart: done");
autoCursor();
clearCartParams();
$("#cart a#back_link").click();
}
},data:"Operation=CartClear&HMAC="+window.global.cart.sHMAC+"&CartId="+window.global.cart.sCartId,error:function(_49,_4a,_4b){
autoCursor();
showAlertDlg(_4b,"Teri to...");
}});
e.preventDefault();
};
function refreshCartParams(_4c){
if(window.global.cart.sCartId==null){
window.global.cart.sCartId=$("CartId",_4c).text();
window.global.cart.sHMAC=$("URLEncodedHMAC",_4c).text();
}
window.global.cart.iItems=parseInt($("CartItem",_4c).size());
window.global.cart.nTotal=$("Cart>SubTotal FormattedPrice",_4c).text();
$.cookie("kutubnuma.cart",JSON.stringify(window.global.cart));
};
function setCartLink(){
syncCartParams();
if(window.global.cart.iItems>0){
var _4d="item";
_4d+=window.global.cart.iItems>1?"s":"";
$("#result_wrapper #cart_link").html("Your Cart: <a href='#'>"+window.global.cart.iItems+" "+_4d+" ("+window.global.cart.nTotal+")</a>");
$("#result_wrapper #cart_link a").click(function(e){
getCart();
e.preventDefault();
});
}else{
$("#result_wrapper #cart_link").empty();
}
};
function checkout(e,_4e){
window.global.confirmFunc=function(){
showCart(_4e,true);
clearCartParams();
window.open($("PurchaseURL",_4e).text());
};
showConfirmDlg("Finalize Purchase?","You will be continuing onwards with Amazon.com for shipping and payment");
e.preventDefault();
};
function clearCartParams(){
window.global.cart.sCartId=null;
window.global.cart.sHMAC=null;
window.global.cart.iItems=0;
window.global.cart.nTotal=0;
$.cookie("kutubnuma.cart",null);
};
function syncCartParams(){
if(window.global.cart.sCartId==null){
var _4f=$.cookie("kutubnuma.cart");
if(_4f!=null){
window.global.cart=JSON.parse(_4f);
}else{
$.cookie("kutubnuma.cart",JSON.stringify(window.global.cart));
}
}
};


