Subversion Repositories SmartDukaan

Rev

Rev 7889 | Rev 8949 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

$(function(){
        jQuery('.mk_videoclick').click(function(){
                var clicked = jQuery(this);
            var scrollTo = jQuery("#recommendedvids");
            if(currentVideoId!=currentVideoIds[clicked.attr("slidecounter")]){
                scrollTo.find('li[ytid=' + currentVideoIds[clicked.attr("slidecounter")] + ']').click();
            }
                trackEventWithGA('Product', 'VideoTabClicked',  $('title').text().split('|')[0].trim()  + "|" + clicked.attr("slidename"));
                jQuery('html, body').animate({ scrollTop: scrollTo.offset().top }, 'medium');
        return false;
        });
        
        var bankEle = jQuery("#bank-name");
        var first = true;
        var allBankImgs = {
                1:"/images/hdfc-icon.png",
                2:"/images/axis-icon.png",
                3:"/images/icici-icon.png",
                4:"/images/indusind-icon.png",
                5:"/images/citi-icon.png",
                6:"/images/std-chartered-icon.png",
                7:"/images/hsbc-icon.png",
                8:"/images/kotak-icon.png",
                9:"/images/sbi-icon.png",
        };


        var minEmi = 999999;
        jQuery.each(emiObj, function(index, opt) {
                if(sellingPrice >= opt.minAmount){ 
                        if(typeof banks[opt.bankId] == "undefined") {
                                banks[opt.bankId] = {};
                                banks[opt.bankId].name =  opt.bankName;
                                banks[opt.bankId].emis =  {};
                                banks[opt.bankId].active =  true;
                                banks[opt.bankId].img = allBankImgs[opt.bankId];
                        }
                        var bank = banks[opt.bankId];
                        var bankTenure = {};
                        bankTenure.tenureDescription = opt.tenureDescription;
                        if (opt.chargeType == "FIXED") {        
                                bankTenure.emiProcessingCharge = opt.chargeValue;
                        } else {
                                bankTenure.emiProcessingCharge = Math.ceil((opt.chargeValue * sellingPrice)/100);  
                        }
                        bankTenure.totalAmount = sellingPrice + bankTenure.emiProcessingCharge;  
                        bankTenure.monthlyEmi =  Number((Math.round((bankTenure.totalAmount*100)/opt.tenure)/100).toFixed(2));
                        if(minEmi > bankTenure.monthlyEmi){
                                minEmi = bankTenure.monthlyEmi;
                        }
                        bank.emis[opt.id] = bankTenure;
                }
        });
        jQuery.each(banks, function(index, bank){
                var style = ' style="background: url(\'' + bank.img + '\') no-repeat right"';
                if(first){
                        bankEle.append('<li title="' + bank.name + '"' + style +'><input type="radio" id="' + index +'" value="' + index +'" name="bankselected"' + (bank.active ? '""' : 'disabled="disabled"') + ' checked="checked"/></li>');
                        first = false;
                }else {
                        bankEle.append('<li title="' + bank.name +'"' + style +'><input type="radio" id="' + index +'" value="' + index +'" name="bankselected"' + (bank.active ? '""' : 'disabled="disabled"') + ' /></li>');
                }
        });
        
        bankEle.find("input[type='radio']").click(onBankClicked);
                
        jQuery("#emi_more").click(function(){
                
                $.colorbox({
                        inline:true, 
                        href:"#emiLightBox",
                width:"600px",
                height:"410px",
                
                onComplete: function(){
                        $('#emiLightBox').show();
                        bankEle.find("input[type='radio']:first").trigger('click');
                },
                onCleanup: function(){
                        $('#emiLightBox').hide();
                }
        });
        trackEventWithGA('Product', 'Emi Know More Clicked',  $('title').text().split('|')[0].trim());
        });

        $('#loopedSlider').loopedSlider();
        
        runEffect();
        
        $(".controls").tabs();
        
        $('#forgot_username_link').tipsy({gravity: 'w'});

        $("#zipcode").val($("#defaultpincode").val());
        
        if(typeof Entity != 'undefined'){
                var select = $("#colorSelector");
                if(Entity.orderedItems){
                        $.each(Entity.orderedItems, function(index, sort) {
                                select.find('option[value="' + sort + '"]').appendTo(select);
                        });
                        $("#item_id").val(Entity.orderedItems[0]);
                        onColorSelectorChange(Entity.orderedItems[0]);
                }
                select.find("option:eq(0)").remove();
        }

    var prodid = $("#product_id").val();
    
        add_to_storage_set("histitems", prodid);

    load_accessories_widget();
    
    load_most_compared_widget();

    
    // Fix to always show first image's title on page load
    if ("undefined" != typeof(title_1)){
        $('.slides .modelName').html(title_1[0]);
    }
    
    
    function getSeletectedItemId(){
         return $("#item_id").val();
    }
    
    
    /**
     * Code to track user clicks on Product slides and 'Proceed to Payment' option
     */
        $('a.vt').click(function(){
                trackEventWithGA('Product', $(this).children('span').text(), $('title').text().split('|')[0].trim());
        });
        
        $('#computeShippingEstimate').click(function(){
                updateEstimate(getSeletectedItemId());
        });
        
    $("#addToCart").click(function(){
        jQuery.ajax({
                        type: "POST",
                        url: "/addtocart",
                        data: "productid=" + getSeletectedItemId(),
                        success: function(msg)  {
                                 if(msg == "") {
                                         trackEventWithGA('Order', 'Add to Cart', getSeletectedItemId() + '');
                                         window.location = "/cart";
                                 } else {
                                         var productDetail =  $("#productDetail");
                                 var prodInfo = $(productDetail).find("span.brand").html() + $(productDetail).find("span.product-name").html();
                                         displayRelatedProducts(msg);
                                         trackEventWithGA('Order', 'OOS Lightbox', prodInfo);
                                 }
                        }
                });
        });
        
        $("#colorSelector").change(function(){
                var itemid = $("#colorSelector option:selected").val();
                onColorSelectorChange(itemid);
                return false;
        });
        
        $(".util_compare").click(function() {
                $.colorbox({
                        inline:true, 
                        href:"#compareLightBox",
                width:"350px",
                height:"230px",
                
                onComplete: function(){
                        $('#compareLightBox').show();
                },
                onCleanup: function(){
                        $('#compareLightBox').hide();
                }
        });
        });
        
        $('.util_addnewresearch').click(function(){
                addResearch('', 'single');
        });
        
    $('#mobilename').keypress(function(e) {
        if(e.keyCode == 13) {
                return compareProducts($("#compare_continue").attr("producttype"));
        }
    });
    
    
    $("#compare_continue").click(function() {
        return compareProducts($(this).attr("producttype"));
    });

    $('.tooltip').click(function() {
        trackEventWithGA('Product', 'Helpdoc Click', $(this).attr('name'));
    });
    
    $('.tooltip').each(function(index) {
           $(this).qtip({
                style: { width: 300, overflow: 'auto',
                        tip: { corner: 'topLeft' }, 
                        border: { width: 2, radius: 2, color: '#DDDDDD' } },
                show: { when: { event: 'click' } },
                content: { url: "/helpdocs/" + $(this).attr('name'), title: { text: '&nbsp;', button: 'Close'} },
                hide: { when: { event: 'unfocus' } },
                position: { adjust: {screen: true} }
                });
        });
        
        
    function compareProducts(productType){
                var hyphenatedProductType = productType.replace(/ +/g, '-').toLowerCase();
        
        var productName = $("#mobilename").val();
                var hypenatedNameTwo = productName.replace(/ +/g, '-').replace(/\/+/g,'-').replace(/-+/g,'-').toLowerCase();
                
                var productTwo = productIdNames[productType][productName];
                if(typeof productTwo == 'undefined'){
                        alert("Please select a valid product");
                        return false;
                }
                var productOne = $("#catalog_id").val();
                var prodDetail = $("#productDetail");
                var productNameOne = $(prodDetail).find("span.brand").html() + " " + $(prodDetail).find("span.product-name").html();  
                var hyphenateNameOne = productNameOne.replace(/ +/g, '-').replace(/\/+/g,'-').replace(/-+/g,'-').toLowerCase();  
                window.location = "/compare-" + hyphenatedProductType + "/" 
                        + hyphenateNameOne + "-vs-" + hypenatedNameTwo + "?p1="+productOne+"&p2="+productTwo+"&fromsrc=product_page";   
        }
    
    $("#accessories table td div a").live('click', function() {
        var productId = $(this).parent().parent().parent().children().find('input[type=checkbox]').val();
        trackEventWithGA('Widget', 'Accessory Click', productId);
    });
    
        
        $('.nextSlide-anchorLink').live('click', function() {
            var nextSlideLink = $(this).attr('nextSlideId');
            $('a[href="' + nextSlideLink +'"]').click();
            return false;
        });
    
    $('a.compare-now').live('click', function(){
                var productType = $(this).attr('producttype');
                productType = productType.replace(/ +/g, '-').toLowerCase();
                
                var productOne = $("#catalog_id").val();
                var prodDetail = $("#productDetail");
                var productNameOne = $(prodDetail).find("span.brand").html() + " " + $(prodDetail).find("span.product-name").html();
                var hyphenateNameOne = productNameOne.replace(/ +/g, '-').replace(/\/+/g,'-').replace(/-+/g,'-').toLowerCase();
                
                var productTwo = $(this).attr("value");
                var hypenatedNameTwo = $(this).attr("title").replace(/ +/g, '-').replace(/\/+/g,'-').replace(/-+/g,'-').toLowerCase();
                
                window.location = "/compare-" + productType + "/" + hyphenateNameOne + "-vs-" + hypenatedNameTwo + "?p1="+productOne+"&p2="+productTwo+"&fromsrc=most_compared_widget";
    });
    
});

