// 公用JS
var jq=jQuery.noConflict();

function LoadScript (url,charset){
    var s=document.createElement("script");
    s.type = "text/javascript";
    if (charset) s.charset=charset;
    s.src = url; document.getElementsByTagName("head")[0].appendChild(s);
}; 

/*广告*/
jQuery.fn.ad = function(_set) {
    _set = jQuery.extend({
        id : 'id1',
        type: 'banner',
        conf: {
            delay: 2,
            show: 10,
            animate: 300,
            top: 10
        },
        ele: []
    },_set);
    var s = this;

    if (_set.ele.length)
        _set_ad();

    function _set_ad() {
        if (_set.type == 'couple') {
            for (var i in _set.ele) {
                if (_set.ele[i].style) {
                    _set.ele[i].style += ' position: absolute;';
                } else {
                    _set.ele[i].style = 'position: absolute;';
                }
                _set.ele[i].close = 1;
            }
        }

        for (var i in _set.ele) {
            s.append(_inner(_set.ele[i], i));
        }

        if (_set.type == 'couple') {
            jQuery('.closebtn').click(function(){
                jQuery(this).parent().hide();
            });
            jQuery(window).scroll(function() { 
                jQuery('.couple_ad').stop();
                show();
            });
            function show(){
                var winTop = jQuery(window).scrollTop();
                jQuery('.couple_ad').animate({
                   top: winTop + _set.conf.top
                 }, 300);
            };
        }

        s.removeClass('hide');
    }

    function _inner(el, i) {
        var _iner = '<div id="a_inner_' + _set.id + '_' + i + '" style="width: ' + el.width + 'px; height: ' + el.height + 'px; ' + el.style + '" class="' + _set.type + '_ad">';
        if (el.type == 'flash') {
            _iner += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="' + el.width + '" height="' + el.height + '" align="center"><param name="movie" value="' + el.src + '" /><param name="wmode" value="transparent" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><embed src="' + el.src + '" quality="high" wmode="transparent" bgcolor="#ffffff" width="' + el.width + '"  height="' + el.height + '" align="center" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object><a href="' + el.href + '" target="_blank" class="ada" style="width: ' + el.width + 'px; height: ' + el.height + 'px; margin-top: -' + el.height + 'px;"></a>';
        }
        if (el.type == 'img') {
            _iner += '<a target="_blank" href="' + el.href + '"><img width="' + el.width + '" height="' + el.height + '" src="' + el.src + '"></a>';
        }
        if (el.type == 'code') {
            _iner += el.code;
        }
        if (el.close) {
            _iner += '<div class="closebtn" style="text-align:right;padding:2px 0px;font-size:12px;cursor:pointer;">关闭</label>';
        }
        _iner += '</div>';

        return _iner;
    }
};

jq.fn.hoverDelay = function(hoverEvent, outEvent){
    hoverEvent = (hoverEvent == undefined) ? function(){} : hoverEvent;
    outEvent = (outEvent == undefined) ? function(){} : outEvent;

    var hoverTimer, outTimer;
    return jq(this).each(function(){
        jq(this).hover(function(){
            var t = this;
            clearTimeout(outTimer);
            hoverTimer = setTimeout(function (){hoverEvent.call(this, t);}, 200);
        },function(){
            var t = this;
            clearTimeout(hoverTimer);
            outTimer = setTimeout(function (){outEvent.call(this, t);}, 200);
        });
    });
}

function get_ad(page) {
    if (page)
        jq.getScript('http://get.' + __DOMAIN__ + '/ad/page_' + page);
}

function refresh(forimg, domain) {
    if (forimg) {
        rand = Math.random();
        obj = jq('#' + forimg);
        src = obj.attr('src').split('?');
        obj.attr('src', src[0] + '?' + rand);
    }
}

function tips(content, time, lock) {
    return artDialog({
        id: 'Tips',
        padding: '0',
        title: false,
        cancel: false,
        fixed: true,
        lock: (lock != undefined) ? lock : false,
        opacity : (lock != undefined) ? 0.1 : false
    })
    .content('<div style="padding: 0 1em; background: #F3FCFF; padding: 40px 60px;">' + content + '</div>')
    .time(time || 2);
};

function dialog_ele(id, title, width) {
    return artDialog({
        id: 'eledialog',
        title: title ? title : false,
        cancel: true,
        content: jq('#' + id).get(0),
        fixed: true,
        lock: true,
        opacity : 0.1,
        width: width ? width : 'auto'
    })
}

