﻿function SwapAreaIndex(imgID) {
    Cookie.dispose('area');
    Cookie.write('area', imgID);

    var szURL = document.URL;
    var componentList = szURL.split('/');
    var szDocument = componentList[componentList.length - 2];
     
    var szNewURL = "http://www.mitchatnim.co.il/" + componentList[3] + "/" + imgID;
    window.location.href = szNewURL;

}

function SearchCity() {
    checkSearchStrCity();
}

function EnterSearchCity(e) {
    var characterCode;
    if (e && e.which) // NN4 specific code
    {
        e = e;
        characterCode = e.which;
    } else {
        e = event
        characterCode = e.keyCode; // IE specific code
    } if (characterCode == 13) //// Enter key is 13
    {
        e.returnValue = false;
        e.cancelBubble = true;
        SearchCity()
    } else return false;
}

function checkSearchStrCity() {
    if ($('cityKeyword').value == '') {
        alert("אנא הכנס ערך לחיפוש");
        //document.getElementById('keyword').value = "";
        return false;
    }
    if (CleanTheCharacters() && CleanTheCharactersIndexOf()) {
        document.location = '/index.aspx?catID=' + categoryHD + '&city=' + escape(document.getElementById('cityKeyword').value);
    }
}

function CleanTheCharactersIndexOfCity() {
    var flag = "0";
    var theWord; var tempVal; var i; theWord = document.getElementById('cityKeyword').value.toLowerCase(); tempVal = document.getElementById('cityKeyword').value.length;
    if ((theWord.indexOf('delete') != -1) || (theWord.indexOf('update') != -1) || (theWord.indexOf('select') != -1) || (theWord.indexOf('insert') != -1) || (theWord.indexOf('alert') != -1) || (theWord.indexOf('drop') != -1) || (theWord.indexOf('table') != -1)) {
        alert("החיפוש שהוקלד מכיל תגים לא חוקיים");
        document.getElementById('cityKeyword').value = "";
        return false;
    }
    return true;
}

function getQuerystring(key, default_) {
    if (default_ == null) default_ = "";
    key = key.toLowerCase();
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href.toLowerCase());
    if (qs == null)
        return default_;
    else
        return qs[1];
}

//function getQuerystring(key, default_) {
//    if (default_ == null) default_ = "";
//    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
//    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
//    var qs = regex.exec(window.location.href);
//    if (qs == null)
//        return default_;
//    else
//        return qs[1];
//}

function OpenToolTip(text, control) {
    if ($('div_BubbleText') != null) {
        var position = $(control).getCoordinates();
        $('div_BubbleText').innerHTML = text;

        $('div_Bubble').setStyles({
            bottom: position.bottom,
            left: position.left,
            top: position.top - 36,
            display: 'block'
        });

        var position2 = $('tbl_Bubble').getCoordinates();
        var w = (position2.width / 2) - 20;
        $('div_Bubble').setStyles({
            bottom: position.bottom,
            left: (position.left - w),
            top: position.top - 36,
            display: 'block'
        });
    }
}

function OpenToolTip2(text, control) {
    if ($('div_BubbleText') != null) {
        var position = $(control).getCoordinates();
        $('div_BubbleText').innerHTML = text;

        $('div_Bubble').setStyles({
            bottom: position.bottom,
            left: position.left,
            top: position.top - 36,
            display: 'block'
        });

        var position2 = $('tbl_Bubble').getCoordinates();

        var ver = getInternetExplorerVersion();
        if (ver == 7.0) {
            var w = (position2.width / 2) - 110;
        }
        else {
            var w = (position2.width / 2) - 20;
        }
        
        $('div_Bubble').setStyles({
            bottom: position.bottom,
            left: (position.left - w),
            top: position.top - 36,
            display: 'block'
        });
    }
}

function getInternetExplorerVersion()
// Returns the version of Windows Internet Explorer or a -1
// (indicating the use of another browser).
{
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
}


function CloseToolTip() {
    if ($('div_BubbleText') != null) {
        $('div_Bubble').setStyle('display', 'none');
    }
}

