$j(document).ready( function () {
    
    newsletterEmail.init();
    
    /* display popup with cookie */
    /*if (document.cookie.indexOf('visited=true') === -1) {
        var expires = new Date();
        expires.setDate(expires.getDate()+30);
        document.cookie = "visited=true; expires="+expires.toUTCString();

        $j.fn.colorbox({href:myPopUp, open:"true", innerWidth:667, innerHeight:442});
    }*/
	
    /* colorbox */
    $j(".video-link").colorbox();
    $j("a[rel='portfolio1']").colorbox({transition:"fade"});
    $j("a[rel='portfolio2']").colorbox({transition:"fade"});
    $j("a[rel='portfolio3']").colorbox({transition:"fade"});
    $j("a[rel='portfolio4']").colorbox({transition:"fade"});
    $j("a[rel='portfolio5']").colorbox({transition:"fade"});
    $j("a[rel='portfolio6']").colorbox({transition:"fade"});
    $j("a[rel='portfolio7']").colorbox({transition:"fade"});
    $j("a[rel='portfolio8']").colorbox({transition:"fade"});
    $j("a[rel='portfolio9']").colorbox({transition:"fade"});

    /* Disabled Javascript */
    $j("html").removeClass("no-js");
    
    /* Select Language */
    $j("#language").hide();
    $j("#form-newsletter").hide();
    $j("#language").jListbox();
    
    /* Superfish Dropdown Menu */
    $j("ul.sf-menu").superfish({
		pathClass: 'current'
	});
    
    /* Cross Browser Font Embedding */
    $j("h1, #tdm h2, #tdm li a, .sf-menu li a, #encarts h3 a, #featured ul.ui-tabs-nav li span, #footer-container li a, #confirmation-email-ok h4, #confirmation-email-ko h4, input.button").ieffembedfix();
    
    /* Slider Home Our Creations */
    jQuery.fn.newsTicker = jQuery.fn.newsticker = function(delay){
        return this.each(
            function()
            {
                if(this.nodeName.toLowerCase()!= "ul") return;
                delay = delay || 4000;
                var self = this;
                self.items = jQuery("li", self);
                // hide all items (except first one)
                self.items.not(":eq(0)").hide().end();
                // current item
                self.currentitem = 0;
                var doTick = function()
                {
                    jQuery.newsticker(self);
                }
                setInterval(doTick,delay);
            }
        )
        .addClass("newsticker")
        .hover(
            function()
            {
                // pause if hovered over
                this.pause = true;
            },
            function()
            {
                // unpause when not hovered over
                this.pause = false;
            }
        );
    }
    
    jQuery.newsticker = function(el){
        // return if hovered over
        if(el.pause) return;
        // hide current item
        jQuery(el.items[el.currentitem]).fadeOut("slow",
            function()
            {
                jQuery(this).hide();
                // move to next item and show
                el.currentitem = ++el.currentitem % (el.items.size());
                jQuery(el.items[el.currentitem]).fadeIn("slow");
            }
        );
    }
    
    $j("#newsticker").newsTicker();
    
    /* Slider Home Award */
    jQuery.fn.newsTicker2 = jQuery.fn.newsticker2 = function(delay){
        return this.each(
            function()
            {
                if(this.nodeName.toLowerCase()!= "ul") return;
                delay = delay || 11000;
                var self = this;
                self.items = jQuery("li", self);
                // hide all items (except first one)
                self.items.not(":eq(0)").hide().end();
                // current item
                self.currentitem = 0;
                var doTick = function()
                {
                    jQuery.newsticker2(self);
                }
                setInterval(doTick,delay);
            }
        )
        .addClass("newsticker2")
        .hover(
            function()
            {
                // pause if hovered over
                this.pause = true;
            },
            function()
            {
                // unpause when not hovered over
                this.pause = false;
            }
        );
    }
    
    jQuery.newsticker2 = function(el){
        // return if hovered over
        if(el.pause) return;
        // hide current item
        jQuery(el.items[el.currentitem]).fadeOut("slow",
            function()
            {
                jQuery(this).hide();
                // move to next item and show
                el.currentitem = ++el.currentitem % (el.items.size());
                jQuery(el.items[el.currentitem]).fadeIn("slow");
            }
        );
    }
    
    $j("#newsticker2").newsTicker2();
    
    /* Featured Content Slider */
    $j("#featured > ul").tabs({fx:{opacity: "toggle"}}).tabs("rotate", 10000, true);
    
    /* jCapSlide - Nice captions for image
    $j(".ic_container").capslide({
        caption_color	: 'white',
        caption_bgcolor	: 'black',
        overlay_bgcolor : '#A0A0A0',
        border		: '',
        showcaption	: false
    });
    */
    
    /* jCarousel Jeux-Concours Landing Page */
    $j('#mycarousel').jcarousel();
    
    /* Hide (Collapse) the toggle containers on load */
    $j("#form-newsletter").hide();
    
    /* Display Newsletter Form */
    $j("a#newsletter-link").click(function(){
        $j("#form-newsletter").toggle();
    });
    
    /* Easy tab vCard */
    //Default Action
	$j(".tab_content").hide(); //Hide all content
	$j("ul.tabs li:first").addClass("active").show(); //Activate first tab
	$j(".tab_content:first").show(); //Show first tab content
	
	//On Click Event
	$j("ul.tabs li").click(function() {
		$j("ul.tabs li").removeClass("active"); //Remove any "active" class
		$j(this).addClass("active"); //Add "active" class to selected tab
		$j(".tab_content").hide(); //Hide all tab content
		var activeTab = $j(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
		$j(activeTab).fadeIn(); //Fade in the active content
		return false;
	});
    
    /* jFlickrFeed Plugin */
    /*$j('#cbox').jflickrfeed({
		limit: 15,
		qstrings: {
			id: '42599913@N04',
            tags:'portfolio',
            description:'toto'
		},
		itemTemplate: '<li>'+
						'<a rel="colorbox" href="{{image}}" title="{{title}}">' +
							'<img src="{{image_s}}" alt="{{title}}" />' +
						'</a>' +
					  '</li>'
	}, function(data) {
		$j('#cbox a').colorbox();
	});*/

});

/* Select Language */
function gotoUrl(url) {
    if (url == "")
    return;
    location.href = url;
}
function newWin(url) {
    // url of this function should have the format: "target,URL".
    if (url == "")
    return;
    window.open(url.substring(url.indexOf(",") + 1, url.length),
    url.substring(0, url.indexOf(",")));
}

/* Current Position on newsletter link */
$j(".toggle a#newsletter-link").click(function() {
    if ($j(this).hasClass("current")) {
        $j(this).toggleClass("current", false);
    } else {
        $j(this).toggleClass("current", true);
    }
    return false;
});

/* Ajax Display Modalbox Message */
function ajaxLoader()
{
    $j('#main').append(
        $j("<div />").attr('id', 'loader').append(
            $j("<img />").attr('id', 'ajax-loader').attr('src', 'images/ajax-loader.gif').attr('alt', '')
        )
    );
}

var newsletterEmail = {
    init: function() {
        $j('#newsletter-form').append('<input type="hidden" name="ajax" value="1" />');
        $j('#newsletter-form').bind('submit', function() {
            $j.ajax({
                url: 'newsletter',
                type: 'post',
                dataType: 'json',
                data: $j('#newsletter-form').serialize(),
                beforeSend: function (XMLHttpRequest) {
                    ajaxLoader();
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    Modalbox.show($('confirmation-email-ko'), {title: '', width: 504});
                    $j('#loader').remove();
                },
                success: function(data, textStatus) {
                    switch (data.code)
                    {
                        case 1:
                            $j('#newsletter_email').val('Entrer email');
                            Modalbox.show($('confirmation-email-ok'), {title: '', width: 504});
                            break;
                        case 999:
                            $j('#newsletter-error').html(data.message);
                        default:
                            //if ($j('#newsletter_email').val() == '') $j('#newsletter_email').val('Entrer email');
                            Modalbox.show($('confirmation-email-ko'), {title: '', width: 504});
                            break;
                    }
                    $j('#loader').remove();
                }
            });

            return false;
        });
    }
}

