﻿/// <reference path="jquery-1.4.min.js" />
var $isDefaultPage = false;
var $SectionProductBox;
var $selectedBookGroup;
var $selectedBookOpenBox;
var $selectedBook;
var $bookVisible;
var isOpenClose = false;
var LastID;
var CurrentID;
var tmpID;
var DeviceType ='Qisda';

        function setUpGroups() 
        {
            $SectionProductBox.find('.SectionProductBooksOpenBox').each(function(i) 
            {
                $(this).insertAfter($SectionProductBox.find('.SectionProductBookGroup:eq(' + i + ')'));
            });
        }

        function homepageSetBottomLink() 
        {
            var cat = $('.SectionProductCatTabActive').attr("cat");

            if (cat != undefined) 
            {
                var catView = $('.SectionProductCatTabActive').attr("catview");

                if (catView != undefined) 
                {
                    cat = $('.SectionProductAllCatLinkOn').attr("cat");
                    if (cat == -1) 
                    {
                        $('.SectionProductMoreLike').text('');
                    }
                    else 
                    {
                        $('.SectionProductMoreLike').text('עוד ב' + $('.SectionProductAllCatLinkOn').text());
                        $('.SectionProductMoreLike').attr("href", "groups.aspx?cid=" + cat + "&mode=1");
                    }
                }
                else 
                {
                    $('.SectionProductMoreLike').attr("href", "groups.aspx?gid=" + cat);
                    $('.SectionProductMoreLike').text('עוד ב' + $('.SectionProductCatTabActive').find('.autoEllipsis').text());
                }
            }
        }

        function closeBook(sender) {
            var newID = $(sender).attr('upid');
            if (newID) {
                tmpID = newID;
            }
            $('#SectionProductTop' + tmpID).slideUp(300);
            $('#SectionProductBookItemTitle' + tmpID).slideDown(300);
        }

        function openBook(obj) 
        {
            if ($isDefaultPage && $selectdBookOpenBox.index() == 1) 
            {
                if ($('html, body').scrollTop() < 75) 
                {
                    $('html, body').scrollTop(90);
                }
            }
            else if ($isDefaultPage && $selectdBookOpenBox.index() == 3) 
            {
                if ($('html, body').scrollTop() < 285) 
                {
                    $('html, body').scrollTop(303);
                }
            }
            $('#SectionProductBookItemTitle' + CurrentID).slideUp(300);
            $('#SectionProductTop' + CurrentID).slideDown(300);
        }

       
        this.setBookDetails = function() 
                {
                    if (($SectionProductBox).find(':animated').length) 
                    {
                        return;
                    }
                    
                    $selectedBook = $(this);
                    var currentObj = $(this);
                    tmpID = CurrentID;                  
                    CurrentID = currentObj.attr('title');
                    $selectedBookGroup = $selectedBook.closest('.SectionProductBookGroup');
                    $selectdBookOpenBox = $selectedBookGroup.next();
                    $bookVisible = $SectionProductBox.find('.BookVisible');

                    if ($bookVisible.length == 0) 
                    {
                        openBookDetailsBox();
                    }
                    else 
                    {
                        if (!$selectedBook.hasClass('BookVisible')) 
                        {
                            closeBookDetails(openBookDetailsBox);
                        }
                        else 
                        {
                            closeBookDetails();
                        }
                    }
                }


        openBookDetailsBox = function() 
        {
            $selectedBook.addClass('BookVisible');
            $bookVisible = $selectedBook;           
            openBook();

            $('#SectionProductBookDetailsBox' + CurrentID).addClass('bookOpenBoxVisible');
            $('#SectionProductBookDetailsBox' + CurrentID).addClass('DetailsBookVisible').css("display", "block");
            $('#SectionProductBookDetailsBox' + CurrentID).parent('.SectionProductBooksOpenBox').addClass('bookOpenBoxVisible');
            $('#SectionProductBookDetailsBox' + CurrentID).parent('.SectionProductBooksOpenBox').slideDown('slow', function() { SetFaceBookLike(CurrentID); cartManagerVar.CheckIsInCart(CurrentID); });
        };

        function closeBookDetails(openBookDetails) {
            if ($bookVisible && $bookVisible.length) 
            {
                $bookVisible.removeClass('BookVisible');
                closeBook(this);

                $SectionProductBox.find('.bookOpenBoxVisible').slideUp('slow', function() 
                {
                    $(this).find('.DetailsBookVisible').css("display", "none").removeClass('DetailsBookVisible');

                    $(this).removeClass('bookOpenBoxVisible');

                    if ($.isFunction(openBookDetails)) 
                    {
                        openBookDetails();
                    }
                });
            }
        }


        function SetFaceBookLike(pid) 
        {
            $selectdBookOpenBox.find('.SectionProductBookDetailsBox').eq($selectedBook.index()).find('#fbIframe').html("<iframe src=\"http://www.facebook.com/plugins/like.php?href=http://www.e-vrit.co.il/Product.aspx?ProductID=" + pid + "&amp;locale=en_US&amp;layout=button_count&amp;show_faces=true&amp;width=72&amp;action=like&amp;font=tahoma&amp;colorscheme=light&amp;height=23\" scrolling=\"no\" frameborder=\"0\" style=\"border:none; overflow:hidden; width:72px; height:23px;\" allowTransparency=\"true\" class=\"facebookIframe\"></iframe>");
        }



        function LoadCategoryView(cat, isGroup) {
            $.ajax({
                url: "Handlers/YITEbookServices.asmx/GetCategoryProductsView",
                data: "{'cat':" + cat + ",'rowsCount':'2','isGroup':" + isGroup + "}",
                success: function(msg) 
                {
                    var catobj = msg.d; 
                    var $catContent = $('.SectionProductBox');
                    $catContent.html(catobj.CategoryContent);
                    setUpGroups();
                    homepageSetBottomLink();
                    $(".autoEllipsis").autoEllipsis();
                },
                error: function(xhr, textStatus, errorThrown) 
                {
                    var err = eval("(" + xhr.responseText + ")");
                    alert(err.Message);
                }
            });
        }

        function loadPageFunction() 
        {
            if ($('body').find('.asp-defaultPage').length > 0) 
            {
                $isDefaultPage = true;
            }

            $SectionProductBox = $('.SectionProductBox');
            setUpGroups();
            homepageSetBottomLink();

            $('.SectionProductCatTab').click(function() 
            {
                if (!$(this).hasClass('SectionProductCatTabActive')) 
                {
                    closeBookDetails();
                    $('.SectionProductCatTabActive').removeClass('SectionProductCatTabActive');
                    $(this).addClass('SectionProductCatTabActive');

                    if ($(this).attr("catView") == 'byCat')
                    {
                        $('.SectionProductCatAll').show();
                        $('.SectionProductMain').addClass("SectionProductCatAllTop");
                        LoadCategoryView($(this).attr("cat"), false);
                        $('.rater').rater();
                    }
                    else 
                    {
                        $('.SectionProductMain').removeClass("SectionProductCatAllTop");
                        $('.SectionProductCatAll').hide();
                        LoadCategoryView($(this).attr("cat"), true);                       
                        $('.rater').rater();
                    }
                }
            });


            $('.SectionProductAllCatLink').click(function() 
            {
                $('.SectionProductAllCatLink').removeClass('SectionProductAllCatLinkOn');
                $(this).addClass('SectionProductAllCatLinkOn');
                LoadCategoryView($(this).attr("cat"), false);
                $('.rater').rater();
                $('.SectionProductCatTabActive').attr("cat", $(this).attr("cat"));

            });

            $('.SectionProduct').live("click", setBookDetails);
            $('.SectionProductClose').live("click", closeBookDetails);
        }

$(function() {
    loadPageFunction();

}
);






