/**
 * We use the initCallback callback
 * to assign functionality to the controls
 */
function mycarousel_initCallback(carousel) {
	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;
	});

	$('#' + JCcontainerID + '_triggers .' + JCcontainerID + '-next').bind('click', function() {
		carousel.next();
		return false;
	});

	$('#' + JCcontainerID + '_triggers .' + JCcontainerID + '-prev').bind('click', function() {
		carousel.prev();
		return false;
	});
	
	// additional triggers for this design
	$('#' + JCcontainerID + '_triggers .simplu-next').bind('click', function() {
		carousel.next();
		return false;
	});

	$('#' + JCcontainerID + '_triggers .simplu-prev').bind('click', function() {
		carousel.prev();
		return false;
	});
	
}

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).removeClass('activ');
				// $(this).addClass('slideInactiv');
			} else {
				// $(this).removeClass('slideInactiv');
				$(this).addClass('activ');
				// hack for showing title & teaser outside the carousel
				$('#myBannersCarousel_title').html($(this).attr('data-title'));
				$('#myBannersCarousel_teaser').html($(this).attr('data-teaser'));
			}
		}
	});

}

function mycarousel_fadeOut(carousel) {
	var JCcontainerID = carousel.clip.context.id;
	$('#' + JCcontainerID).fadeOut();
}

function mycarousel_fadeIn(carousel) {
	var JCcontainerID = carousel.clip.context.id;
	$('#' + JCcontainerID).fadeIn();
}

// 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-800)/2, y = (screen.height-600)/2;
	$(this).each(function() {
		$(this).bind("click", function() {
			window.open(urlToOpen, "CtrlWindow", "width=800,height=600,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 target="_blank" on links with class name "target_blank"
// $.fn.setupTargetBlank = function() {
	
	// $(this).each(function() {
		// $(this).live("click", function() {
			// window.open(this.href);
      // return false;
		// });
	// });
	
// }

function setupTargetBlank() {
	$('a.target_blank').live('click', function() {
		window.open(this.href);
    return false;
	});
}

// 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 prevHref = $(this).attr('href');
			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');
						$(this).removeClass('shareLink');
						$(this).addClass('target_blank');
					});
					icon.attr('src', icon.attr('orig-src'));
					$('.shareLink').removeAttr('orig-src');
					window.open(prevHref + data);
				}
			});
			
			return false;
		});
	});
}

// show tooltip with ajax data
function tooltipMe(tooltipClass) {

	$('a.'+tooltipClass).each(function(){
		$(this).qtip({
			content: {
				text: '<img class="throbber" src="images/butoane/r0.gif" alt="Se incarca..." />',
				ajax: {
					url: $(this).attr('href') // Use the href attribute of each element for the url to load
				},
				title: {
					text: $(this).attr('title'), // Give the tooltip a title using each elements title
					button: true
				}
			},
			position: {
				at: 'top center', 
				my: 'bottom center',
				adjust: { screen: true }
			},
			show: {
				event: 'click',
				solo: true
			},
			hide: 'unfocus',
			style: {
				classes: 'ui-tooltip-wiki ui-tooltip-light ui-tooltip-shadow'
			}
		})
		// Make sure it doesn't follow the link when we click it
    .click(function() { 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();
}

// asynchronous JavaScript Google Analytics
function initiateGoogleAnalytics() {

	var analyticsKey = $('#analyticsKey').attr('data-myValue');
	if(analyticsKey != '') {
		var _gaq = _gaq || [];
		_gaq.push(['_setAccount', analyticsKey]);
		_gaq.push(['_trackPageview']);

		(function() {
			var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
			ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
			var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
		})();
	}
	
}

// what to do when DOM fully loaded
$(document).ready(function() {
	
	// initiateGoogleAnalytics();
	
	// $('form.ajaxMe').ajaxMe();
	
	$('.completeMe').completeMe();
	
	// fancy box initialization
	$(".fancyb").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'width'					: 670,
		'height'				: 550
	});
	
	// XHTML 1.1 compliant target blank replacement
	// $('a.target_blank').setupTargetBlank();
	setupTargetBlank();
	
	// short url generation
	$('.shareLink').setupShareLinks();
	
	// print page buttons prep
	$('.printPage').printerFriendly();
	
	// video js
	if(typeof VideoJS != 'undefined')
		VideoJS.setupAllWhenReady();
		
	// left menu
	// anylinkcssmenu.init("anchorclass");
	
	// awesome tooltip
	// tooltipMe('tooltipMe');
	
	// shoppingcart updater
	// $('#shoppingcartWidget').ekko({
		// minTimeout	: 3000,
		// multiplier	: 1.2,
		// url : $('#shoppingcartWidget').attr('data-url')
	// }, function (data) {
		// $('#shoppingcartWidget').html(data);
	// });

	// ie browser deprecation notice
	ieDeprecated();
	
});