function PhoneClick(divID, SupplierID, supplierName) {

    if ($(divID).getStyle('display') != 'block') {
        pageTracker._trackEvent('PromotedClick', supplierName.replace("\"", ""), 'הצג טלפון');
        $(divID).setStyle('display', 'block');
        var url = "/ajax/PhoneClick.aspx?SupplierID=" + SupplierID;
        var req = new Request.HTML({
            method: 'get',
            url: url
        }).send();
    }
}

function floatw(SuppId) {
    newWin1 = window.open("http://www.mitchatnim.co.il/Category/SupplierDetails.asp?id=" + SuppId + "&Cat=9", "popup1", "width=543,height=500, status=no,scrollbars=yes,top=20")
}

//accordion functions
function OpenRecc(openDiv) {

//    var fxElementsArray = [currAccordion, openDiv];
//    var toWidth = $(currAccordion).getCoordinates().width;
//    var fromWidth = 22;
//    
//    var fxElementsObject = new Fx.Elements(fxElementsArray, {
//        link: 'chain',
//        duration: 1000,
//        transition: 'sine:in:out',
//        onStart: function() {
//            ($(openDiv).getElements('div'))[1].getElement('a').setStyle('display', 'none');
//            $(currAccordion).getElement('div.ac_openBox').setStyle('display', 'none');
//        },
//        onComplete: function() {
//            ($(currAccordion).getElements('div'))[1].getElement('a').setStyle('display', 'block');
//             $(openDiv).getElement('div.ac_openBox').setStyle('display', 'block');
//            currAccordion = openDiv;
//        }
//    });

//    fxElementsObject.start({
//        '0': {
//        'width': [toWidth, fromWidth]            
//        },
//        '1': {
//        'width': [fromWidth, toWidth]            
//        }
//    });



    var oldFx = new Fx.Tween(currAccordion);
    var toWidth = $(currAccordion).getCoordinates().width;
    var fromWidth = 22;
    var newFx = new Fx.Tween(openDiv, {
    onStart: function() {            
            ($(openDiv).getElements('div'))[1].getElement('a').setStyle('display', 'none');
            $(currAccordion).getElement('div.ac_openBox').setStyle('display', 'none');
        },
        onComplete: function() {
            ($(currAccordion).getElements('div'))[1].getElement('a').setStyle('display', 'block');
            $(openDiv).getElement('div.ac_openBox').setStyle('display', 'block');
            currAccordion = openDiv;
        }
    });

    oldFx.start('width', toWidth, fromWidth);
    newFx.start('width', fromWidth, toWidth);
    
    
}


if (navigator.appName == "Microsoft Internet Explorer") {
    window.attachEvent("onscroll", FixLeftColumn);
}
else {
    document.onscroll = FixLeftColumn;

}

function FixLeftColumn() {
    var fastNavHeight = -120;
    if ($('div_FastNav') != null) {
        if ($('div_FastNav').getStyle('display') == 'block') {
            fastNavHeight = 95; 
        }
    }

    var leftColumn = $('LeftColumnSlide');
    if (leftColumn == null) {
        return;
    }
    
    var rigthColumn = $('RightColumn');
    if (rigthColumn == null) {
        return;
    }
    
    var positionRight = rigthColumn.getCoordinates();
    var positionLeft = leftColumn.getCoordinates();
    var currentTop = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
    var fixDiv = $('RightColumnCenter');
    var endheight = 1135;
    
    if (fixDiv == null) {
        fixDiv = rigthColumn;
    }
    else {
        endheight = 1080;
    }

    if (positionLeft.height + fastNavHeight > positionRight.height) {
        fixDiv.setStyle('height', positionLeft.height + fastNavHeight );
    }


    if (positionRight.bottom < currentTop + 983 + fastNavHeight) {
        leftColumn.setStyles({
            top: (positionRight.bottom - endheight - fastNavHeight),
            width: 286,
            position: 'absolute'
        });

    }
    else if (leftColumn != null && currentTop > 285) {
        leftColumn.setStyles({
            top: -2,
            width: 286,
            position: 'Fixed'
        });

    }
    else {
        leftColumn.setStyles({
            top: 0,
            width: 286,
            position: 'relative'
        });
    }
}