function changeSignInClass(){
        if(document.getElementById("signinClass").className.indexOf("signin1") > -1)    {
                document.getElementById("signinClass").className = 'signin';
        } else  {
                document.getElementById("signinClass").className = 'signin1';
        }
}


function updateEstimate(itemId) {
        itemId = itemId || $("#colorSelector option:selected").val();
        
        jQuery.ajax({
                type: "GET",
                url: "/estimate/" + $("#zipcode").val() + "_" + itemId,
                beforeSend: function()  {
                        $("#shipping_time").show();
                        $("#shipping_time_1").hide();
                        $("#shipping_time .red").html("<img src='/images/loader_l.gif'>");
                },
                success: function(msg)  {
                        var response = eval('(' + msg + ')');
                        var deliveryEstimate = response['delivery_estimate'];
                        var codDeliveryEstimate = response['cod_delivery_estimate'];
                        var isCODAvailableForLocation = (response['is_cod_available_for_location'] === 'true');
                        var isOTG = (response['on_time_guarantee'] === 'true');
                        var businessDays = response['business_days'];
                        
                        if(deliveryEstimate == -1)      {
                                $("#shipping_time").hide();
                                $("#shipping_time_1").show();
                                $('#cod_know_more').hide();
                        } else  {
                                $('#cod_know_more').hide();
                                if($("#shipping_time").html().indexOf('after arrival')>-1){
                                        $("#shipping_time .red").html(businessDays + " Days");
                                }else{
                                        if(isCODAvailableForLocation && (codDeliveryEstimate != deliveryEstimate)){
                                                $("#shipping_time .cod").html("Cash On Delivery orders is " + codDeliveryEstimate);
                                                $('#cod_know_more').show();
                                        }
                                        $("#shipping_time .red").html(deliveryEstimate);
                                }
                        }
                        
                        if (isCODAvailableForLocation)  {
                                $('#cod').show();
                        } else {
                                $('#cod').hide();
                        }
                        
                        if (isOTG)      {
                                $('#otg').show();
                        } else {
                                $('#otg').hide();
                        }
                }
        });
}

