$(document).ready(function(){
$('.flyout a').flyout();






$('.btn').each(function(){
// find the DOM element we're processing
var b = $(this);
// find the current text or value of the currently processed .btn element
var tt = b.text() || b.val();
// find the href value - only for normal buttons
var href = $(this).attr('href') || '';
// make a note of the parent form
var parentForm = $(this).parents('form');
// work out what type of element we're processing.
var type = $(this).attr('type');
if(type == 'submit' || type == 'reset') {
// just hide the button, so the form still submits when enter key is pressed on a field
$(this).hide();
// insert the replacement button after the existing hidden element
b = $('' + tt + '').insertAfter(this);
// add a click listener to the button
b.click(function() {
if(type == 'submit') {
parentForm.submit();
} else if(type == 'reset') {
parentForm.reset();
}
return false;
});
// allows for the enter button to submit form contents cross-browser
parentForm.keyup(function(e) {
if(e.keyCode == 13) {
parentForm.submit();
return false;
}
});
} else {
// normal Anchor Tag, just replace the contents of the element with the button formatting
b = $(this).html('' + tt + '');
}
});

});




function setlang(){
    ln=$("#language").val();
    pt=document.location.pathname;
    if (pt.search(/en/) >0 || pt.search(/ar/) >0) {
        if (ln=='en')eski='ar';
        else eski='en';
        pt=pt.replace('/'+eski+'/','/'+ln+'/');
        $("#next").val(pt);
        }
    $('#langform').submit();
}





search_cache='';
function search_set(focus,obj,def){
    if(focus==1 && obj.value==def){
        search_cache=obj.value;
        obj.value='';
        }
    else{
        if (obj.value==''){
            if (search_cache!='')obj.value=search_cache;
            else obj.value=def;
        }
    }
}


function add2cart(id){
    $("#bookid").val(id);
    $("#bookform").submit();
    }
function removeFromCart(id){
    $("#removeitem").val(id);
    $("#maincart").submit();
    }





function getCities(id){
    country=$("#"+id);
    var index=country.attr('id').replace("id_address_set-","").replace("-country","");
    selectedcounty=country.val();
    selectedcity=$("#city_"+index).val();

    if (selectedcounty)$.get("/l10n/cities/",{ countryid:selectedcounty,selected:selectedcity},function(data){$("#id_address_set-"+index+"-city").html(data);});
    }

function getCities41(val){
    $.get("/l10n/cities/",{ countryid:val},function(data){$("#id_city").html(data);});
    }


function findPos(obj) {

	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}


function gzlSptMsj(){
    m=$("#msjbox");
    m.fadeTo(2000,0.0).hide('slow');
    setTimeout("m.css('z-index',-2)",2000);
    }

function mbox(msj,nsn,hideafter){
    pos=false;
    if(typeof(nsn)=='undefined' || !nsn ){
        mouseX = mouseY = 0;
        $().mousemove(function(e) { mouseX = e.pageX; mouseY = e.pageY; });
        pos=[mouseX,mouseY];
        }
    else if(typeof(nsn)=='string'){
            nsn=$('#'+nsn)[0]
        }
    if(!pos && nsn)pos=findPos(nsn);
    if (!$("#msjbox")[0])$("body").append("<div id='msjbox'></div>");
    m=$("#msjbox");

    m.hide();
    m.html(msj);
    m.css('left',pos[0]);
    m.css('top',pos[1]+20);
    m.fadeTo(1,0.3).show().fadeTo(400,0.7).css('z-index',1000);
    setTimeout('gzlSptMsj()',8000);
    if(hideafter)$("#"+hideafter).hide('slow');
    }

function showSearchOptions(nsn){
    if(nsn)pos=findPos(nsn);
    //if (!$("#msjbox")[0])$("body").append("<div id='msjbox'></div>");
    m=$("#searchbox");
    //m.hide();
    m.css('left',pos[0]-49+position_fix);
    m.css('top',pos[1]+29);
    m.show('slow').fadeTo(400,0.9).css('z-index',1000);
    }

function getBookCommentForm(){
    $.get("/store/add_book_comment/",{},function(data){$("#xcontainer").html(data).show('slow');});
    }

function getBookComments(page){
    $.get("/store/book_comments/"+$("#bid").val(),{'page':page},function(data){$("#comments").html(data).show('slow');$('.dstars').rating();});
    }

function rateBook(rating,nsn){
    bid=$("#bid").val()
    $.post("/store/add_book_comment/",{'rating':rating,'bookid':bid},function(data){mbox(data,nsn)});
    }

function reviewBook(nsn){
    s=$("#id_title").val()
    t=$("#id_comment").val()
    bid=$("#bid").val()
    $.post("/store/add_book_comment/",{'title':s,'comment':t,'bookid':bid},function(data){mbox(data,nsn,'add_book_comment');});
    }


function loadContents(u){
    document.location='?page='+(parseInt(u)+1);
    }
