// UTF-8 Dreamweaver detect (éàç)



function ddOver(d, height){

	d.style.overflow = "visible";

	d.style.height = height + "px";

	d.style.backgroundImage = "url(/images/dd_ov.gif)";

	d.style.borderColor = '#eee';

	/*d.style.margin = '-1px 0 0 -1px';*/

}



function ddOut(d){

	d.style.overflow = "hidden";

	d.style.height = "13px";

	d.style.backgroundImage = "url(/images/dd_of.gif)";

	d.style.borderColor = '#fff';

	/*d.style.border = "0";

	d.style.margin = '0';*/

}



function getClientSize() {

	var myWidth = 0, myHeight = 0;

	if( typeof( window.innerWidth ) == 'number' ) {

		//Non-IE

		myWidth = window.innerWidth;

		myHeight = window.innerHeight;

	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {

		//IE 6+ in 'standards compliant mode'

		myWidth = document.documentElement.clientWidth;

		myHeight = document.documentElement.clientHeight;

	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {

		//IE 4 compatible

		myWidth = document.body.clientWidth;

		myHeight = document.body.clientHeight;

	}

	return [myWidth, myHeight];

}



function resize_gallery() {

	if(gallery_container= jQuery("#gallery-container")) {

		clientSizes = getClientSize();

		w = (clientSizes[0] - gallery_container.offset().left - 50);

		jQuery("#gallery-container").css('display', 'block').css('width', w + "px");

		jQuery("#galleryScrollTrack").css('display', 'block').css('width', w - 25  + "px");



		var gallery_rows= 4;

		var nb_pieces= gallery_container.find('.piece').length;

		var gallery_columns = Math.ceil(nb_pieces / gallery_rows);



		jQuery("#gallery").css('width', gallery_columns * 120  + "px").css('height', gallery_columns * 213  + "px");



		if(jQuery('#galleryScrollTrack').length > 0) {

			scrollInit();

		}

	}

}



function scrollInit() {

	galleryScroll = new scrollerObj("galleryScroll");

	galleryScroll.scrollContent = "gallery-container";

	galleryScroll.scrollUpImg = "/images/left_of.gif";

	galleryScroll.scrollUpImgOv = "/images/left_of_o.gif";/**/

	galleryScroll.scrollDownImg = "/images/right_of.gif";

	galleryScroll.scrollDownImgOv = "/images/right_of_o.gif";/**/

	galleryScroll.scrollerImg = "/images/scrollerFS.gif";

	galleryScroll.scrollerImgOv = "/images/scrollerFS_o.gif";

	galleryScroll.scrollerBGImg = "/images/trackBG.gif";

	galleryScroll.trackHideOnDisabled = true;

	galleryScroll.scrollBtnWidth = 32;

	galleryScroll.scrollBtnHeight = 21;

	galleryScroll.scrollerFixedWidth = 105;

	galleryScroll.scrollerFixedHeight = 21;

	galleryScroll.scrollspeed= 16;

	galleryScroll.direction = 1;

	galleryScroll.init();



	jQuery('#galleryScrollTrack').css('display', 'block');

}



function loadImages(){

	k=1;

	for (i=0; i< pieces['tri_'+pieces.tri].length; i++){

		id_piece = pieces['tri_'+pieces.tri][i];

		// Si le filtre "Favoris" actif, ne laisse passer que les favoris

		if(pieces.filtre=='tout' || pieces.filtre=='favoris' && pieces.p[id_piece].fav){

			setTimeout("loadNextImage("+id_piece+")", k * 10);

			k++;

		}

	}

}



function galeryOver(which){

	which.style.border = "1px solid #b3b3b3";

	if(document.location.href.indexOf("details") > 0){

		setOpacity(which, 1);

	}

}



function galeryOut(which){

	if(which.id != "gal_" + curId) {

		which.style.border = "1px solid #d3d3d3";

		if(document.location.href.indexOf("details") > 0){

			setOpacity(which, 0.55);

		}

	}

}





function gotoDetails(id, sufPage) {

	document.location.href = "/details#id="+id;

}



function goto(id, sufPage) {

	document.location.href = "/details#id="+id;

	//document.location.href = "details.php?id="+id+"&sufPage="+sufPage+"#id="+id;

}



function gotoR(id, sufPage, region) {

	document.location.href = "/details&region=" + region+"#id="+id;

	//document.location.href = "details.php?id="+id+"&sufPage="+sufPage+"&region=" + region+"#id="+id;

}



function setOpacity(el, value) {

	if(document.all){

		el.style.filter='progid:DXImageTransform.Microsoft.Alpha(Opacity=' + value * 100 + ")";

	} else {

		el.style.MozOpacity = value;

	}

}

/* ----- AJAX -----*/

function ajax_req() {

	var ua;

	if(window.XMLHttpRequest) {

		try {

			ua = new XMLHttpRequest();

		} catch(e) {

			ua = false;

		}

	} else if(window.ActiveXObject) {

		try {

			ua = new ActiveXObject("Microsoft.XMLHTTP");

		} catch(e) {

			ua = false;

		}

	}

	return ua;

}

function ajax_comet(req, url, callback, p) {

	req.open('get', url);

	req.onreadystatechange = callback;

	/* Non compatible avec les ActiveX

	if (p){

		for (var key in p){

			req[key] = p[key];

		}

	}*/

	req.send(null);

}

k_url = {

	// Trouve la VALUE d'un HASH par sa KEY

	get_hash:function(key, url){

		if (url == null){

			url = document.location.href;

		}

		var hash_pos = url.indexOf('#');

		// Si y'a un HASH

		if (hash_pos != -1){

			var re_key = new RegExp("[#&]"+key+"=","i");

			var key_pos = url.search(re_key);

			// Si la KEY est presente

			if (key_pos != -1){

				var amp_pos = url.indexOf('&', key_pos+1);

				// Si y'a un AMP qui suit

				if (amp_pos != -1){

					var value = url.substring(key_pos+1+key.length+1, amp_pos);

				}else{

					var value = url.substring(key_pos+1+key.length+1);

				}

				if (value != ''){

					return (value);

				}else{

					return (-1);

				}

			}else{

				return (-1);

			}

		}else{

			return (-1);

		}

	},

	// Ajoute, ou remplace s'il existe, un HASH a une URL (#id=4&page=photo)

	add_hash:function(key, value, url){

		if (url == null){

			url = document.location.href;

		}

		var hash_pos = url.indexOf('#');

		// Si y'a un HASH

		if (hash_pos != -1){

			var re_key = new RegExp("[#&]"+key+"=","i");

			var key_pos = url.search(re_key);

			// Si la KEY est presente

			if (key_pos != -1){

				var amp_pos = url.indexOf('&', key_pos+1);

				// Si y'a un AMP qui suit
				if (amp_pos != -1){

					return (url.substring(0, key_pos+1+key.length+1) + value + url.substring(amp_pos));

				}else{

					return (url.substring(0, key_pos+1+key.length+1) + value);

				}

			}else{

				return (url + '&' + key + '=' + value);

			}

		}else{

			return (url + '#' + key + '=' + value);

		}

	},

	// Ajoute, ou remplace s'il existe, le/les HASH d'une URL a une autre

	add_current_hash:function(url_from, url_to){

		if (url_from == null){

			url_from = document.location.href;

		}

		var hash_pos_from = url_from.indexOf('#');

		// Si y'a un HASH FROM

		if (hash_pos_from != -1){

			var hash_pos_to = url_to.indexOf('#');

			// Si y'a un HASH TO

			if(hash_pos_to != -1){

				var keys_vals_from = url_from.substring(hash_pos_from+1).split('&');

				var key_val_from = new Array();

				for (k=0;k<keys_vals_from.length;k++){

					key_val_from = keys_vals_from[k].split('=');

					url_to = this.add_hash(key_val_from[0], key_val_from[1], url_to);

				}

				return(url_to);

			}else{

				return (url_to + url_from.substring(hash_pos_from));

			}

		}else{

			return (url_to);

		}

	},

	// Retire un GET (en utilisant sa KEY) d'une URL

	remove_get:function(key, url){

		if (url == null){

			url = document.location.href;

		}

		var key_pos = url.indexOf(key);

		// Si la KEY est trouvee

		if (key_pos != -1){

			var amp_pos = url.indexOf('&', key_pos);

			// Si y'a un AMP qui suit

			if (amp_pos != -1){

				return (url.substring(0,key_pos) + url.substring(amp_pos+1));

			}else{

				var hash_pos = url.indexOf('#', key_pos);

				// Si y'a un HASH

				if (hash_pos != -1){

					return (url.substring(0,key_pos-1) + url.substring(hash_pos));

				}else{

					return (url.substring(0,key_pos-1));

				}

			}

		}else{

			return (url);

		}

	},

	// Modifie la CIBLE d'une URL tout en gardant ses paramètres

	change_target:function(cible, url){

		if (url == null){

			url = document.location.href;

		}

		var re_param = new RegExp("[\?#]","i");

		var param_pos = url.search(re_param);

		// Si y'a des PARAM

		if (param_pos != -1){

			var param = url.substring(param_pos);

		}else{

			var param = '';

		}

		var slash_pos = url.lastIndexOf('/');

		return (url.substring(0,slash_pos+1) + cible + param);

	}

}

/* ###################################################### HEADER ##################################################### */

// Fonction appelee au "onload" du "body" (header.php)

function loaded(){

	set_menu_rollOver();

	set_subMenu_rollOver();

	lightview_glow();		//	Greffe le "glow" sur le "Lightview" (js/lightview_glow.js)

}

// Applique les fonctions de rollOver au boutons du menu inclus dans la liste "menu_items" (header.php)

function set_menu_rollOver(){

	for(k=0;k<menu_items.length;k++){

		if ((menu_items[k] != 'finalistes' || finaliste_on) && !is_menu_actif(menu_items[k])){

			menu_item = document.getElementById('menu_item_'+menu_items[k]);

			menu_item.onmouseover = function(){

				this.src = this.src.replace('N','O');

			}

			menu_item.onmouseout = function(){

				this.src = this.src.replace('O','N');

			}

		}

	}

}

// Applique les fonctions de rollOver au boutons du sous-menu (header.php)

function set_subMenu_rollOver(){

	if (!is_menu_actif('galeries')){

		var menu_item_galeries = document.getElementById('menu_item_galeries_right');

		menu_item_galeries.onmouseover = function(){

			this.style.width = '200px';

			var subMenu = document.getElementById('subMenu_galeries');

//			subMenu.style.display = 'block';

		}

		menu_item_galeries.onmouseout = function(){

			this.style.width = '0';

			var subMenu = document.getElementById('subMenu_galeries');

//			subMenu.style.display = 'none';

		}

	}

}

// Si la page actuelle correspond au bouton envoye: TRUE sinon FALSE (header.php)

function is_menu_actif(menu_item){

	switch(page){

	case 'finalistes':

	case 'details_finalistes':

	case 'commentaires_finalistes':

		return (menu_item == 'finalistes');

		break;

	case 'participer':

		return (menu_item == 'participer');

		break;

	case 'galeries':

	case 'details':

	case 'commentaires':

		return (menu_item == 'galeries');

		break;

	case 'faq':

		return (menu_item == 'faq');

		break;

	case 'concept':

		return (menu_item == 'concept');

		break;

	}

}

/* ###################################################### POPUP LIGHTVIEW ##################################################### */

function call_popupLV(popup, get, param){

	size = new Object();

	sizes = {

		xSmall:{

			width:450,

			height:120

		},

		small:{

			width:450,

			height:180

		},

		small_2:{

			width:450,

			height:220

		},

		medium:{

			width:500,

			height:310

		},

		medium2:{

			width:500,

			height:380

		},

		medium3:{

			width:500,

			height:240

		},

		medium4:{

			width:500,

			height:150

		},

		medium5:{

			width:500,

			height:190

		},

		medium6:{

			width:520,

			height:150

		},

		large:{

			width:500,

			height:500

		},

		xLarge:{

			width:600,

			height:500

		}

	}

	// True: l'appel est fait dans le parent

	par = false;



	switch(popup){

	case 'connexion':

		if (param){

			size = {width:500,height:260};

		}else{

			size = sizes.medium2;

		}

		break;

	case 'register':

		size = sizes.xLarge;

		par = true;

		break;

	case 'confirm_register':

		size = sizes.medium6;

		par = true;

		break;

	case 'motDePasseOublie':

		size = sizes.medium3;

		break;

	case 'confirm_motDePasseOublie':

		size = sizes.medium4;

		break;

	case 'screensaver':

		// Le texte du concour en francais prend 2 lignes

		if (param.lang=='fr' && param.phaseConcours){

			size = {width:510,height:160};

		}else{

			size = {width:510,height:144};

		}

		break;

	case 'confirm_update':

		size = sizes.xSmall;

		break;

	case 'confirm_upload':

		size = sizes.medium4;

		break;

	case 'supprimer':

		size = sizes.medium5;

		break;

	case 'confirm_supprimer':

		size = sizes.medium4;

		par = true;

		break;

	case 'voter':

		size = sizes.medium;

		break;

	case 'confirm_voter':

		size = sizes.medium4;

		par = true;

		break;

	case 'reglements':

		size = sizes.xLarge;

		break;

	case 'password':

		size = sizes.large;

		break;

	case 'change_password':

		size = sizes.medium3;

		break;

	case 'confirm_changedPassword':

		size = sizes.medium4;

		break;

	}





	if (par){

		parent.Lightview.show({

			href: '/PULV_'+popup+(get?get:''),

			rel: 'iframe',

			options: size

		});

	}else{

		Lightview.show({

			href: '/PULV_'+popup+(get?get:''),

			rel: 'iframe',

			options: size

		});

	}

}



/**

 *

 * Ajax save for notify_comments property.

 *

 **/

function display_piece_details(piece_id) {

	jQuery('#details #leftCol .piece.details').hide();



	var piece= jQuery('#details #piece-details-' + piece_id);

	if(piece.length === 0) {

		jQuery.get('/piece_details', {id:piece_id}, function(r) {

			piece= jQuery('#details #leftCol').append(r);

			FB.init({status: true, cookie: true, xfbml: true});

		});

	}



	jQuery('#details #gallery-container .piece').removeClass('active');

	jQuery('#details #gallery-container #piece-' + piece_id).addClass('active');



	piece.show();

}



/**

 *

 * Initialise l'affichage de la galerie détails

 *

 **/

function init_gallery_details() {

	jQuery('#details #gallery .piece .details-link').click(function() {

		var piece_id= jQuery(this).attr('id').replace('details-link-', '');

		display_piece_details(piece_id);



		return false;

	});



	var current= jQuery('#details #leftCol .piece.details:visible');

	var current_id= current.attr('id').replace('piece-details-', '');

	jQuery('#details #gallery-container #piece-' + current_id).addClass('active');



	resize_gallery();

	jQuery(window).resize(resize_gallery);

}



/**

 *

 * Like (or dislike) a comment.

 *

 **/

function like_comment(comment_id, rating) {

	rating=parseInt(rating);



	jQuery.post('/ajax_like_comment', {comment_id:comment_id,rating:rating}, function(response) {

		if(response) {

			response= parseInt(response);

			jQuery('#like-comment-' + comment_id).attr('src', '/images/like_comment_' + (response >= 1 ? 'on' : 'off') + '.png');

			jQuery('#dislike-comment-' + comment_id).attr('src', '/images/dislike_comment_' + (response <= -1 ? 'on' : 'off') + '.png');



			var likes= parseInt(jQuery('#comment-likes-' + comment_id).text());

			if(response == 0) {

				likes-= rating;

			} else {

				likes+= response;

			}



			jQuery('#comment-likes-' + comment_id).text(likes);

		}

	});

}



/**

 *

 * Affiche la prochaine pièce dans la galerie

 *

 **/

function next_piece(current_id) {

	var current= jQuery('#details #gallery #piece-' + current_id);

	var next= current.next();

	if(next.length === 0) {

		next= jQuery('#details #gallery .piece:first');

	}



	var next_id= next.attr('id').replace('piece-', '');

	display_piece_details(next_id);

}



function post_comment(piece_id) {

	var comment= jQuery('#new-comment-' + piece_id);

	comment_text= comment.val();



	if(comment_text.length === 0) {

		return false;

	}



	jQuery.post('/ajax_add_comment.php', {oeuvre:piece_id,comment:comment_text}, function(r) {

		jQuery('#comments-' + piece_id + ' .commentsBox').append(r);

		comment.val('');

	});

}



/**

 *

 * Affiche la précédente pièce dans la galerie

 *

 **/

function prev_piece(current_id) {

	var current= jQuery('#details #gallery #piece-' + current_id);

	var prev= current.prev();

	if(prev.length === 0) {

		prev= jQuery('#details #gallery .piece:last');

	}



	var prev_id= prev.attr('id').replace('piece-', '');

	display_piece_details(prev_id);

}



/**

 *

 * Ajax save for notify_comments property.

 *

 **/

function save_notify_comments(input) {

	jQuery.post('/ajax_save_notify_comments.php', {checked:jQuery(input).is(':checked')}, function(r) {

		// @todo: Handle response

	});

}





function facebookShare() {

	window.open('http://www.facebook.com/sharer.php?u='

				+ encodeURIComponent((lang == "fr") ? 'http://www.carteblancheauxcreatifs.ca/concept?lang=fr&region=quebec' : 'http://www.carteblancheauxcreatifs.ca/concept?region=quebec&lang=en')

				+ '&t=' + encodeURIComponent((lang == "fr") ? "Inscris-toi à Carte Blanche " : "Sign up for Carte Blanche"),

			'sharer', 'toolbar=0,status=0,width=750,height=500');

	return false;

}



function twitterShare() {

	window.open('http://twitter.com/home?status='

				+ encodeURIComponent((lang == "fr") ?

						"Tu te demandes où diable trouver un concours pour créatif publicitaire enfin digne de toi? Ici : http://bit.ly/a9LGpn. Bienvenue."

					  : "Got a creative itch to scratch? The Carte Blanche contest is here to help: http://bit.ly/a9LGpn"),

//				+ encodeURIComponent((lang == "fr") ? 'http://www.carteblancheauxcreatifs.ca/' : 'http://www.carteblancheforcreatives.ca/'),

			'sharer', 'toolbar=0,status=0,width=950,height=500');

	return false;

}



function linkedinShare() {

	window.open('http://www.linkedin.com/shareArticle?mini=true'

			//+ encodeURIComponent(location.href)

			+ '&url=' + encodeURIComponent((lang == "fr") ? 'http://www.carteblancheauxcreatifs.ca/' : 'http://www.carteblancheforcreatives.ca/')

			+ '&title=' + encodeURIComponent((lang == "fr") ? 'Inscris-toi à Carte Blanche' : 'Sign up for Carte Blanche')

			+ '&summary=' + encodeURIComponent((lang == "fr") ?

					"Cher confrère créatif, le concours Carte Blanche reprend. Qui mieux que toi pour remporter le voyage à Cannes et gagner la reconnaissance éternelle de l'industrie ? Honnêtement je vois pas. "

				  : "Dear genius creative, the Carte Blanche contest is back. And who better to win the trip to Cannes and the undying attention of the entire industry? That's right, you.")

			+ '&source=' + encodeURIComponent((lang == "fr") ? 'Carte Blanche' : 'Carte Blanche')

			, 'sharer', 'toolbar=0,status=0,width=950,height=450');

	return false;

}





//JQuery





( function($) {



	function initialisation() {

		if(typeof jQuery().accordion === 'function') {

			$( "#etapesAccordion" ).accordion({

				autoHeight: false,

				navigation: true,

				icons: false,

				active:$('#infoMenu_nomUtilisateur').length === 0 ? 5 : 5

			});

		}



		if(typeof jQuery().uniform === 'function') {

			$("select, input[type=text], input[type=checkbox], input[type=radio], input[type=file], input[type=submit], input[type=password], a.button, button").uniform();

		}



		var fileText = (lang == 'fr'? 'Aucun fichier sélectionné' : 'No file selected');

		var buttonText = (lang == 'fr'? 'Parcourir' : 'Select');



		$(":file").uniform({fileBtnText: buttonText, fileDefaultText: fileText});



	}







	$(document).ready(function() {

		initialisation();

	});



} ) ( jQuery );


