var marker_menu = function(){};
if (typeof(window['user_info']) == "undefined") user_info = {};
var filters = false;
var cities = false;
var showNoSite;
var showVideo;

jQuery(document).ready(function(){
    function trackWebShopClick(type, handl) {
        jQuery.post('/js_api', {'act': 'trackWebShopClick', 'type': type}, function(data) {handl(data);});
    }

    jQuery('.webshop_trigger').mousedown(function(){
        trackWebShopClick('webshop', function(){});
    });

    jQuery('.product_webshop_trigger').mousedown(function(){
        trackWebShopClick('product_webshop', function(){});
    });
});

function loadCities(handl) {
    if (!cities) {
        cities = true;
        js_api.load_cities(function(data) {
          if (data.status == 'success') {
              cities = data.cities;
              handl();
          } else alert(data.error);
        });
    } else handl();
}

function loadFilters(handl) {
    if (!filters) {
        filters = true;
        js_api.load_filters(function(data) {
          if (data.status == 'success') {
              filters = data.filters;
              handl();
          } else alert(data.error);
        });
    } else handl();
}

var scroll_to, scroll_to_bottom;

var update_inputs = function() {
    jQuery('.input input').focus(function(){
        jQuery(this).parent().parent().parent().parent().addClass('active');
        jQuery(this).parent().parent().parent().parent().parent().find('td.text div div').hide();
        jQuery(this).parent().parent().parent().parent().parent().find('td.text div#text_' + this.id).show();
    }).blur(function(){
        jQuery(this).parent().parent().parent().parent().parent().find('tr').removeClass('active');
        jQuery(this).parent().parent().parent().parent().parent().find('td.text div div').hide();
        jQuery(this).parent().parent().parent().parent().parent().find('td.text div#text_default').show();
    });
}

function load_tpl(name, handl) {
   js_api.load_tpl(name, function(data) {
      if (data.status == 'success') {
          $('body').append(data.html);
          update_inputs();
          handl();
      } else alert(data.error);
   });
}

var abstr_window = function(el) {
    var win = el;

    this.init = function() {
       jQuery('body').append(win);
    }

    this.show = function(eff, hdl) {
        if(jQuery.browser.msie && jQuery.browser.version == "6.0") eff = false;
        eff? win.fadeIn(200, hdl) : win.show(200, hdl);
    }

    this.to_center = function() {
        if (jQuery.browser.msie && jQuery.browser.version == "6.0") return;//win.css({'opacity': 1, 'position': 'absolute', 'left': (jQuery(window).width() / 2) - (win.find('.content').width() / 2), 'top': (jQuery(window).height() / 2) - (win.find('.content').height() / 2)});
        else win.css({'opacity': 1, 'position': 'fixed', 'left': (jQuery(window).width() / 2) - (win.find('.content').width() / 2), 'top': (jQuery(window).height() / 2) - (win.find('.content').height() / 2)});
    }

    this.hide = function(eff, hdl) {
        if (jQuery.browser.msie && jQuery.browser.version == "6.0") eff = false;
        eff? win.fadeOut(200, hdl) : win.hide(200, hdl);
    }

    this.get_win = function() {
        return win;
    }

    this.init();
    return this;
}

var spl_screen;
var getSplScreen = function() {
   if (!spl_screen) {
        var el = jQuery('<div id="spl_scr"></div>').css({'position': 'fixed',
                'background': '#fff', 'z-index': 20, 'opacity': 0.6, 'width': '100%',
                'height': '100%'}).hide().click(function(){jQuery('.close').trigger('click')});

        spl_screen = new abstr_window(el);
   }
   return spl_screen;
}

var new_region_win = false;
var showNewRegionWin = function(city_name, city_id) {
    var show_win = function() {
        getSplScreen().show(true, function(){
            new_region_win.show(true);
            new_region_win.to_center();
        });
    }

    var check_region = function() {
        var arr = jQuery.cookie("new_region_check")? jQuery.cookie("new_region_check").split(',') : new Array();
        arr[arr.length] = city_id;
        jQuery.cookie("new_region_check", arr.join(','));
    }

    if (!new_region_win) {
        load_tpl('new_region', function() {
            new_region_win = new abstr_window($('#new_region_win'));
            new_region_win.get_win().find('#new_region_name').html(city_name);
            new_region_win.get_win().find('.wbutton').click(function(){
                check_region();
                new_region_win.hide(true, function() {getSplScreen().hide(true)});
            });

            new_region_win.get_win().find('.close').click(function(){
                new_region_win.hide(true, function() {getSplScreen().hide(true)});
            });

            new_region_win.get_win().find('.sbutton').click(function(){
                jQuery.cookie("region_id", city_id);
                check_region();
                new_region_win.hide(true, function() {getSplScreen().hide(true)});
                window.location.reload();
            });

            show_win();
        });
    } else show_win();
}

