﻿
/*----------------------------------------------------------------------------------------------*/
//                               Some common page stuff and fixes
/*----------------------------------------------------------------------------------------------*/

(function($)
{
    $.fn.noSelection = function() {
        return this.each(function() {           
            $(this).attr('unselectable', 'on')
                   .css({ '-ms-user-select':'none', '-moz-user-select':'none', '-webkit-user-select':'none', 'user-select':'none' })
                   .each(function() {
                       this.onselectstart = function() { return false; };
                   });
        });
    };
})(jQuery);



$(function ()
{
    $.conf = { path: $('body').attr("id") };

    // Alphabet
    $('.alphabet td').click(function () { ExecSrv("Alph", "{a:'" + $(this).html() + "'}", onAlph); });
    ExecSrv("Alph", "{a:''}", onAlph);

    // Bottom Brends list
    ExecSrv("Brends", "{}", function (res) { if (res != null) { $('.footer .linklist').html(res.d); } });

    LoadSwfir($.conf.path);
    LoadAddToAny();

    $('.i_block, .i_dsc, .i_bds').noSelection();

    //PNGFix();
});


function onAlph(res)
{
    if (res != null)
    {
        $('#BrendListTtl').html(res.d.alph);
        var $l = $('#BrendList');
        $l.slideUp(function () { $l.html(res.d.brnd); });
        $l.slideDown(function () { ArrangeClm(); });
    }
}


function PNGFix()// Png IE5 IE6 fix
{
    var arVersion = navigator.appVersion.split("MSIE")
    var version = parseFloat(arVersion[1])

    if (version < 7 && version > 5 && document.body.filters)
    {
        for (var i = 0; i < document.images.length; i++)
        {
            var img = document.images[i]
            var imgName = img.src.toUpperCase()
            if (imgName.substring(imgName.length - 3, imgName.length) == "PNG")
            {
                var imgID = (img.id) ? "id='" + img.id + "' " : ""
                var imgClass = (img.className) ? "class='" + img.className + "' " : ""
                var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
                var imgStyle = "display:inline-block;" + img.style.cssText
                if (img.align == "left") imgStyle = "float:left;" + imgStyle
                if (img.align == "right") imgStyle = "float:right;" + imgStyle
                if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
                img.outerHTML = "<span " + imgID + imgClass + imgTitle + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
                    + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
                i--;
            }
        }
    }
}


function flash_version()
{
    var d, n = navigator, m, f = 'Shockwave Flash';
    if ((m = n.mimeTypes) && (m = m["application/x-shockwave-flash"]) && m.enabledPlugin && (n = n.plugins) && n[f]) { d = n[f].description }
    else if (window.ActiveXObject) { try { d = (new ActiveXObject((f + '.' + f).replace(/ /g, ''))).GetVariable('$version'); } catch (e) { } }
    var k = d ? d.replace(/\D+/, '').split(/\D+/) : [0, 0];
    return k[0];
};


function ArrangeClm()
{
    var $l = $('#clmn_l');
    var $m = $('#clmn_m');
    var $r = $('#clmn_r');

    var lH = $l[0].scrollHeight;
    var mH = $m[0].scrollHeight;
    var rH = $r[0].scrollHeight;
    
    var maxH = (lH > mH ? (lH > rH ? lH : rH) : (mH > rH ? mH : rH));

    $l.height(maxH);
    $m.height(maxH);
    $r.height(maxH);

    //NY();
}


function LoadSwfir(path)
{
    if (flash_version() <= 2) return;
    
    $.getScript(path + 'SWFir/swfir.js', function()
    {
        var swf = new swfir();

        $('#SwfirImgs').html('<img rotate="-25" link="' + path + 'Marina-De-Bourbon/Paradise-Lys.aspx" class="swfir" style="width:150px; height:150px; position:absolute; top:-10px; left:650px;" src="' + path + 'BannersTop/PARADISE LYS.gif" alt="PARADISE LYS" />\
                              <img rotate="15" link="' + path + 'Anna-Sui/Rock-Mee-Summer-Of-Love.aspx" class="swfir" style="width:160px; height:160px; position:absolute; top:-0px; left:760px;" src="' + path + 'BannersTop/ROCK MEE SUMMER of LOVE.gif" alt="ROCK MEE SUMMER of LOVE" />\
                              <img rotate="15" link="' + path + 'Dupont/Intense-Pour-Femme.aspx" class="swfir" style="width:160px; height:160px; position:absolute; top:-10px; left:1000px;" src="' + path + 'BannersTop/INTENSE pour FEMME.gif" alt="INTENSE pour FEMME" />\
                              <img rotate="-15" link="' + path + 'John-Galliano.aspx" class="swfir" style="width:160px; height:160px; position:absolute; top:-0px; left:880px;" src="' + path + 'BannersTop/JOHN GALLIANO.gif" alt="JOHN GALLIANO" />');

        swf.specify("border-radius", "12");
        swf.specify("border-width", "8");
        swf.specify("border-color", "#ffffff");
        swf.specify('shadow-blur', '15');
        swf.specify('src', path + 'SWFir/swfir.swf');
        swf.swap(".swfir");
    });
}


