$(function(){
    try {
        if(is_window){
            var mail_text=0;
            $("#mail_text").click(function(){
                if(mail_text==0){
                    $(this).val('');
                    mail_text=1;
                }
            });
        
            var old='';
            $('.center_faqs2 a, .center_faqs a').click(function(){
                
                var tab=$(this).attr('name');
                if(old!=tab){
                    if(old!=''){
                        $('#'+old).animate({'height':'0px'},500);    
                    }else{
                        $('.center_faqs2 div:first, .center_faqs div:first').animate({'height':'0px'},500);
                    }
                    $('#'+tab).animate({'height':$('#'+tab+' div').height()+'px'},500);
                }else{
                }
                old=tab;
            })
            
            $('.center_faqs2 div:first').animate({'height':$('.center_faqs2 div:first div').height()+'px'},500);
            $('.center_faqs div:first').animate({'height':$('.center_faqs div:first div').height()+'px'},500);
            
            if(set_d_type!=0){
                set_distributors_cookie(set_d_type);
            }
            /*show_survey_container();*/
        }
    }catch (e) {}
});


/**********************************/
/***Survey***/
/**********************************/

    function show_survey_container(){
        
        $('a').click(function(e){

            if($.cookie(name_cookie)==null){
                set_cookie(ses_cookie,0,time_cookie);
            }else{
                var co=$.cookie(name_cookie).split('&');
                if(co[0]==ses_cookie && (co[1]==0 || co[1]==2)){
                    if(co[1]==0)
                        set_cookie(ses_cookie,0,time_cookie);
                    else
                        set_cookie(ses_cookie,2,time_cookie);
                }
            }
            
        });
        if($.cookie(name_cookie)==null){
            stop_time=setInterval("increase_time()",1000);
        }else{
            var co=$.cookie(name_cookie).split('&');
            if(co[0]==ses_cookie && co[1]==2){
                time_cookie2=parseInt(co[3],10);;
                if(time_cookie2<max_time2){
                    show_survey(true);
                    stop_time2=setInterval("increase_time2()",1000);
                }else{
                    show_survey();
                }
            }else if(co[0]==ses_cookie && co[1]==0){
                time_cookie=parseInt(co[2],10);
                if(parseInt(co[2],10)<max_time)
                    stop_time=setInterval("increase_time()",1000);
                else
                    increase_time();
            }else if(co[0]!=ses_cookie){
                if(co[1]==3){
                    set_cookie(ses_cookie,0,0);
                    stop_time=setInterval("increase_time()",1000);
                }else if(co[1]==0){
                    set_cookie(ses_cookie,0,co[2]);
                    stop_time=setInterval("increase_time()",1000);
                }
            }
        }    
    }
    function set_cookie(ses_cookie,type,time){
        $.cookie(name_cookie, ses_cookie+'&'+type+'&'+time+'&'+time_cookie2, { expires: 100, path: '/', domain: COOKIE_DOMAIN  });
    };
    
    function increase_time(){
        if(time_cookie>=max_time){
            clearInterval(stop_time);
            show_survey();
        }else
            time_cookie+=1;
    }
    function increase_time2(){
        if(time_cookie2>=max_time2){
            clearInterval(stop_time2);
            show_big_survey();
        }else
            time_cookie2+=1;
    }
    function show_big_survey(){
        time_cookie2=0;
        if($('#flashMsg2').length==1){
            $("#dark").css({'height':$(document).height()+'px','opacity':'0.6','visibility':'visible','display':'block','width':$(document).width()+'px','position':'absolute','top':'0px','left':'0px'});
            $('#con_face1').fadeIn('fast');
            $('#con_face2').fadeOut('fast');
            var pos=$('#flashMsg2').offset();
            $('#flashMsg2').animate({'top':150,'height':290,'width':490,'left':pos.left-105},'fast',function(){
                $('#con_face1').removeAttr('style').css({'position':'absolute','padding':20})
            });
        }
    }
    function show_survey(small_window){
        var w='<strong>Tell us what you think for a chance to win a Caterham driving day!</strong> If you’ve got a few minutes spare, please let us know what you think about our website and be entered into a prize draw to win an awesome Caterham Driving Experience. You can complete the survey now or after your visit, it should only take a few minutes. We\'re committed to continually developing our website and making it a useful place to come – your opinion really counts.';
        if(name_cookie!='2_type_window'){
            w='<strong>Tell us what you think for a chance to win a Caterham driving day!</strong> If you’ve got a few minutes spare, please let us know what you think about our website and be entered into a prize draw to win an awesome Caterham Driving Experience. You can complete the survey now or after your visit, it should only take a few minutes. We\'re committed to continually developing our website and making it a useful place to come – your opinion really counts.';
            var href="http://kadence-cati.dubinterviewer.com/scripts/dubinterviewer.dll/Frames?Quest=1875&sitec=2";
        }else
            var href="http://kadence-cati.dubinterviewer.com/scripts/dubinterviewer.dll/Frames?Quest=1875&sitec=1";
        
        
        var a='<div id="con_flashMsg" style="left:0px;top:0px;position:absolute;width:100%;text-align:left;">';
            a+='     <div id="dark" class="dark"></div>';
            a+='    <div id="flashMsg2">';
            a+='        <div id="con_face" style="position:relative">';
            a+='            <div id="con_face1" style="position:absolute;padding:20px;">';
            a+='            <img src="'+URL_A+'img/logo-small.gif" />';    
            a+='                <div style="font-size:12px;margin:0;padding:12px 0 15px 0; line-height:16px;"><div id="flashMessage" class="message">'+w+'</div></div>';
            a+='                <div style="padding-bottom:10px"><a href="#" address="'+href+'" class="cancel" rel="1" style="font-weight:bold;">Yes – I will go to the survey right now</a></div>';
            a+='                <div style="padding-bottom:10px"><a href="#" class="cancel" rel="2" id="around" style="font-weight:bold;">Yes – When I have finished looking around</a></div>';
            a+='                <div style="padding-bottom:10px"><a href="#" class="cancel" rel="3" style="font-weight:bold;">No – Not on this visit </a></div>';
            a+='                <div style="padding-bottom:10px"><a href="#" class="cancel" rel="4" style="font-weight:bold;">No – I never want to take the survey</a></div>';
            a+='            </div>';
            a+='            <div id="con_face2" style="position:absolute;display:none;padding:10px 10px 10px 20px;">';
            a+='                <div><a href="#" address="'+href+'" class="cancel" rel="1"><b>Take our survey now for a chance to win a Caterham driving day</b></a></div>';
            a+='            </div>';
            a+='        </div>';
            a+='    </div>';
            a+='</div>';
        $('body').append(a);
        if(small_window!=true){
            $("#dark").css({'height':$(document).height()+'px','opacity':'0.6','visibility':'visible','display':'block','width':$(document).width()+'px','position':'absolute','top':'0px','left':'0px'});
        }
        var width=parseInt($(document).width()/2)-225;
        $("#flashMsg2").css({'top':'150px','left':width+'px','visibility':'visible','display':'block'});        
        
        if(small_window==true){
            $('#con_face1').css('display','none');
            $('#con_face2').css('display','block');
            var width=parseInt($(document).width()/2)-140;
            $("#flashMsg2").css({'top':62,'left':width+'px','height':40,'width':405,'visibility':'visible','display':'block'});
            
        }else{
            var width=parseInt($(document).width()/2)-245;
            $("#flashMsg2").css({'top':'150px','left':width+'px','visibility':'visible','display':'block'});
        }

        
        $("#flashMsg2 .cancel").click(function(e) {
            e.preventDefault();
            e.stopPropagation();
            var rel=$(this).attr('rel');
            
            if (rel!=2){
                $('#con_flashMsg').remove();
            }else{
                $('#con_face1').fadeOut('fast');
                $('#con_face2').fadeIn('fast');
                var pos=$('#flashMsg2').offset();
                $('#flashMsg2').animate({'top':62,'height':40,'width':405,'left':pos.left+105},'fast');
                time_cookie2=0;
                clearInterval(stop_time2);
                stop_time2=setInterval("increase_time2()",1000);
            }
            $('#dark').css({'visibility':'hidden'});
            if(rel==1)
                set_cookie(ses_cookie,1,31);
            else if (rel==2){
                rel=3;
                set_cookie(ses_cookie,2,31);
            }else if (rel==3)
                set_cookie(ses_cookie,3,31);
            else if (rel==4)
                set_cookie(ses_cookie,4,31);
            clearInterval(stop_time);
            if(name_cookie!='2_type_window'){
                ajax_survey(rel);
            }
            if(rel==1){
                window.open ($(this).attr('address'),"mywindow")
            }
        });
        var border = RUZEE.ShadedBorder.create({ corner:8, shadow:16,  border:3 });
        border.render('flashMsg2');
        $(window).resize(function(){
          resize();  
        });
        
    }
    function set_distributors_cookie(type){
        
        if($.cookie(name_cookie)==null){        
            if(type==3|| type==2)
                set_cookie(ses_cookie,0,0);
            else
                set_cookie(ses_cookie,type,31);
        }else{
            var co=$.cookie(name_cookie).split('&');
            if(co[0]!=ses_cookie){
                if(type==3 || type==2){
                    set_cookie(ses_cookie,0,0);
                }
                else{
                    set_cookie(ses_cookie,type,31);
                }
            }
        }
    }

    function ajax_survey(type){
        $.ajax({
            type:"POST",
            url: URL_A+'distributors/survey_distributor/'+type,
            data: 'start=1',
            timeout:25000,
            beforeSend: function(){},
            success: function(msg){},
            error: function(){}
        });
    }