function ShowAccordion(divID, categoryID) {
    var url = "/ajax/Accordion.aspx?catID=" + categoryID;
    var fade = $('div_AccordionWrap');
    fade.setOpacity(0);
    var req = new Request.HTML({
        method: 'get',
        url: url,
        update: fade,
        onStart: function() {
            fade.empty().innerHTML = '<div style="padding-top:20px"><center><img src="/images/ajax_loaderHD.gif" ></center></div>';
            $('div_AccordionOpen').setStyle('margin-top', '12px');
        },
          onRequest: function() {
           
            $(divID).setStyle('display', 'none');
            $('div_NetoWrap').setStyle('display', 'none');
        },
        onComplete: function() {
            var myEffect = new Fx.Tween(fade, {
                property: 'opacity',
                duration: 800
            });
            myEffect.start(0, 1);
        }
    }).send();
}




function ShowDealsNavigation() {
    $('div_DealsClosed').setStyle('display', 'none');
    //var divHeight = 155;
    $('div_DealsOpen').setStyles({ display: 'block', height: 0 });
    //$('div_DealsInner').setStyle('display','none');
    var myEffect = new Fx.Tween($('div_DealsOpen'), {
        property: 'height',
        duration: 400//,
        // onComplete: function() { $('div_DealsInner').setStyle('display', 'block'); }
    });
    myEffect.start(0, divHeight);

}

function HideDealsNavigation() {
    $('div_DealsClosed').setStyle('display', 'none');
    var divHeight = 100;
    //$('div_DealsOpen').setStyles({ display: 'block', height: 0 });
    var myEffect = new Fx.Tween($('div_DealsOpen'), {
        property: 'height',
        duration: 400,
        //onStart: function() {$('div_DealsInner').setStyle('display','none');},
        onComplete: function() {
            $('div_DealsClosed').setStyle('display', 'block');
            $('div_DealsOpen').setStyles({ display: 'none' })
        }
    });
    myEffect.start(divHeight, 0);

}

function sendCategoryVisitEvent(categoryArea) {
    pageTracker._trackEvent('category visit', categoryArea);
}






//*****************************************

function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent)
        while (1) {
        curleft += obj.offsetLeft;
        if (!obj.offsetParent)
            break;
        obj = obj.offsetParent;
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent)
        while (1) {
        curtop += obj.offsetTop;
        if (!obj.offsetParent)
            break;
        obj = obj.offsetParent;
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}

FixLeftColumn();


function ClickView(SupId, Img) {

    if ($(Img).getProperty('src') == '/images/icon_view_off.gif') {

        var myCookie = Cookie.read('viewSupplier');

        var NewValue;
        if (myCookie == null) {
            NewValue = SupId + ',';
        }
        else {
            NewValue = myCookie + SupId + ',';
        }

        Cookie.write('viewSupplier', NewValue, { duration: 30, path: "/" });

        $(Img).setProperty('src', '/images/icon_view.gif');
    }

}

function AddRemoveFavorites(fAnchor, item, key, status) {

    var req = new Request.HTML({
        method: 'get',
        url: '/ajax/MyWedding/AddRemoveFavorites.ashx?item=' + item + '&key=' + key + '&status=' + status,
        onRequest: function() { $(fAnchor).empty().innerHTML = '<img src="/images/ajax_loaderHD.gif" width="17" height="15" align="bottom" />'; },
        onComplete: function(response) {
            var ret = response.length > 0 ? response[0].data : "";
            if (ret == '1') {
                $(fAnchor).onclick = new Function("AddRemoveFavorites('" + fAnchor + "','" + item + "','" + key + "','False')");
                $(fAnchor).onmouseover = new Function("OpenToolTip('הסר מהמועדפים שלי','" + fAnchor + "')");
                $(fAnchor).empty().innerHTML = '<img src="/images/icon_favRemov.gif" width="17" height="15" align="bottom" />';
            }
            else if (ret == '0') {
                $(fAnchor).onclick = new Function("AddRemoveFavorites('" + fAnchor + "','" + item + "','" + key + "','True')");
                $(fAnchor).onmouseover = new Function("OpenToolTip('הוסף למועדפים שלי','" + fAnchor + "')");
                $(fAnchor).empty().innerHTML = '<img src="/images/icon_favorit.gif" width="17" height="15" align="bottom" />';
            }
        }
    }).send();

}