function LoadAddToAny()
{
    $('.footer').prepend($('\
        <div style="float:right; margin-right:20px;" class="banner">\
            <span class="a2a_kit a2a_default_style">\
            <a class="a2a_dd" href="#">Share</a>\
            <span class="a2a_divider"></span>\
            <a class="a2a_button_linkedin"></a><a class="a2a_button_facebook"></a><a class="a2a_button_twitter"></a><a class="a2a_button_email"></a>\
            </span>\
        </div>'));

    var a2a_config = a2a_config || {};
    a2a_config.linkname = "elle.dp.ua";
    a2a_config.linkurl = "http://elle.dp.ua";
    a2a_config.locale = "ru";
    $.getScript("http://static.addtoany.com/menu/page.js");
}


/*----------------------------------------------------------------------------------------------*/
//                                  FeedForm show/hide
/*----------------------------------------------------------------------------------------------*/

function FeedForm()
{
    $('#Feeds_frm').animate({ height: $('#Feeds_frm').height() == 0 ?'190px' : '0px' });
}


/*----------------------------------------------------------------------------------------------*/
//                                  Web Service Call!!!
/*----------------------------------------------------------------------------------------------*/

function ExecSrv(cmd, params, onSuccess)
{
    $.ajax({
        type: "POST",
        url: $.conf.path + "_Service.asmx/" + cmd,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: params,
        success: onSuccess,
        error: function(XMLHttpRequest, textStatus, errorThrown)
        {
            //alert("Error! Please try to reload the page.");
        }
    });
}


/*----------------------------------------------------------------------------------------------*/
//                                          Pop Up
/*----------------------------------------------------------------------------------------------*/

function PopUp(p)
{
    p.open = p.open != null ? p.open : true;
    p.fix = p.fix != null ? p.fix : false;

    if (p.open)
    {
        var $p = $('#' + p.id).appendTo('body'), $f = $('<div id="' + p.id + '_fade"></div>').appendTo('body');
        if (p.fade == false) $f = $('#faik');

        $p.css({
            'position': p.fix ? 'absolute' : 'fixed',
            'top': p.top ? p.top : ($(window).height() - $p.outerHeight()) / 2 + (p.fix ? $(document).scrollTop() : 0),
            'left': ($(window).width() - $p.outerWidth()) / 2,
            'z-index': '99999',
            'width': Number(p.w)
        }).fadeIn(); // fixed

        $f.css({
            'display': 'none',
            'background': '#000',
            'position': 'fixed',
            'left': '0',
            'top': '0',
            'width': '100%',
            'height': '100%',
            'filter': 'alpha(opacity=50)',
            'opacity': '.50',
            'z-index': '999'
        }).fadeIn();

        $f.bind('click', function() { $p.add($f).fadeOut(function() { $f.remove(); }); });
    }
    else
    {
        var $p = $('#' + p.id), $f = $('#' + p.id + '_fade');
        if ($p.size() > 0) $p.add($f).fadeOut(function() { $f.remove(); });
    }
}


/*----------------------------------------------------------------------------------------------*/
//                                          New Year Pop Up
/*----------------------------------------------------------------------------------------------*/

$(function ()
{
    $('<div id="NY"><div id="NB"></div>\
        <div class="cls" onclick="PopUp({ id: \'NY\', open: false });">x</div>\
        <div class="msg">Внимание!<br/>Мы снова принимаем заказы.<br/><br/>С Рожеством Христовым!</div>\
    </div>').appendTo('body');
});

var _ny = true;
function NY()
{
    if (_ny) PopUp({ id: 'NY', w: 500, fade: false, fix: true, top:160 });
    _ny = false;
}


/*----------------------------------------------------------------------------------------------*/
//                                          Message Pop Up
/*----------------------------------------------------------------------------------------------*/

$(function()
{
    $('<div id="Msg">\
        <div class="cls" onclick="PopUp({ id: \'Msg\', open: false });">x</div>\
        <div class="msg"></div>\
        <center><div onclick="PopUp({ id: \'Msg\', open: false });" class="grey_btn"><div>Закрыть</div></div></center>\
    </div>').appendTo('body');
});

function Msg(txt)
{
    $('#Msg .msg').html(txt);
    PopUp({ id: 'Msg', w: 330 });
}


/*----------------------------------------------------------------------------------------------*/
//                                          Order Pop Up
/*----------------------------------------------------------------------------------------------*/