/**********************************/
/***End Survey***/
/**********************************/

var this_faceboxit=null;

function faceboxit(divId) {
    if($.browser.msie){
            $("select").each(function (){                            
                var width=$(this).width()+2;
                var height=$(this).height()+2;
                $(this).css({'display':'none'}).after('<div style="border:solid 1px #cacccd;background:#fff;width:'+width+'px;height:'+height+'px;" class="hiddediv"></div>');
            });
        }
    
              
        $("#dark").css({'height':$(document).height()+'px','opacity':'0.6','visibility':'visible','display':'block','width':$(document).width()+'px','position':'absolute','top':'0px','left':'0px'});
        var width=parseInt($(document).width()/2)-225;
        $("#"+divId).css({'top':'150px','left':width+'px','visibility':'visible','display':'block'});
        this_faceboxit=divId;
}

function removeFacebox(divId){        
        if($.browser.msie){
            $('.hiddediv').remove();
            $('select').css({'display':'block'});
        }
        $("#"+divId+",#dark").css({'top':'0px','left':'0px','display':'none'});
        $("#dark").css({'height':'0px'})
}

var start_width=0;

function resize(){
        //$('#aaaa').html($('body').height() +'----------'+ $('#resize_window').height());
        
        var width=parseInt($('body').width()/2)-225;
        $("#"+this_faceboxit).css({'left':width+'px'});
        
        if($("#flashMsg2").length)
            $("#flashMsg2").css({'left':(parseInt(($('body').width()/2)-($("#flashMsg2").width()/2),10))+'px'});
        

        if($("#dark").width()!=$(window).width())
            $("#dark").width($(window).width());
        if($("#dark").height()!=$(window).height())
            $("#dark").height($(window).height());
        if($("#dark").height()<$('#topcontainer').height()+15)
            $("#dark").height($('#topcontainer').height()+15);
        
}


/**
 * Loads in a URL into a specified divName, and applies the function to
 * all the links inside the pagination div of that page (to preserve the ajax-request)
 * @param string href The URL of the page to load
 * @param string divName The name of the DOM-element to load the data into
 * @return boolean False To prevent the links from doing anything on their own.
 */
function loadPiece(href,divName) {
    $(divName).load(href, {}, function(){
        var divPaginationLinks = divName+" .mcpaginationstock a";
        $(divPaginationLinks).click(function() {     
            var thisHref = $(this).attr("href");
            loadPiece(thisHref,divName);
            return false;
        });
    });
} 
