﻿function onload() {
    roundAllImagesCorners();
    handleNewsTicker();
    if(document.location.href.indexOf("print") != -1){
        window.print();
    }
    
    $('.MoreMagazineTextContainer').hover(
        function() { $(this).find('.SubCategories').show(); },
        function() { $(this).find('.SubCategories').hide(); }
    );
    
    $('#classid').change(fillSubClassifications);    
}

function voteForSurvey(sender, answerId)
{
    location.href = $("#ServeyLink").attr('href') + "?vote=" + answerId ;
}

function roundAllImagesCorners()
{
    var roundCornersHtml = "<div class='Corner CornerTopLeft'></div>";
    roundCornersHtml += "<div class='Corner CornerBottomRight'></div>";
    
    var roundCornersWhiteHtml = "<div class='Corner CornerTopLeftWhite'></div>";
    roundCornersWhiteHtml += "<div class='Corner CornerBottomRightWhite'></div>";
    
    var roundCornersSearchHtml = "<div class='Corner CornerSearchTopLeft'></div>";
    roundCornersSearchHtml += "<div class='Corner CornerSearchBottomRight'></div>";
    
    var roundCornerAbsoluteHtml = "<div class='Corner CornerAbsoluteTopLeft'></div>";
    roundCornerAbsoluteHtml += "<div class='Corner CornerAbsoluteBottomRight'></div>";
    
    var roundAllCornerHtml = "<div class='Corner CornerAbsoluteTopLeft'></div>";
    roundAllCornerHtml += "<div class='Corner CornerAbsoluteBottomRight'></div>";
    roundAllCornerHtml += "<div class='Corner CornerAbsoluteTopRight'></div>";
    roundAllCornerHtml += "<div class='Corner CornerAbsoluteBottomLeft'></div>";
    
    $(".RoundMyCorners").append(roundCornersHtml);
    $(".RoundAllMyCorners").append(roundAllCornerHtml);
    $(".RoundMyCornersWhite").append(roundCornersWhiteHtml);
    $(".RoundMyCornersSearch").append(roundCornersSearchHtml);
    $(".RoundMyCornersAbsolute").append(roundCornerAbsoluteHtml);
    $(".SmallAdContainer").append("<div class='AdCorner'></div>");
}

// add to favorites and set hompage functions :

var urlAddress = window.location.href;
var pageName = "Restb2b.co.il"; 
function addToFavorites() 
{ 
        if (window.sidebar) {
        // Mozilla Firefox Bookmark
        //alert("FIREFOX!");
        window.sidebar.addPanel(pageName, urlAddress,"");
        } else if( window.external ) {
        // IE Favorite
        //alert("YES IE");
        window.external.AddFavorite( urlAddress, pageName);
        }
        else if(window.opera && window.print) {
        // Opera Hotlist
        return true; }

} 

function setHome()
{
   //alert('setHome');
   if (window.sidebar) 
   {
        // Mozilla Firefox Bookmark
        //alert("FIREFOX!");
        FireFoxPopUp('open');
    } 
    else if( window.external ) {
        // IE Favorite
        //alert("IE");
        document.body.style.behavior="url(#default#homepage)"; 
        document.body.setHomePage(urlAddress);
    }
    else if(window.opera && window.print) {
        // Opera Hotlist
        return true; 
    }
    
}
function FireFoxPopUp(OPTN)
{
   switch (OPTN)
   {
    case "open":
        $('.FireFoxOPTNcontainer').css('display','block');
    break;
    case "close":
        $('.FireFoxOPTNcontainer').css('display','none');
    break;
   }
    
}

function SendToFriend(sender, link)
{
    var container = $(sender).parent();
    //container.css('position','relative');
    
    $(".SendToFriendWindow").hide();
    if(container.find(".SendToFriendWindow").length > 0){
        container.find(".SendToFriendWindow").slideDown(300);
    }
    else{
        $.post( baseUrl + 'SubViews/SendToFriend.aspx',
            {
                link : link
            },
            function(data)
            {
                container.append(data).find(".SendToFriendWindow").slideDown(300);
            });
    }
}

