jQuery.noConflict();



function setCharCounter (a, b, n) {
    var o = document.getElementById(a);
    o.charCounterMax = n;
    o.charCounterDisplay = b;
    o.onkeyup = charCounterUpdate;
}
function charCounterUpdate () {
    var n = this.value.length;
    if (n > this.charCounterMax){
        alert("Vous ne pouvez pas dépasser " + this.charCounterMax + " caractères pour cette zone.");
        return false;
    }
    if (this.charCounterDisplay) {
        var o = document.getElementById(this.charCounterDisplay);
        if (o.tagName.toLowerCase()=="input") o.value = n;
        else o.innerHTML = n;
    }
}


function intCoucoursChoix(){
    if($('id_concours')) {
        if($('id_concours').get('value') == 1){
            $('choix-concours').setStyle('display','none');
            $('coucours-2').setStyle('display','');
        } else {
            $('coucours-2').setStyle('display','none');
        }

        $('activ-concours-premium').addEvent('click', function(e) {
            e = new Event(e).stop();
            $('id_concours').set('value', '1');
            $('choix-concours').setStyle('display','none');
            $('coucours-2').setStyle('display','');
        });
        $('undo-concours-premium').addEvent('click', function(e) {
            e = new Event(e).stop();
            $('id_concours').set('value', '0');
            $('choix-concours').setStyle('display','');
            $('coucours-2').setStyle('display','none');
        });

    }
}

function counterCommentaire(){
    if($('counter_description')){
        $('counter_description').set('text',$('commentaire_photo').get('value').length);
        $('commentaire_photo').addEvent('keyup', function(e) {
            $('counter_description').set('text',$(this).get('value').length);
        });

    }
}

function intTagManager() {
    if($('photo-edit')) {
        var tagManager = $('photo-edit');
        var tagSelect = $('tag-select');

        $('ajax_add_tag').addEvent('click', function(e) {
            e = new Event(e).stop();

            if($('tag-select').get('value') == 0){
                alert('sélectionnez un mot clé');
            } else {
                var url = $('photo-edit').get('action');
                tagManager.set('send', {
                    method: 'post',
                    onSuccess: function(result) {
                        $('inject-tags').set('html', result);
                        $each($$('#tag-select option'), function(el, index) {
                            if($('tag-select').get('value') == el.getProperty('value')) {
                                // console.log('destroy');
                                el.dispose();
                            }
                        });
                        initDeleteTags();
                        $('ajax_add_tag').set('disabled', false);
                    }
                });
                tagManager.send();
                $('ajax_add_tag').set('disabled', true);
            }
            return false;
        });

        initDeleteTags();
    }
};


function initDeleteTags() {
    // console.log('initDeleteTags');
    $$('#inject-tags .delete').removeEvent('click');
    $$('#inject-tags .delete').addEvent('click', function() {
        var deleteEl = this;
        var deleteLink = deleteEl.getProperty('href');

        var myHTMLRequest = new Request.HTML({
            url: deleteLink,
            method:"post",
            data: "ajax=yes&id_photo="+$('hidden-id-photo').get('value'),
            update : $('inject-tags'),
            onSuccess: function(tree, els, html) {
                reloadTagManager();
                initDeleteTags();
            }
        }).send();
        return false;
    });
}

function reloadTagManager() {
    var myHTMLRequest = new Request.HTML({
        url: "/account/photos_select_tags/",
        data: "ajax=yes&id_photo="+$('hidden-id-photo').get('value'),
        method:"post",
        update: $('tag-select'),
        onSuccess: function () {
        // console.log('yeah');
        }
    }).send();
}

function initAjaxLoader() {
    if($$('.submitajaxloader')) {
        var ajaxloader = $$('.ajax_loader');
        ajaxloader.setStyle('display', 'none');

        var submitajaxloader = $$('.submitajaxloader');
        submitajaxloader.addEvent('click', function(){
            ajaxloader.setStyle('display', 'block');
        });
    }
}

function initRating() {
    if($('id-photo')) {
        var idPhoto = $('id-photo').get('html');

        $$('.rate').each(function(element,i){

            /* rate event */
            element.addEvent('click', function(){
                var myCookie = Cookie.read('apc_votes');
                var locked = false;
                if($defined(myCookie)) {
                    if(myCookie.contains(idPhoto)) {
                        var locked = true;
                    }
                }


                if(!locked) {
                    var myStyles = ['star-0', 'star-1', 'star-2', 'star-3', 'star-4', 'star-5', 'star-6', 'star-7', 'star-8', 'star-9', 'star-10'];
                    myStyles.each(function(myStyle){
                        if(element.getParent().hasClass(myStyle)){
                            element.getParent().removeClass(myStyle)
                        }
                    });

                    myStyles.each(function(myStyle, index){
                        idNum = element.id.replace(/star-/, '');

                        if(index == idNum){
                            var myHTMLRequest = new Request.HTML({
                                url: "/photos-1/vote/"+idPhoto,
                                method:"post",
                                data: "rating="+idNum,
                                update : $('average-rating'),
                                onSuccess: function(tree, els, html) {
                                    var currentRate = Math.round($('average-rating').get('html'));
                                    myStyles.each(function(myStyle){
                                        if(element.getParent().hasClass(myStyle)){
                                            element.getParent().removeClass(myStyle)
                                        }
                                    });
                                    $('rating').addClass('star-'+currentRate);
                                    $('rating').addClass('locked');
                                }
                            }).send();

                        }
                    });
                } else {
                    $('rating').addClass('locked');
                }
                return false;
            });
        });

        /* set stars from db */
        var currentRate = Math.round($('average-rating').get('html'));
        $('rating').addClass('star-'+currentRate);
        var myCookie = Cookie.read('apc_votes');
        var locked = false;

        if($defined(myCookie)) {
            if(myCookie.contains(idPhoto)) {
                var locked = true;
            }
        }
        if(locked == true) {
            $('rating').addClass('locked');
        }

    }

}

function initPlayer() {

    jQuery("a#single_image").fancybox({
        'onClosed' : function() { jQuery("#infos-top").hide() },
        'transitionIn'	:	'elastic',
        'transitionOut'	:	'elastic',
        'easingIn' : 'easeOutBack',
        'easingOut' : 'easeInBack'

    });


    jQuery("#infos-top").hide();
    jQuery("#infos-bottom").hide();
    jQuery("#focus_photo .photo").hover(
      function () {
        jQuery("#infos-top").slideDown();
        jQuery("#infos-bottom").slideDown();
        
      },
      function () {
        jQuery("#infos-top").slideUp();
        jQuery("#infos-bottom").slideUp();
      }
    );


}



jQuery(document).ready(function() {
	
    // IE6 Hacks
    if ( jQuery.browser.msie && parseInt(jQuery.browser.version) == 6 )
    {
        // PNG Fix
        jQuery('img[src$=png] , *:regex(css:background-image,.png)').addClass('pngfix');
        DD_belatedPNG.fix('.pngfix');
		
        // Listing links debug
        jQuery('.listing a')
        .children()
        .css({
            cursor:'pointer'
        })
        .click(function(){
            window.location = jQuery(this).parent('a').attr('href');
        });
    }

    initPlayer();
    initRating();
    initAjaxLoader();
    intTagManager();
    intCoucoursChoix();
    counterCommentaire();

});


