/*---- addClass ---*/
function addClass(){
	jQuery('.btn-category a,.btn-action a,.partners ul a,.btn-more a').click(function() {
		jQuery(this).addClass('mouse-down');
		jQuery(this).mouseout(function() {
			jQuery(this).removeClass('mouse-down');
		});
	});
}
/*--- IE6 hover ---*/
function ieHover(h_list, h_class){
	if(jQuery.browser.msie && jQuery.browser.version < 7){
		if(!h_class) var h_class = 'hover';
		jQuery(h_list).mouseenter(function(){
			jQuery(this).addClass(h_class);
		}).mouseleave(function(){
			jQuery(this).removeClass(h_class);
		});
	}
}
/*---- clear inputs ---*/
function clearInputs(){
	$('input:text, input:password, textarea').each(function(){
		var _el = $(this);
		var _val = _el.val();
		_el.bind('focus', function(){
			if(this.value == _val) this.value = '';
		}).bind('blur', function(){
			if(this.value == '') this.value = _val;
		});
	});
}
jQuery('document').ready(function(){
		
	addClass();
	clearInputs();
	
	// partner widget
	initPartnerWidget();
	
	jQuery('.twitterPost').bind('click.twitterPost', twitterPost);
	formatMenu();
	formatButton();
	
	ieHover('.nav li','hover');	
	if(jQuery('div.partners').find('ul > li').length > 6){
		jQuery('div.partners').gallSlide({
			duration: 1000,
			autoSlide: 8000
		});
	}
	
})
jQuery.fn.gallSlide = function(_options){
	// defaults options	
	var _options = jQuery.extend({
		duration: 700,
		autoSlide: 5000
	},_options);

	return this.each(function(){
		var _hold = jQuery(this);
		var _speed = _options.duration;
		var _timer = _options.autoSlide;
		var _wrap = _hold.find('ul');
		var _el = _hold.find('ul > li');
		var _count = _el.index(_el.filter(':last'));
		var _w = _el.outerWidth(true);
		var _wrapHolderW = Math.ceil(_wrap.parent().width()/_w);
		var _t;
		var _active = 0;
		function scrollEl(){
			_wrap.eq(0).animate({
				marginLeft: -(_w * _active) + "px"
			}, {queue:false, duration: _speed});
		}
		function runTimer(){
			_t = setInterval(function(){
				_active++;
				if (_active > (_count - _wrapHolderW + 1)) _active = 0;
				scrollEl();
			}, _timer);
		}
		runTimer();
		_hold.hover(function(){
			if(_t) clearTimeout(_t);
		}, function(){
			runTimer();
		});
	});
}

/** posts an url to twitter * */
twitterPost = function() {
	var clickedLink = jQuery(this);

	var link = clickedLink.attr('link');
	var text = clickedLink.attr('linktext');

	var login = 'tinotruppel';
	var apikey = 'R_2a368adda5137d8ec56773b8a48b952b';

	var url = 'http://api.bit.ly/v3/shorten?login=' + login + '&apiKey='
			+ apikey + '&longUrl=' + link + '&format=json';

	var twitterUrl = 'http://twitter.com/?status=';

	var data = $.ajax({
		url : url,
		timeout : 1000,
		dataType : 'json',
		success : function(data) {			
			var url = twitterUrl + text + ': ' + data['data']['url'];
			window.location.href = url;
			return false;
		},
		error : function(data) {
			var url = twitterUrl + text + ': ' + link;
			window.location.href = url;
			return false;
		}
	});
	return false;
};

/** if menu has subcategory, show the arrow down **/
formatMenu = function() {
	jQuery('div.drop').closest('.page_item').find('span.nosubcat').removeClass('nosubcat');	
};

formatButton = function() {
	var buttons = jQuery('#wrapper input[type=submit]');
	jQuery.each(buttons, function() {
		var button = jQuery(this);
		button.wrap('<div class="button-wrapper" />');
		var wrapper = button.closest('.button-wrapper');
		wrapper.append('<div class="button-right" />');
	});
};

initPartnerWidget = function() {
	$('#mycarousel').jcarousel({
		vertical: true,
		auto: 2,
		wrap: 'circular',
		scroll: 1
	});
};

/** image hover effect **/
/*
 * Image preview script 
 * powered by jQuery (http://www.jquery.com)
 * 
 * written by Alen Grakalic (http://cssglobe.com)
 * 
 * for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
 *
 */
 
this.imagePreview = function(){	
	/* CONFIG */
		xOffset = 10;
		yOffset = 30;
		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
		
	/* END CONFIG */
	$("a.preview").hover(function(e){
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='preview'><img src='"+ this.href +"' alt='Image preview' />"+ c +"</p>");								 
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.css("position","absolute")
			.css("z-index","99")
			.fadeIn("fast");						
    	},
	function(){
		this.title = this.t;	
		$("#preview").fadeOut("fast", function() {$("#preview").remove()});
    	});	
	$("a.preview").mousemove(function(e){
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});
};


// starting the script on page load
$(document).ready(function(){
	imagePreview();
});

