﻿$(document).ready(function(){
    var itmDesc = $('span#navDesc');
    var itmDescClass = $(itmDesc).attr('class');
    var itmDescText = $(itmDesc).text();
    $('#mainNav > ul > li > a').hover(function() {
        var itm = $(this).text();
        switch(itm) {
            case 'Lustre':
                if ($(this).hasClass('nLuCurr')) {
                    //DO NOTHING - This is the current tab.
                } else {
                    $(itmDesc).removeClass().addClass('ndLu').text('Roli\'s imprint for high quality illustrated books.');
                }
                break;
            case 'Lotus':
                if ($(this).hasClass('nLoCurr')) {
                    //DO NOTHING - This is the current tab.
                } else {
                    $(itmDesc).removeClass().addClass('ndLo').text('Roli\'s imprint for non-fiction.');
                }
                break;
            case 'India Ink':
                if ($(this).hasClass('nInCurr')) {
                    //DO NOTHING - This is the current tab.
                } else {
                    $(itmDesc).removeClass().addClass('ndIn').text('Roli\'s critically acclaimed fiction list.');
                }
                break;
            case 'Roli Junior':
                if ($(this).hasClass('nChCurr')) {
                    //DO NOTHING - This is the current tab.
                } else {
                    $(itmDesc).removeClass().addClass('ndCh').text('Roli\'s exciting list of children books.');
                }
                break;
            case 'Designwallas':
                if ($(this).hasClass('nDeCurr')) {
                    //DO NOTHING - This is the current tab.
                } else {
                    $(itmDesc).removeClass().addClass('ndDe').text('Roli\'s range of fashion stationary.');
                }
                break;
            case 'Distribution':
                if ($(this).hasClass('nDiCurr')) {
                    //DO NOTHING - This is the current tab.
                } else {
                    $(itmDesc).removeClass().addClass('ndDi').text('Roli\'s third party distribution service.');
                }
                break;
            default:
                break;
        }
    },function() {
        $(itmDesc).removeClass().addClass(itmDescClass).text(itmDescText);
    });
    
/*======= TOP SEARCH BOX STYLING =======*/
    $('#q').focus(function(){
        if ($(this).val() == 'Search...'){
            $(this).val('');
            $(this).removeClass('darkr');
        }
    });
    $('#q').blur(function(){
        if ($.trim($(this).val()) == ''){
            $(this).val('Search...');
            $(this).addClass('darkr');
        }
    });
    
    $('#searchForm').submit(function(){
        var searchTerm = $("#q").val();
        if ((searchTerm=='') || (searchTerm=='Search...')){
            alert("What are you looking for? At least, give me a hint!");
            return false;
        } else {
            searchTerm = searchTerm.replace(/\s+/g,"+");
            var actionUrl = "/search/" + searchTerm + "/";
            //actionUrl = actionUrl.replace("[term]",searchTerm);
            $("#searchForm").attr({action : actionUrl});
            $("#searchForm").submit();
            return false;
   }

    $('#userComments').click(function(){
        if($('#divUserComments').is(':hidden'))   {
           $('#divShippingComments').hide();
           $('#divRoliComments').hide();
         $('#divUserComments').slideDown('slow')
         
      }else{
        $('#divUserComments').hide();   }      
      })
      
      $('#RoliComments').click(function(){
        if($('#divRoliComments').is(':hidden'))   {
           $('#divUserComments').hide();
           $('#divShippingComments').hide();       
         $('#divRoliComments').slideDown('slow')
      }else{
        $('#divRoliComments').hide();   }      
      })
      
      $('#shippingComments').click(function(){
        if($('#divShippingComments').is(':hidden'))   {
        $('#divUserComments').hide();     
        $('#divRoliComments').hide();
         $('#divShippingComments').slideDown('slow')
      }else{
        $('#divShippingComments').hide();}      
      })
      
});
    

/*======= SMOOTH SCROLL FOR ANCHORS =======*/
    $('a[href*=#]').click(function() {

        if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
            var $target = $(this.hash);
            $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
            if ($target.length) {
                var targetOffset = $target.offset().top;
                $('html,body')
                .animate({scrollTop: targetOffset}, 500);
                return false;
            }
        }
    });

    
/*======= USER REVIEWS =======*/
    $('#postreview').submit(function(){
    $('#postreview:submit').attr('disabled', true);
    var act = $(this).attr('action');
    var bi = $.trim($('input[name=\'bid\']').val());
    var yn = $.trim($('input[name=\'yname\']').val());
    var ye = $.trim($('input[name=\'yemail\']').val());
    var yp = $.trim($('input[name=\'yrating\']').val());
    var yr = $.trim($('textarea[name=\'yreview\']').val());

    if ((bi!='') && (yn!='') && (yp!=0) && (ye!='') && (yr!='')) {
        $.ajax({ 
            type: 'POST', url: act, data: 'bi=' + bi + '&yn=' + yn + '&ye=' + ye + '&yp=' + yp + '&yr=' + yr, dataType: 'html', success: function(msg){
                if (msg > 0) {
                    $('#postreview').slideUp('normal', function(){
                        $('div.bookUserReview').html('<div class="msgOky">Thank you for sending us your review. We will inform you by email as when we publish it on the website.</div><p>&nbsp;</p>');
                    });
                } else if (msg = -99) {
                    $('#postreview').slideUp('normal', function(){
                        $('div.bookUserReview').html('<div class="msgErr">We encountered an error while saving your review. Please try again after some time.</div><p>&nbsp;</p>');
                    });
                
                }
            }
        });
    } else {
        alert('Please fill in all the fields.');
        $('#postreview:submit').removeAttr('disabled');
    }
    return false;
    });
    
    if(typeof Shadowbox!="undefined"){
        var options = {
            loadingImage:       '/images/loadingAnimation.gif',
            resizeLgImages:     false,
            flvPlayer:          '/library/flvPlayer.swf',
            handleUnsupported:  'remove',
            keysClose:          ['c', 27] // c or esc
        };
        Shadowbox.init(options);
    }
    
    /* Coded By Ashwani Rana */
    $('#viewAll').click(function(){$('#addressBook').toggle();});
}); 

function setRating(rt) {
    $('li.current-rating').width(rt*2*10+'%')
    $('input[name=\'yrating\']').val(rt);
};

function changeCurrency(sl) {
    alert('Please note that the currency has changed but you will be billed in Indian Rupees (INR) only.');
    $.ajax({
        url: '/set-currency/default.aspx',
        data: "c="+ sl.options[sl.selectedIndex].value,
        type: "POST",
        dataType: 'html',
        success: function(data){self.location.reload();}
    })
}

//function SetAction() {
//    var searchTerm = $("#q").val();
//    if ((searchTerm=='') || (searchTerm=='Search...')){
//    //if ((searchTerm=='')){
//        alert("Please provide some criteria to search!");
//        return false;
//    } else {
//        searchTerm = searchTerm.replace(/\s+/g,"+");
//        var actionUrl = "/search/[term]/";
//        actionUrl = actionUrl.replace("[term]",searchTerm);
//        $("#searchForm").attr({action : actionUrl});
//        alert(actionUrl);
//        return false;
//   }
//}