$(function()
{
    $('<div id="OrderPop"><br />\
        <div class="m_block"><br />\
            <div class="f_base">\
                <div class="f_title">Форма заказа</div>\
                <img src="' + $.conf.path + 'Img/cart_big.jpg" style="position:absolute; margin:-10px 0 0 525px; cursor:pointer;" onclick="OrderPopUp(false);" alt="Вернуться в каталог" title="Вернуться в каталог" />\
                <div id="OrderLst"></div><br /><br /><br />\
                <div style="margin:10px 0 10px 0;">Для подробной консультации обращайтесь по телефонам <em>(063)382-42-14</em> или <em>(067)630-72-81</em></div>\
                \
                <table><tr><td>\
                    <div class="f_field">Ваше имя <sub>*</sub><br /><input type="text" id="mName" /></div>\
                </td><td style="padding-left:30px;">\
                    <div class="f_field">Телефон <sub>*</sub><br /><input type="text" id="mPhon" /></div>\
                </td></tr></table>\
                \
                <div class="f_field">E-mail<br /><input type="text" id="mMail" /></div>\
                <div class="f_field">Адрес доставки<br /><input type="text" id="mAdr" class="f_lng" /></div>\
                <div class="f_field">Коментарии<br /><textarea id="mCmnt" class="f_sml"></textarea></div>\
                \
                <input type="button" style="width:100px;" value="Отправить" id="OrderButton" onclick="send();" /> &nbsp;&nbsp;\
                <span id="mMsg" class="msg"></span>\
                <div onclick="OrderPopUp(false);" class="grey_btn"><div>Вернуться в каталог</div></div>\
                <div style="clear:both;"></div>\
            </div>\
        </div>\
    </div>').appendTo('body');

    ExecSrv("Order", "{act:'', itm:''}", onOrder);
});


function OrderPopUp(open)
{
    PopUp({ id: 'OrderPop', w: 660, open: open, fix: true });
}


function add(itm) { ExecSrv("Order", "{act:'add', itm:'" + itm + "'}", onOrder); }
function del(itm) { ExecSrv("Order", "{act:'del', itm:'" + itm + "'}", onOrder); }
function dec(itm) { ExecSrv("Order", "{act:'dec', itm:'" + itm + "'}", onOrder); }
function inc(itm) { ExecSrv("Order", "{act:'inc', itm:'" + itm + "'}", onOrder); }

function onOrder(res)
{
    if (res != null)
    {
        $('#OrderHtm').html(res.d.htm);
        $('#OrderLst').html(res.d.pop);
        ArrangeClm();
        if (res.d.act == 'add') OrderPopUp();
    }
}


function send()
{
    if ($('#mName').val() == "")
    {
        $('#mMsg').html("Пожалуйста, укажите ваше имя.");
        return;
    }
    if ($('#mPhon').val() == "")
    {
        $('#mMsg').html("Пожалуйста, укажите ваш телефон.");
        return;
    }

    ExecSrv("SendOrder", "{name:'" + $('#mName').val() +
                    "', phon:'" + $('#mPhon').val() +
                    "', mail:'" + $('#mMail').val() +
                    "', adr:'" + $('#mAdr').val() +
                    "', cmnt:'" + $('#mCmnt').val() + "'}",
        onSendOrder);

    OrderPopUp(false);
    Msg('Заказ успешно отправлен. <br /> Наш менеджер свяжется с вами в ближайшее время.');

    $('#OrderHtm').html('');
    $('#OrderLst').html('');

    $('#mName').val('');
    $('#mPhon').val('');
    $('#mMail').val('');
    $('#mAdr').val('');
    $('#mCmnt').val('');
}

function onSendOrder(res)
{
    if (res.d.err) Msg(res.d.msg);
}

/*----------------------------------------------------------------------------------------------*/
//                                  Set tips init
/*----------------------------------------------------------------------------------------------*/


function Tip(p)
{
    var slf = this;
    slf.$t = null;

    if (p.tail == null) p.tail = 'lft top'; // 'mdl bot/top/lft/rgt' | 'lft/rgt top/bot' | 'none'


    slf.Show = function(c)
    {
        if (slf.$t == null)
        {
            slf.$t = $('<div class="_tip">\
                <div class="' + p.tail + '">\
                    <div class="corner top lft"></div>\
                    <div class="corner top rgt"></div>\
                    <div class="corner bot lft"></div>\
                    <div class="corner bot rgt"></div>\
                    <div class="fill top rest" style="width: 10px;"></div>\
                    <div class="fill top" style="margin-left: 74px;"></div>\
                    <div class="fill lft"><div class="fill rgt"><div class="cont">' + p.txt + '</div></div></div>\
                    <div class="fill bot"></div>' + (p.tail != 'none' ? '<div class="tail sht"></div>' : '') +
                '</div></div>').appendTo($('body'));

            slf.$t.mousemove(function(e) { clearTimeout(slf.to); });
            slf.$t.mouseout(slf.Hide);
        }

        slf.$t.css({ 'left': c.x - 35, 'top': c.y + 20 });
        slf.$t.show();
    }

    slf.Hide = function()
    {
        slf.to = setTimeout(function() { slf.$t.hide(); }, 100);
    }

    p.obj.mousemove(function(e)
    {
        clearTimeout(slf.to);
        slf.Show({ 'x': e.pageX, 'y': e.pageY }); 
    });

    p.obj.mouseout(slf.Hide);

    return slf;
}

$(function()
{
    $('.set').each(function() { Tip({ 'obj': $(this).closest('tr'), 'txt': $(this).attr('alt') }); });
});