var imag_core;
var oldBrowser;
var customTipWinCount = 0;

// подсказака
function customTip(options) {

    var win;
    var close = function() {
        win.hide(true, getSplScreen().hide(true));
        options.close_handler();
        return false;
    }

    var defaults = {
        'content': false,
        'tip_class': false,
        'text': '',
        'confirm_btn_name': '',
        'cancel_btn_name': '',
        'confirm_btn_action': function(){},
        'cancel_btn_action': close,
        'close_handler': function(){},
        'open_handler': function(){}
    }

    var options = jQuery.extend({}, defaults, options);

    this.init = function() {
        load_tpl('custom_win', function() {
            var el = $('#show_block').clone();
            $('body').append(el);
            win = new abstr_window(el);
            if (options.tip_class) win.get_win().addClass(options.tip_class);
            update();
        });
    }
    
    this.setContent = function(ctn) {
         win.get_win().find('.show_block_border').children().not('.close_btn_sh').remove();
         win.get_win().find('.show_block_border').append(ctn);
         return this;
    }

    var setContent = this.setContent;

    this.open = function() {
        if (typeof win != "undefined") {
            getSplScreen().show(true, function(){
                win.show(true);
                win.to_center();
            });
            options.open_handler();
        } else {
            setTimeout(this.open, 1200);
        }
    }

    this.setOption = function(name, value) {
        options[name] = value;
        update();
        return this;
    }

    var update = function() {
        if (options.content) this.setContent(options.content);
        else {
            win.get_win().find('#show_bl_text').html(options.text);
            win.get_win().find('#show_bl_conf').html(options.confirm_btn_name).unbind('click').click(options.confirm_btn_action);
            win.get_win().find('#show_bl_cancl').html(options.cancel_btn_name).unbind('click').click(options.cancel_btn_action);
            if (!options.confirm_btn_name.length) win.get_win().find('#show_bl_conf').hide();
            else win.get_win().find('#show_bl_conf').show();
        }

        getSplScreen().get_win().click(close);
        win.get_win().find('.close_btn_sh').click(close);
    }

    this.close = close;
    this.init();
}

var videoTip, noSite;
jQuery(document).ready(function() {
    noSite = new customTip({
        text: 'The site you want to see is under construction.<br/>You\'ll be viewing an older version.',
        confirm_btn_name: 'Proceed',
        cancel_btn_name: 'Stay here',
        open_handler: function() {jQuery('.popup-holder').removeClass('active');}
    });
    videoTip = new customTip({});
});

function showNoSite(el) {
    noSite.setOption('confirm_btn_action', function(){
        window.location.href = $(el).attr('href');
        return false;
    }).open();
}

function showVideo(id) {
    $.post('/video', {'id': id}, function(data){
        videoTip.setContent('<span style="font-size: 22px;float: left;">' + data.title + '</span><br style="clear: both"/><br/>' + data.flash).open();
    })
}

