/* Main functions 
================================================================= */

function toggleMain(e) {
    // toggling function - using the href attribute of the source of the event and switch class from "none" to "selected"
    var myEvent = new Event(e);
    var myTarget = myEvent.target;
    var divToShow = $(myTarget.href.split("#")[1]);
    // get the closest parent 'div' tag
    var elemParent = myTarget.getParent();
    while(elemParent.getTag() != 'ul') {
        elemParent = $(elemParent).getParent();
    }
    if(divToShow.getStyle('display')=='none') {
        // hide every divs
        var everyDivs = $$(elemParent.getElementsByTagName('a')).getProperty('href'); // #nameoftheid
        $each(everyDivs, function(singleDiv){
               divToHide = singleDiv.split('#')[1];
               $(divToHide).setStyle('display', 'none');
        });
        // remove class to the links
        $each($(elemParent).getElementsByTagName('a'), function(singleLink) {
            singleLink.removeClass('selected');
    });
        //show a single div and add 'selected' class to the clicked link
        divToShow.setStyle('display', 'block'        );
        myTarget.addClass('selected');
    }    
    myEvent.stop();
}

function toggleStuff(e) {
    var myEvent = new Event(e);
    myEvent.stop();
    var target = (!myEvent.target.href) ? myEvent.target.parentNode : myEvent.target;
    var divToShow = target.href.split('#')[1];
    var divToShow = $(divToShow);
    if(divToShow.getStyle('display')=='none') {
        $(divToShow).setStyle('display', 'block');
    } else {
        divToShow.setStyle('display', 'none');
    }
}

function frameBuster(){
  if(($$('#content div.bloghead')[0])||($$('#topleftcol div.blogCarHead')[0])) return; // css rules in order to test if this is a blog page - index, post or cat
  if ( top != self ){
    top.location.href = unescape(window.location.pathname);
  }
}

function newSearch(e) {
    var myEvent = new Event(e);    
    var searchInput= $('searchinput');
    searchInput.value="";
    searchInput.focus();
    myEvent.stop();
}

function printThis(e) {
    var myEvent = new Event(e);
    // stop the event
    window.print();
}

function toggleConnectForm(e) {
    var myEvent = new Event(e);
    // stop the event
    myEvent.stop();
    // toggle the form
    connectFormSlider.toggle();
}


function delicious(e){
    var myEvent = new Event(e);
    u=location.href;
    t=document.title;
    window.open('http://del.icio.us/post?tags=test&v=4&jump=close&url='+u+'&title='+t, 'delicious','toolbar=no,width=700,height=400,scrollbars=yes,resizable=yes');
    myEvent.stop();
}

function facebook(e){
    var myEvent = new Event(e);
    u=location.href;
    t=document.title;
    window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
    myEvent.stop();
}

function digg(e){
    var myEvent = new Event(e);
    u=location.href;
    t=document.title;
    window.open('http://digg.com/submit?phase=2&url='+u+'&title='+t+'&topic=tech_news','digg');
    myEvent.stop();
}

function scoopeo(e){
    var myEvent = new Event(e);
    u=location.href;
    t=document.title;
    window.open('http://www.scoopeo.com/scoop/new?newurl='+u+'&amp;title='+t,'Scoopeo');
    myEvent.stop();
}

/*
Init functions ==================================================== */

function initFooter() {
    //vars
    var navLinks = $$('#bottomnav a');
    var navNames = ["cnetfr","sitemap","networks"];
    // add behaviour
    $each(navLinks, function(myNavLink) {myNavLink.addEvent('click', toggleMain);});
    // hide panes
    $each(navNames, function(myNav){ $(myNav).setStyle('display','none')});
    // display default
    $('cnetfr').setStyle('display', 'block');
}

function initSendForm() {
    if(!$('sendtofriend')) return;
    var myButton = $('btsend');
    myButton.addEvent('click', toggleStuff)
}

function initShare() {
    // services in news section
    if(!$('postlist')) return;
    var myButton = $('btpost');
    myButton.addEvent('click', toggleStuff)
    // initServices
    if ($$('#postlist .facebook a')) $$('#postlist .facebook a')[0].addEvent('click', facebook);
    if ($$('#postlist .digg a')) $$('#postlist .digg a')[0].addEvent('click', digg);
    if ($$('#postlist .delicious a')) $$('#postlist .delicious a')[0].addEvent('click', delicious);
    if ($$('#postlist .scoopeo a')) $$('#postlist .scoopeo a')[0].addEvent('click', scoopeo);
}

function initPrintThis() {
    if(!$('btprint')) return;
    var buttonPrint = $('btprint');
    buttonPrint.addEvent('click', printThis);
}

function initNewSearch() {
    if(!$$('p.helper a')[0]) return;
    var myLink = $$('p.helper a')[0];
    myLink.addEvent('click', newSearch);
}

/* Launch everything
================================================================= */

window.addEvent('domready', function()
{
    if($('cartech_vertical_slide'))
    {
        var mySlide = new Fx.Slide('cartech_vertical_slide');
        
        mySlide.hide();
        
        $('loginBtnRight').addEvent('click', function(e){
            e = new Event(e);
            mySlide.toggle();
            e.stop();
        });
    }
    
    if($('advSearchForm'))
    {
        var mySlideAdv = new Fx.Slide('advSearchForm');
        
        mySlideAdv.hide();
        
        $('newAdvSearch').addEvent('click', function(e){
            e = new Event(e);
            mySlideAdv.toggle();
            e.stop();
        });
    }
});


function findModeles(marque)
{
    if(findModeles.arguments[1] == undefined)
    {
        var divTarget = 'ecoModeles';
        var target = '&target=eco';
    }
    else
    {
        var divTarget = findModeles.arguments[1];
        var target = '&target=adv';
    }
   // alert('/cartech/ecopastille/model_list.htm?marque='+marque+target);
    
    new Ajax('/cartech/ecopastille/model_list.htm?marque='+marque+target, {
        method: 'get',
        update: $(divTarget)
    }).request();
    
    if(divTarget == 'ecoModeles')
    {
	$('ecoVersions').innerHTML = '<select name="ecoVersions" class="ecolist" title="S&eacute;lectionnez une Version" disabled><option value=""></option></select>';
    }
    //$('ecoModeles').disabled = false;
}

function findVersions(mod)
{
    new Ajax('/cartech/ecopastille/version_list.htm?mod='+mod, {
        method: 'get',
        update: $('ecoVersions')
    }).request();
    
    //$('ecoVersions').disabled = false;
}


function player_tool(zone_id, button_id)
{
	if($(zone_id))
	{		
		$(zone_id).style.display = "block";
		
		var mySlide = new Fx.Slide(zone_id);
		
		mySlide.hide();
		
		$(button_id).addEvent('click', function(e)
		{
			e = new Event(e);
			mySlide.toggle();
			e.stop();
		});
	}
}

function player_tools()
{
	player_tool('send', 'sendbtn');
	player_tool('share', 'sharebtn');
	player_tool('embed', 'embedbtn');
}

window.addEvent('load', frameBuster); 
window.addEvent('load', initNewSearch);
window.addEvent('load', initShare);
window.addEvent('load', initPrintThis);
window.addEvent('load', initSendForm);
window.addEvent('load', initFooter);
window.addEvent('load', player_tools);