var switch_interval=0;
$(document).ready(function () {
    var w_height = $(window).height();
    var w_width = $(window).width();
    var alerts_left = (w_width - $("#price_alerts").width())/2;
    var alerts_top = (w_height - $("#price_alerts").height())/2;
    if (alerts_top<25) alerts_top = 25;
    $("#price_alerts").css("left",alerts_left+"px");
    $("#price_alerts").css("top",alerts_top+"px");

    //Features box auto switch
    if ($("#features_box").length>0){
        switch_interval = setInterval("auto_switch_fetures()", 5000);
    }
});
function getOffers(masterid, sellers, image, title, price, link, description, percent) {

    var w_height = $(window).height();
    var w_width = $(window).width();
    var offers_left = (w_width - $(".popup-box").width())/2;
    var offers_top = (w_height - $(".popup-box").height())/2;
    if (offers_top<25) offers_top = 25;
    $(".popup-box").css("left",offers_left+"px");
    $(".popup-box").css("top",offers_top+"px");

    if ($("#offers_"+masterid+" div.white-bg").length==0) {
        $.ajax({
          type: "GET",
          dataType: 'json',
          url: "?r=category/offer",
          data: ({masterid : masterid, sellers: sellers}),
          success: function(data){
            /*$("#offers_"+masterid).append("<a class='close-img' href='javascript:void(0)' onclick='offers_close("+masterid+")'><span>Close</span></a><div class='clear'></div><div class='popup-content'></div>");


            //$("#offers_"+masterid+" .offer_loader_icon").hide();
            $("#offers_"+masterid+" .popup-content").append("<span class='best'>best price</span><span class='model'>"+title+"</span>");
            var content = "";
            if (percent!=0) {
                var saving = "<div class='saving'>"+percent+"%</div>";
            }
            else {
                var saving = '';
            }

            content += "<div class='white-bg'>";
            content += "<div class='product-img'><img src='"+image+"' alt=''/></div>";
            content += "<div class='product-price'><span>"+data[0]["price"]+"</span>"+saving+"</div>";
            content += "<div class='description'><a href='"+data[0]["url"]+"' class='laskys' target='_blank'><img src='"+data[0]["retailer_logo"]+"'/></a>";
            content += "<a href='"+data[0]["url"]+"' class='atlaskys' target='_blank'>"+data[0]["retailer"]+"</a>";
            content += "<div class='feature'>"+description+"</div></div>";

            content += "<div class='best-price-block'><div class='product-price-2'>"+data[0]["price"]+"</div>";
            content += "<a href='"+data[0]["url"]+"' class='shop-now' target='_blank'>View Store</a></div>";
            content += "<div class='clear'></div>";
            content += "</div><div class='clear'></div>";
            $("#offers_"+masterid+" .popup-content").append(content);
            if (data.length>1) {
                content = "<span class='more-price'>More prices for:</span>";
                content += "<span class='black-small'>"+" "+title+"</span>";
                content += "<div class='clear'></div>";
                content += "<div class='white-bg-2'><div class='scroll'><div><table border='0' width='700' align='right'>";
                for (k=1; k<data.length; k++){
                    content += "<tr><td width='110' align='center' valign='middle' class='table-price'>"+data[k]["price"]+"</td>";
                    content += "<td width='118' align='center' valign='middle'>"+(data[k]["retailer_logo"] ? "<img src='"+data[k]["retailer_logo"]+"' alt=''/>" : data[k]["retailer"])+"</td>";
                    //content += "<td width='220' align='justify' valign='middle' class='product-description'>"+data[k]["retailer"]+"</td>";
                    content += "<td width='100' align='center'  valign='middle'><a href='"+data[k]["url"]+"' class='shop-now-small v2' target='_blank'>View Store</a></td></tr>";
                }
                content += "</table></div></div></div>";
                $("#offers_"+masterid+" .popup-content").append(content);
            }*/
          }
        });
    }
    //$(".popup-box").hide();
    //$("#overlay").show();
    //$("#offers_"+masterid).show();
    var popup_width = w_width*75/100;
    var popup_height = w_height*75/100;
    var popup_left = (w_width - popup_width)/2;
    var popup_top = (w_height - popup_height)/2;
    if (popup_top<25) popup_top = 25;
    newWindow = window.open(
        base_url+"/category/offer?masterid="+masterid+"&sellers="+sellers,
        "subWind"+masterid,
        "statusbar,menubar,scrollbars,location,height="+popup_height+",width="+popup_width+",left="+popup_left+",top="+popup_top
    );
    newWindow.focus( );
    return false;
}
function offers_close(masterid){
    $("#offers_"+masterid).hide();
    $("#overlay").hide();
    return false;
}
function filter_submit(){
    $("#filters_form").submit();
}
function switch_fetures(item){
    $(".product-icon-top").removeClass("active");
    $("#nav"+item).addClass("active");
    $("#features_box .box-product-large").hide();
    $("#fb"+item).show();
    clearInterval(switch_interval);
    switch_interval = setInterval("auto_switch_fetures()", 5000);
}
function auto_switch_fetures(){
    var current = Number($("#features_box .top-product .active").attr("id").substr(3,1));
    if (current==$("#features_box .product-icon-top").length){
        var next = 1;
    }
    else {
        var next = current+1;
    }
    switch_fetures(next);
}
function price_alert(){
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    var email = $("#price_alert_email").val();
    if(reg.test(email) == false || email=='') {
        alert('Invalid Email Address');
        return false;
    }
    var product = $("#price_alert_product").val();
    if (product==''){
        alert('Enter product name');
        return false;
    }
    $("#price_alert_email").val('');
    $("#price_alert_product").val('');
    $.ajax({
      type: "POST",
      url: "index.html?r=site/wishlist",
      data: ({email : email, product: product}),
      success: function(data){
        alert('Your request has been succesfully sent!');
        //t = setTimeout("$('#overlay').hide(); $('#price_alerts').hide();",3000);
        $('#overlay').hide();
        $('#price_alerts').hide();
      }
    });
}

function showBuingGuide(){
	$('#buingGuidesBlock').slideToggle();
}