jq(function(){ 

    jq('.form_clear').each(function(){
        this.reset();
    });

    /*选项卡切换*/
    jq('.tab_select a').hoverDelay(function(t) {
        jq(t).parent().find('a').removeClass('current');
        jq(t).addClass('current');
        jq(t).parent().parent().find('> .tab_warp').addClass('hide');
        jq('.' + jq(t).attr('id') + '_warp').removeClass('hide');
    });

    /*双层选项卡-top*/
    jq('.tab_select2 a').hoverDelay(function(t) {
        var _top = jq(t).parent().parent().parent();
        jq(t).parent().find('a').removeClass('current');
        jq(t).addClass('current');
        _top.find('.tab_sel_sub').removeClass('on');
        var _ntop = jq('.' + jq(t).attr('id') + '_warp');
        _ntop.addClass('on');
        _ntop.find('a').removeClass('current');
        _ntop.find('a').eq(0).addClass('current');
        var _nsub = jq('.' + jq(t).attr('id') + '_warp').find('a').eq(0).attr('id');
        _top.nextAll('.tab_warp2').addClass('hide');
        jq('.' + _nsub + '_warp').removeClass('hide');
    });

    /*双层选项卡-sub*/
    jq('.tab_sel_sub a').hoverDelay(function(t) {
        jq(t).parent().find('a').removeClass('current');
        jq(t).addClass('current');
        jq(t).parent().parent().nextAll('.tab_warp2').addClass('hide');
        jq('.' + jq(t).attr('id') + '_warp').removeClass('hide');
    });

    /*自定义下拉框动作*/
    jq(document).click(function(ev){
      if( jq(ev.target).parents("kbd").hasClass("myselect") && jq(ev.target).parents("kbd").length>0 ){
          if (jq(ev.target).parents("kbd").children("span").html() == ''){return;}
          else if(ev.target.tagName=="INS" && jq(ev.target).parents("kbd").hasClass("on") ){ jq(".myselect").removeClass("on"); }
          else if(ev.target.tagName=="INS"){ jq(".myselect").removeClass("on"); jq(ev.target).parents("kbd").addClass("on"); }
          else if(jq(ev.target).parents("span").length>0 && ev.target.tagName=="A"){ jq(ev.target).parents("kbd").children("ins").text(jq(ev.target).text()).attr('alt', jq(ev.target).attr('alt'));jq(".myselect").removeClass("on"); }
          else if(jq(ev.target).parents("s").length>0 && !jq(ev.target).hasClass("space") && ev.target.tagName=="A"){
              jq(ev.target).parents("kbd").children("span").scrollTop(0);
              jq(ev.target).parents("kbd").children("span").scrollTop( jq(ev.target).parents("kbd").find("code:contains('"+jq(ev.target).text()+"')").position().top-36 );
              jq(ev.target).parents("s").children('a').removeClass('current');
              jq(ev.target).addClass('current');
          }
      }else{
        jq(".myselect").removeClass("on");  
      }
    });

    /*团购报名*/
    jq('.group_form .subbtn').click(function() {
        form = jq(this).parent().parent();
        pref = form.attr('pref');
        ck = false;

        brand = form.find('.sel_brand ins').attr('alt');
        if (brand == '' || brand == undefined) {
            tips('请选择品牌。');
            return;
        }

        series = form.find('.sel_series ins').attr('alt');
        if (series == '' || series == undefined) {
            tips('请选择车系。');
            return;
        }

        form.find('.verify').each(function(){
            if (jq(this).val() == '') {
                tips('您的信息填写不完整，请补充后提交。');
                ck = false;
                return false;
            } else {
                if (jq(this).attr('check') != '') {
                    var str = jq(this).val();
                    switch (jq(this).attr('check')) {
                        case 'mobile' :
                            var preg = /^((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)$/;
                            if (!preg.exec(str)) {
                                tips('请填写正确的电话号码，方便我们及时与您取得联系。');
                                ck = false;
                                return false;
                            }
                            break;
                    }
                }
            }
            ck = true;
        });

        if (ck) {
            tips('提交中，请等待...', 5, true);
            jq.ajax({
                url:'http://' + pref + '.' + __DOMAIN__ + '/ajax/group',
                type:'POST',
                data: form.serialize() + '&b=' + brand + '&s=' + series,
                dataType:'json',
                success:function(rps){
                    tips(rps.msg);
                    if (rps.status) {
                        form.get(0).reset;
                        setTimeout("window.location.reload()", 1000);
                    }
                }
            });
        }
    });

    /*车型选择器*/
    var __fbrand='';
    var __brand=[];
    var __series=[]
    jq('.sel_brand').click(function(ev) {
        if (ev.target.tagName == 'INS') {
            if (jq(ev.target).next().length <= 0) {
                if (__fbrand != '') {
                    jq(ev.target).after(__fbrand);
                } else {
                    jq.getScript('http://car.' + __DOMAIN__ + '/ajax/getcar', function () {
                        if (data == null)
                            return;
                            __fbrand = data;
                            jq(ev.target).after(data);
                        }
                    );
                }
            }
        } else if (ev.target.tagName=="A") {
            var e = jq(ev.target).attr('alt');
            var sel_series = jq(this).parent().find('.sel_series');
            if (e == '' || sel_series.length <= 0)
                return;
            _clear_html(jq(this), 'b');
            if (__brand[e]) {
                sel_series.find('span').html(_make_html(__brand[e]));
            } else {
                jq.getScript('http://car.' + __DOMAIN__ + '/ajax/getcar_' + e, function () {
                        if (data == null)
                            return;
                        __brand[e] = data;
                        sel_series.find('span').html(_make_html(data));
                    }
                );
            }
        }
    });

    jq('.do_sel_brand ins').each(function() {
        var e = jq(this).attr('alt');
        var sel_series = jq(this).parent().parent().find('.sel_series');
        if (e == '' || sel_series.length <= 0)
            return;
        _clear_html(jq(this), 'b');
        if (__brand[e]) {
            sel_series.find('span').html(_make_html(__brand[e]));
        } else {
            jq.getScript('http://car.' + __DOMAIN__ + '/ajax/getcar_' + e, function () {
                    if (data == null)
                        return;
                    __brand[e] = data;
                    sel_series.find('span').html(_make_html(data));
                }
            );
        }
    })

    jq('.sel_series').click(function(ev){
        if (ev.target.tagName!="A")
            return;

        var e = jq(ev.target).attr('alt');
        var sel_model = jq(this).parent().find('.sel_model');
        if (e == '' || sel_model.length <= 0)
            return;
        _clear_html(jq(this), 's');
        if (__series[e]) {
            sel_model.find('span').html(_make_html(__series[e]));
        } else {
            jq.getScript('http://car.' + __DOMAIN__ + '/ajax/getcar_' + e, function () {
                    if (data == null)
                        return;
                    __series[e] = data;
                    sel_model.find('span').html(_make_html(data));
                }
            );
        }
    });

    jq('.do_sel_series ins').each(function() {
        var e = jq(this).attr('alt');
        var sel_model = jq(this).parent().parent().find('.sel_model');
        if (e == '' || sel_model.length <= 0)
            return;
        _clear_html(jq(this), 's');
        if (__series[e]) {
            sel_model.find('span').html(_make_html(__series[e]));
        } else {
            jq.getScript('http://car.' + __DOMAIN__ + '/ajax/getcar_' + e, function () {
                    if (data == null)
                        return;
                    __series[e] = data;
                    sel_model.find('span').html(_make_html(data));
                }
            );
        }
    })

    function _make_html(data) {
        var _html = '';
        for (var i in data) {
            if (data[i].sub == undefined) {
                _html += '<a href="javascript:;" alt="' + data[i].e + '">' + data[i].name + '</a>';
            } else {
                _cur = data[i];
                _html += '<code>' + _cur.name + '</code>';
                for (var j in _cur.sub) {
                    _html += '<a href="javascript:;" alt="' + _cur.sub[j].e + '">' + _cur.sub[j].name + '</a>';
                }
            }
        }
        return _html;
    }

    function _clear_html(jqobj, type) {
        if (type == 'b') {
            var sel = jqobj.parent().find('.sel_series');
            sel.find('span').html('');
            sel.find('ins').html('选择车系').attr('alt', '');
        }
        if (type == 'b' || type == 's') {
            var sel = jqobj.parent().find('.sel_model');
            sel.find('span').html('');
            sel.find('ins').html('选择车型').attr('alt', '');
        }
    }
});