function load_most_compared_widget(){
        jQuery.ajax({
                type : "GET",
                url : "/most-compared-products/" + $("#product_id").val(),
                cache: false,
                success : function(html) {
                    $("#mostcompared").html(html);

                    // Product Title
                    $("#mostcompared table td div a").each(function() {
                                if($(this).attr('class') != "compare-now"){
                                        $(this).truncate({addtitle : true});
                                }
                    });

                    // Product Price
                    $("#mostcompared table td div div.price").each(function() {
                                        $(this).truncate({addtitle : true});
                        });

                    // Product Details
                    $("#mostcompared table td div div.text").each(function() {
                                        $(this).truncate( {addtitle : true});
                        });
            }
        });
}
function onColorSelectorChange(itemid){
        if($('#sp').length>0){
                $('#sp').html(PARAMETERS[itemid].SP);
                $('#mrp').html(PARAMETERS[itemid].MRP);
                $('#saving').html(PARAMETERS[itemid].SAVING);
                $("#item_id").val(itemid);
        }
        updateEstimate(itemid);
}

banks = {};
var first = true;
for (obj in PARAMETERS) {
   sellingPrice = Number(PARAMETERS[obj].SP);
   break;
}

function onBankClicked(){
        var bankId = jQuery(this).val();
        var bank = banks[bankId];
        var emiTableEle = jQuery("#emi_options");
        var tdHtml=[];
        var tenureHtml=[];
        tdHtml.push("<td><b>" + bank.name + "</b></td>");
        jQuery.each(bank.emis, function(key,val){
                tdHtml.push('<td><span style="font-size:11px" class="rupee-icon"><b>' + val.monthlyEmi+ '</b></span></td>');
                tenureHtml.push('<td><span style="font-size:11px"><b>' + val.tenureDescription + '</b></span></td>');
        });
        emiTableEle.find("td.mk_emioptions").attr("colspan", count(bank.emis));
        emiTableEle.find("tr.emirow").html(tdHtml.join(""));
        emiTableEle.find("tr.mk_tenure").html(tenureHtml.join(""));     
}

function count(obj) {
   var count=0;
   for(var prop in obj) {
      if (obj.hasOwnProperty(prop)) {
         ++count;
      }
   }
   return count;
}