(function($) {$(function() {
    scroll_to = function(el, offset) {
    offset = offset? offset : 0;
    var time_out = 600;
    var end_pos = $(el).offset().top - ($(window).height() / 2) + offset;
    $('html,body').animate({'scrollTop': end_pos}, time_out, "easeOutQuad");
}

scroll_to_bottom = function() {
    var time_out = 600;
    $('html,body').animate({'scrollTop': $(document).height() - screen.height + 100}, time_out);
}


function preload(images) {
    if (typeof document.body == "undefined") return;
    try {
        var div = document.createElement("div");
        var s = div.style;
        s.position = "absolute";
        s.top = s.left = 0;
        s.visibility = "hidden";
        document.body.appendChild(div);
        div.innerHTML = "<img src=\"" + images.join("\" /><img src=\"") + "\" />";
    } catch(e) {
        // Error. Do nothing.
    }
}
//preload([]);


jQuery(document).ready(function() {
    //jQuery('*').click(function() { jQuery('.popup-holder').hasClass('active')? jQuery('.popup-holder').removeClass('active') : ''});
    //jQuery('.area').click(function(e) {e.preventDefault()});

    update_inputs();

    if ($('#catalog_sub_menu').length) {
        var el = $('#catalog_sub_menu');
        var sub_menu_offset = el.offset();
        var sub_menu_show = false;
        var sub_menu_show_proccess = false;
        var sub_menu_hide = false;
        var sub_menu_hide_proccess = false;
        $('#catalog_sub_menu').css('opacity', 0.6);
        $('.head_fon .cent').mousemove(function(e){
           if (e.pageX > sub_menu_offset.left && e.pageX < (sub_menu_offset.left + el.width()) && e.pageY > sub_menu_offset.top && e.pageY < (sub_menu_offset.top + el.height())) {
               if (!sub_menu_show && !sub_menu_show_proccess) {
                   sub_menu_show_proccess = true;
                   $(el).animate({'opacity': 1}, 300, function(){
                        sub_menu_show_proccess = false;
                        sub_menu_hide = false;
                   });
               }
           }
           else {
               if (!sub_menu_hide && !sub_menu_hide_proccess) {
                   sub_menu_hide_proccess = true;
                   $(el).animate({'opacity': 0.6}, 300, function(){
                        sub_menu_hide_proccess = false;
                        sub_menu_show = false;
                   });
               }
           }
        });
    }
});


jQuery(document).ready(function(){

   // интернет машазин
   var imag_core = function() {

        var win;

        this.init = function() {
            load_tpl('no_ishop', function() {
                win = new abstr_window($('#alert_win'));
                win.get_win().find('.close, .wbutton').click(function(){win.hide(true, getSplScreen().hide(true));});
            });
        }

        this.show = function() {
            getSplScreen().show(true, function(){
                win.show(true);
                win.to_center();
            });
        }

        this.init();
    }

    imagCore = new imag_core();
})

function showWindow(id) {
        var win;
        if (typeof(window['sliderStop']) != "undefined") sliderStop();
        win = new abstr_window($('#' + id));
        win.get_win().find('.close').click(function(){
            if (typeof(window['sliderStart']) != "undefined") sliderStart();
            win.hide(true, getSplScreen().hide(true));
        });

        getSplScreen().show(true, function(){
            win.show(true);
            win.to_center();
        });
}

jQuery(document).ready(function(){
    var video_win ;
    var eff = ($.browser.msie && $.browser.version != "9.0")? false : true;
    function set_close(win) {
        win.get_win().find('.close').click(function(){
            win.get_win().find('#contnt').html('');
            if (typeof(window['sliderStart']) != "undefined") sliderStart();
            win.hide(eff, getSplScreen().hide(eff));
        });
    }



/*
    oldBrowser = function(arr) {
        if ($.cookie("chk_brouser")) return;
        load_tpl('old_brouser', function() {
            var oldb_win = new abstr_window($('#oldb_win'));
            set_close(oldb_win);

            $.cookie("chk_brouser", 1);
            oldb_win.get_win().find('#browser_info').html(arr[0] + ', ver. ' + arr[1] + '.' + arr[2]);
            oldb_win.get_win().find('#year').html('ранее 2005');
            getSplScreen().show(eff, function(){
                oldb_win.show(eff);
                oldb_win.to_center();
            });
        });
    };

    var sites = {'MSIE': '', 'Opera': '', 'Firefox': '', 'Chrome': '', 'Safari': ''};

    var binfo = browserDetectNav(2);
    if (binfo[0] == 'MSIE' && binfo[1] <= 7) oldBrowser(binfo);
    else if (binfo[0] == 'Opera' && binfo[1] <= 8) oldBrowser(binfo);
    else if (binfo[0] == 'Firefox' && binfo[1] < 3) oldBrowser(binfo);
    //oldBrowser(browserDetectNav(4));
    //alert(arr[0]);*/
});

  });
})(jQuery);


function link_redirect(link) {
	var ct = new customTip ({'text': 'You are going to visit store\'s local website',  'confirm_btn_name': 'Proceed', 'cancel_btn_name': 'Stay here', 'confirm_btn_action': function(){window.location = link;}});
	ct.open();
}

function Scroller(options) {
    var el;

    var defaults = {
        'text': 'to the top',
        'top': 20,
        'class': 'scroller'
    }
    
    var options = jQuery.extend({}, defaults, options);

    this.init = function() {
        el = $('<div class="' + options['class'] + '-wrapper"><div class="' + options['class'] + '">' + options.text + '</div><div>');
        el.click(function() {
            var curPos = $(window).scrollTop();
            var scrollTime = curPos / 1.73;
            $("body,html").animate({"scrollTop": 0}, scrollTime);
        })
        $('body').prepend(el);
        $(window).scroll(function() {
            var scroll = parseInt($(window).scrollTop());
            if (!scroll) el.hide();
            else el.show()
        });
    }
    
    this.init();
}
  
jQuery(document).ready(function(){ 
    var scrl = new Scroller({});
});
