/**
 * We use the initCallback callback
 * to assign functionality to the controls
 */
function mycarousel_initCallback(carousel, state) {
	var JCcontainerID = carousel.clip.context.id;
	
	$('#' + JCcontainerID +'_triggers a.jumper').bind('click', function(el) {
		carousel.scroll($.jcarousel.intval($(this).attr('myValue')));
		return false;
	});

	$('#' + JCcontainerID + '_triggers .jcarousel-scroll select').bind('change', function() {
		carousel.options.scroll = $.jcarousel.intval(this.options[this.selectedIndex].value);
		return false;
	});

	if (state == 'init') {
		carousel.startAutoOrig = carousel.startAuto;
		carousel.startAuto = function() {
			if (!carousel.paused) {
				carousel.startAutoOrig();
			}
		}
		carousel.pause = function() {
			carousel.paused = true;
			carousel.stopAuto();
		};
		carousel.play = function() {
			carousel.paused = false;
			carousel.startAuto();
		};
		$('li.jcarousel-item').mouseover(function() {
				carousel.pause();
		});
		$('li.jcarousel-item').mouseout(function() {
				carousel.play();
				// carousel.next();
		});
	}

	carousel.play();
	
};

function mycarousel_triggerClassCheck(carousel, liObj,  posIndex, stateAction) {
	var JCcontainerID = carousel.clip.context.id;
	
	//console.log(carousel, liObj,  posIndex, stateAction);
	
	$('#' + JCcontainerID +'_triggers a').each(function(){
		if(!$(this).hasClass('ignoreMe')){
			if($(this).attr('myValue') != posIndex){
				$(this).html('<img src="' + baseHref + 'images/icons/slides_inactiv.png" alt="" />');
				// $(this).removeClass('slideActiv');
				// $(this).addClass('slideInactiv');
			} else {
				$(this).html('<img src="' + baseHref + 'images/icons/slides_activ.png" alt="" />');
				// $(this).removeClass('slideInactiv');
				// $(this).addClass('slideActiv');
			}
			$(this).addClass('ignoreMe');
		}
	});

}

// autocomplete fields
$.fn.completeMe = function() {
	$(this).each(function(){
		$(this).focus(function(){
			if(this.value == this.defaultValue){
				this.value = '';
				if(this.innerHTML)
					this.innerHTML = '';
			}
		});
		
		$(this).blur(function(){
			if(this.value == ''){
				this.value = this.defaultValue;
				if(this.innerHTML)
					this.innerHTML = this.defaultValue;
			}
		});
	});
}

// force ajax on the forms - only for fancybox forms!!!
$.fn.ajaxMe = function() {
	$(this).each(function() {
		$(this).bind("submit", function() {
			$.fancybox.showActivity();

			$.ajax({
				type		: "POST",
				cache	: false,
				url		: $(this).attr('action'),
				data		: $(this).serializeArray(),
				success: function(data) {
					$.fancybox(data);
				}
			});
			
			return false;
		});
	});
}

// printer friendly
$.fn.printerFriendly = function() {
	var urlToOpen = $(location).attr('href') + '?printPage=1';
	var x = (screen.width-1000)/2, y = (screen.height-600)/2;
	$(this).each(function() {
		$(this).bind("click", function() {
			window.open(urlToOpen, "CtrlWindow", "width=1000,height=600,toolbar=no,menubar=yes,location=no,scrollbars=yes,resizable=no, screenX="+x+", screenY="+y+", left="+x+", top="+y);
			return false;
		});
	});
}

$.fn.mediaShare = function() {
	// var urlToOpen = $(location).attr('href') + '?printPage=1';
	var x = (screen.width-626)/2, y = (screen.height-436)/2;
	$(this).each(function() {
		$(this).bind("click", function() {
			// console.log(this.href);
			window.open(this.href, "CtrlWindow", "width=626,height=436,toolbar=no,menubar=yes,location=no,scrollbars=yes,resizable=no, screenX="+x+", screenY="+y+", left="+x+", top="+y);
			return false;
		});
	});
}

// timed page reload
$.fn.reloadPage = function(timerS) {
	if (!timerS) timerS = 1; //defaults to imediate
	setTimeout(function() {
		location.reload();
	}, timerS);
}

// force ajax on the first click on the share links
$.fn.setupShareLinks = function() {
	// var baseHref = $('base').attr('href')
	var ajaxTo = baseHref + 'index.html' + '/mainpage|getShortUrl';
	
	$(this).each(function() {
		$(this).bind("click", function() {
			// show activity
			var icon = $(this).children(':first');
			icon.attr('orig-src', icon.attr('src'));
			icon.attr('src', 'images/butoane/r0.gif');
			
			$.ajax({
				type		: "POST",
				cache	: false,
				url		: ajaxTo,
				data		: { url: $(location).attr('href') },
				success: function(data) {
					// short url generation is a success
					$('.shareLink').each(function() {
						$(this).attr('href', this.href + data);
						$(this).unbind('click');
					});
					icon.attr('src', icon.attr('orig-src'));
					$('.shareLink').removeAttr('orig-src');
				}
			});
			
			return false;
		});
	});
}

// show unintrusive browser deprecated warning
function ieDeprecated(ver, msg) {
	if (!ver) ver = 6; //default warns on ie 6 and lower
	if (!msg) msg = 'Your browser is deprecated. &nbsp; Please update to latest version to enjoy the full web experience !'; //default message

	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { //test for MSIE x.x;
		var ieversion = new Number(RegExp.$1); // capture x.x portion and store as a number
		if (ieversion <= ver) {
			$.jnotify(msg, true);
		}
	}
};

// custom pagination
function setPage(pageID, hInput, formID) {
	$('#' + hInput).val(pageID);
	$('#' + formID).submit();
}

// what to do when DOM fully loaded
$(document).ready(function() {
	
	$('form.ajaxMe').ajaxMe();
	
	// $('.completeMe').completeMe();
	
	// fancy box initialization
	$(".fancyb").fancybox({
		// 'type'					: 'iframe',
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'hideOnOverlayClick': true,
		'centerOnScroll'		: true,
		'overlayOpacity'		: 0.2
		// 'showCloseButton'		: false
		// 'autoDimensions'		: false,
		// 'width'							: 900,
		// 'height'						: 550
	});
	
	// short url generation
	// $('.shareLink').setupShareLinks();
	
	// print page buttons prep
	$('.printPage').printerFriendly();
	$('.mediaShare').mediaShare();
	
	$('body div:not(#footer) a:not(.anchorclass)').each(function () {
		// console.log($(this));
		var href = $(this).attr('href');
		href = (typeof href !== 'undefined') ? href : '';
		// console.log('href.length > 1', href.length > 1);
		// console.log('href.indexOf(\'#\') !== 0', href.indexOf('#') !== 0);
		// console.log('href.indexOf(baseHref) !== 0', href.indexOf(baseHref) !== 0);
		if (href.length > 1 && href.indexOf('#') && href.indexOf(baseHref)) {
			// console.log('X', $(this));
			$(this).attr('target', '_blank');
			// $(this).addClass('external');
		}
		// else {
			// console.log('N', $(this));
		// }
	});
	
	// ie browser deprecation notice
	ieDeprecated();
	
});