/****************************************************************/
/*      add Events                                              */
/****************************************************************/

window.addEvent('domready', function () {

    $$('#RightColumn a[event=phone]').addEvent('click', function () {
        var supId = this.get('supid');
        PhoneClickDom(supId);
    });

    $$('#RightColumn a[event=video]').addEvents({
        click: function () {
            var supId = this.get('supid');
            var supplierName = GetSupplierName(supId).replace("\"", "");
            pageTracker._trackEvent('PromotedClick', supplierName, 'לחיצה על וידאו מאינדקס');
        },
        mouseover: function () {
            var supId = this.get('supid');
            var supplierName = GetSupplierName(supId);
            var a_Video = $("a_Video_" + supId);
            OpenToolTip('וידאו ' + supplierName, a_Video);
        },
        mouseout: function () {
            CloseToolTip();
        }
    });

    $$('#RightColumn a[event=catalog]').addEvents({
        click: function () {
            var supId = this.get('supid');
            var supplierName = GetSupplierName(supId).replace("\"", "");
            pageTracker._trackEvent('PromotedClick', supplierName, 'לחיצה על קטלוג דיגיטאלי מאינדקס');
            var catalogLink = this.get('Link');
            OpenCatalog(catalogLink);
        },
        mouseover: function () {
            var supId = this.get('supid');
            var a_Catalog = $("a_Catalog_" + supId);
            var page = this.get('page');
            OpenToolTip('עמוד ' + page + ' בקטלוג', a_Catalog);
        },
        mouseout: function () {
            CloseToolTip();
        }
    });

    $$('#RightColumn a[event=favorite]').addEvents({
        click: function () {
            var supId = this.get('supid');
            var a_Favorite = $("a_Favorite_" + supId);
            var isfavorite = this.get('isfavorite');
            if (isfavorite == "0") {
                AddRemoveFavoritesNew(a_Favorite, supId, 'True');
                this.setProperty('isfavorite', '1');
            }
            else {
                AddRemoveFavoritesNew(a_Favorite, supId, 'False');
                this.setProperty('isfavorite', '0');
            }
        },
        mouseover: function () {
            var supId = this.get('supid');
            var a_Favorite = $("a_Favorite_" + supId);
            var isfavorite = this.get('isfavorite');
            if (isfavorite == "0") {
                OpenToolTip('הוסף למועדפים שלי', a_Favorite);
            }
            else {
                OpenToolTip('הסר מהמועדפים שלי', a_Favorite);
            }
        },
        mouseout: function () {
            CloseToolTip();
        }
    });

    $$('#RightColumn a[event=review]').addEvents({
        click: function () {
            var supId = this.get('supid');
            var supplierName = GetSupplierName(supId).replace("\"", "");
            pageTracker._trackEvent('PromotedClick', supplierName, 'Review Page');
        },
        mouseover: function () {
            var sum = this.get('sum');

            var ToolTipText = '';
            if (sum == "0") {
                ToolTipText = 'היה הראשון לדרג';
            }
            else {
                ToolTipText = 'צפייה בחוות-דעת';
            }

            var supId = this.get('supid');
            var lbl_Review = $("lbl_Review_" + supId);
            OpenToolTip2(ToolTipText, lbl_Review);
        },
        mouseout: function () {
            CloseToolTip();
        }
    });

});