function SendMailToFriend(sender, link)
{
    var form = $(sender).parents("form");
    
    if(form.validateInputs())
    {
        var fullName    = $("#txtFullName").val();
        var senderMail     = $("#txtSenderMail").val();
        var phone       = $("#txtPhone").val();
        var email       = $("#txtEmail").val();
        var comment     = $("#txtComment").val();
        var link = (link || location.href);
        var handlerUrl = baseUrl + "Handlers/SendToFriend.ashx";
        
        
        switch(form.attr("id"))
        {
            case "SendToFriend":
                var type = "../SubViews/SendToFriend_MailBody.aspx";
                var subject = "כתבה מעניינת מ rest b2b נשלחה אליך";
            break;
            
            case "ContactUs":
                var type = "../SubViews/ContactUs_MailBody.aspx";
                var subject = fullName + " יצר קשר דרך אתר " + baseUrl;
            break;
        }
        
        $.post(handlerUrl ,
        {
            fullName    :fullName,
            senderMail     :senderMail,
            phone       :phone,
            email       :email,
            comment     :comment,
            link : link ,
            htmlBodyType : type,
            subject : subject
            
        },function(data){
          
          if(data == "OK")
          {
            $("#tblMailForm").hide("fast");
            $(".ThankUForSenddingMail").slideDown("slow");
          }
          else
            alert(data);
        })
    }
}

var newsTickerIndex = 0;
var newsTickers;
function handleNewsTicker()
{
    if (newsTickers == null)
        newsTickers = $('.NewsTicker');
        
    if (newsTickers.length > 0)
    {
        $('.NewsTicker.Active').removeClass('Active').hide();
        
        if (newsTickerIndex + 1 > newsTickers.length)
            newsTickerIndex = 0;
        else
            newsTickerIndex++;
            
        $(newsTickers[newsTickerIndex]).addClass('Active').fadeIn();
        
        setTimeout(handleNewsTicker ,5000);
    }
}

function getTickerItem(currentIndex, dir)
{
    $("#NewsTicker .Form_container").hide();
    $("#AjaxLoader").show();
    
    $.get(baseUrl + 'SubViews/NewsTicker.aspx',
        {
           currentIndex : currentIndex,
           dir : dir
        },
        function(data)
        {
           $("#PopUp_container").html(data);
           $("#NewsTicker .Form_container").slideDown(1000);
        });
}

function fillSubClassifications(sender)
{
    $.get(baseUrl + 'Handlers/GetSubClassifications.ashx',
        {
            id : $('#classid').val()
        },
        function(data)
        {
           $("#classsubid option[@value!='']").remove();
           var classifications = eval( "(" + data + ")" );
           for(var i = 0; i < classifications.length; i++)
           {
                $('#classsubid').append("<option value='" + classifications[i].Id + "'>" + classifications[i].Name + "</option>");
           }           
        });

}

function notifyTracker(url)
{
    try
    {
        if (typeof(pageTracker) != "undefined")
            pageTracker._trackPageview(url);
    }
    catch(e) {}
}

function GetPromotedBusinessDetails(businessId , type , sender)
{
    $.post(baseUrl + 'Index/Handlers/GetPhoneByPromotedBusiness.ashx',
        {
           itemId : businessId,
           itemType : type
        },
        function(data)
        {
            if ($(sender).is('span, .WebSite'))
            {
                if ( type == "Url" )
                    window.open(data);
                else
                    $(sender).html(data);
            }
        });
}

function printCurrentUrl() {
    window.open(window.location.href + "?print=true");
}

function fShowSimulator() {
    if (screen.width > 1024) {
        if (document.getElementById("divLeftBanner") != null) {
            if (document.body.clientWidth >= 1090) {
                document.getElementById("divLeftBanner").style.display = "inline";
            }
            else {
                document.getElementById("divLeftBanner").style.display = "none";
            }
        }
    }
    else {
        document.getElementById("div_Main").style.styleFloat = "right";
        document.getElementById("div_Main").setAttribute("style", "float: right;");
        if (document.getElementById("divLeftBanner") != null) {
            if (document.body.clientWidth >= 1000) {
                document.getElementById("divLeftBanner").style.display = "inline";
            }
            else {
                document.getElementById("divLeftBanner").style.display = "none";
            }
        }
        if (document.getElementById("divRightBanner") != null) {
            document.getElementById("divRightBanner").style.display = "none";
        }
    }
}