function AddRemoveFavoritesNew(fAnchor, key, status) {

    var req = new Request.HTML({
        method: 'get',
        url: '/ajax/MyWedding/AddRemoveFavorites.ashx?item=1&key=' + key + '&status=' + status,
        onRequest: function () { $(fAnchor).empty().innerHTML = '<img src="/images/ajax_loaderHD.gif" width="17" height="15" align="bottom" />'; },
        onComplete: function (response) {
            var ret = response.length > 0 ? response[0].data : "";
            if (ret == '1') {
                $(fAnchor).empty().innerHTML = '<img src="/images/icon_favRemov.gif" width="17" height="15" align="bottom" />';
            }
            else if (ret == '0') {
                $(fAnchor).empty().innerHTML = '<img src="/images/icon_favorit.gif" width="17" height="15" align="bottom" />';
            }
        }
    }).send();
}

function GetSupplierName(supId) {
    return $("lbl_SupName_" + supId).get('text');
}


function PhoneClickDom(supId) {

    var lbl_Phone = $("lbl_Phone_" + supId);

    if (lbl_Phone.getProperty('class') == 'phoneOff') {

        lbl_Phone.setProperty('class', 'phoneOn');

        var url = "/ajax/PhoneClick.aspx?SupplierID=" + supId;
        var req = new Request.HTML({
            method: 'get',
            url: url
        }).send();

        var supplierName = GetSupplierName(supId).replace("\"", "");
        pageTracker._trackEvent('PromotedClick', supplierName, 'הצג טלפון');
    }
    else {
        lbl_Phone.setProperty('class', 'phoneOff');
    }
}


/****************************************************************/
/*      MoreTags & MultipleChoice                               */
/****************************************************************/

function OpenIndexMoreTags(propertyId) {
    OpenIndexTags(propertyId, 0);
}

function OpenIndexMultipleChoice(propertyId) {
    OpenIndexTags(propertyId, 1);
}

function OpenIndexMoreProperties() {
    if ($('divTopperBlackContainer') != null && $('divTopperBlackOverlay') != null) {

        var req = new Request.HTML({
            method: 'get',
            url: "/ajax/Index/GetIndexProperties.aspx?catID=" + CurrentCategory + "&areaid=" + CurrentArea + "&filter=" + CurrentFilter,
            update: $('divTopperBlackContainer'),
            onComplete: function () { $('divTopperBlackOverlay').setStyle('display', 'block'); }
        }).send();

    }
}

function OpenIndexTags(propertyId, isMultipleChoice) {

    if ($('divTopperBlackContainer') != null && $('divTopperBlackOverlay') != null) {

        var req = new Request.HTML({
            method: 'get',
            url: "/ajax/Index/GetIndexTags.aspx?catID=" + CurrentCategory + "&areaid=" + CurrentArea + "&filter=" + CurrentFilter + "&property=" + propertyId + "&isMultipleChoice=" + isMultipleChoice,
            update: $('divTopperBlackContainer'),
            onComplete: function () { $('divTopperBlackOverlay').setStyle('display', 'block'); }
        }).send();

    }

}

function SearchMultipleChoice(propertyId) {

    var filter = CurrentFilter;

    var InputsList = $("divTagsItems").getElements('input:checked');
    for (var i = 0; i < InputsList.length; i++) {
        var input = InputsList[i];
        if (input.getProperty('type') == 'checkbox') {
            if (input.checked) {
                filter = filter + "_" + propertyId + "t" + input.value;
            }
        }
    }

    var req = new Request.HTML({
        method: 'get',
        url: '/ajax/Index/FixIndexURL.ashx?catID=' + CurrentCategory + '&areaid=' + CurrentArea + '&filter=' + filter,
        onComplete: function (response) {
            var ret = response.length > 0 ? response[0].data : "";
            document.location.href = ret;
        }
    }).send();

}

function CloseIndexTags() {
    $('divTopperBlackContainer').empty();
    $('divTopperBlackOverlay').setStyle('display', 'none');
}


/****************************************************************/
