
/***************** JAVASCRIPT VARIABLES ***********************/
var LANDING_IMAGES_PATH = 'img/landing';
var LANDING_IMAGE_TIME_CHANGE = 3500;





$(document).ready(function() {

/* HOVER IMAGES */
function hovers () {
	$('.hover_icon, .hover_icon a').hover(
		function(){
			var bgPosToLeft = $(this).css('background-position').replace(/ .*$/, '');
			$(this).css('background-position', bgPosToLeft + ' bottom');
		},
		function(){
			var bgPosToLeft = $(this).css('background-position').replace(/ .*$/, '');
			$(this).css('background-position', bgPosToLeft + ' top');
		}
	);
	
	/* Add "_hover" at the images paths when rollover on it */
	$('.hover_img, .hover_img a').hover(
		function(){
			//if (!$(this).hasClass('menu_selected')) {
				$(this).addClass('hover_menuItem');
				var srcAddHover = $(this).attr('src').replace(/(\.\w{3,4}(?:"?\))?)$/, '_hover$1');
				$(this).attr('src', srcAddHover);
			//};
		},
		function(){
			//if (!$(this).hasClass('menu_selected')) {
				$(this).removeClass('hover_menuItem');
				var srcAddHover = $(this).attr('src').replace(/(_hover)+/, '');
				$(this).attr('src', srcAddHover);
			//}
		}
	);
}

/* Add "_hover" at the background images paths when rollover */
$('.hover_bg, .hover_bg a').hover(
	function(){
		var srcAddHover = $(this).css('background-image').replace(/(\.\w{3,4}(?:"?\))?)$/, '_hover$1');
		$(this).css('background-image', srcAddHover);
	},
	function(){
		var srcAddHover = $(this).css('background-image').replace(/(_hover)+/, '');
		$(this).css('background-image', srcAddHover);
	}
);




// {{{ vertical scrollbars
setTimeout(function(){
if ($.fn.jScrollPane != undefined) {
	$.fn.jScrollPane.defaults.showArrows = true;
	$.fn.jScrollPane.defaults.scrollbarWidth = 17;
	
	
	$('.vertical_scroll').jScrollPane().ready(function(){
	hovers();
	});
	
/* $('.vertical_scroll_stepjj').each(function(){
	// ArrowDown
	$(this).parent().find('.jScrollArrowDown').unbind('mousedown');
	$(this).parent()[0].currStep = 0;
	$(this).parent().find('.jScrollArrowDown').mousehold(200, function(){
		var prevTop = $(this).parent().find('.content_large_scroll').offset().top;
		// alert(($(this).parent()[0].currStep+1)+' == '+$(this).parent().find('.content_large_scroll tr:first,.content_large_scroll li').length);
		
		$(this).parent()[0].currStep++
		var elemH = $(this).parent().find('.content_large_scroll tr:first,.content_large_scroll li:first,.content_large_scroll div:first').height();
		$(this).parent().find('.content_large_scroll')[0].scrollTo(elemH*$(this).parent()[0].currStep+1)
		
		var currTop = $(this).parent().find('.content_large_scroll').offset().top;
		if (prevTop == currTop) {
			$(this).parent()[0].currStep--
		};
	});
	// ArrowUp
	$(this).parent().find('.jScrollArrowUp').unbind('mousedown');
	$(this).parent()[0].currStep = 0;
	$(this).parent().find('.jScrollArrowUp').mousehold(200, function(){
			if ($(this).parent()[0].currStep == 0) {
				return false;
			};
		$(this).parent()[0].currStep--;
		var elemH = $(this).parent().find('.content_large_scroll tr:first,.content_large_scroll li:first,.content_large_scroll div:first').height();
		var r = $(this).parent().find('.content_large_scroll')[0].scrollTo(elemH*$(this).parent()[0].currStep)
	});
}); */
};
}, 300);
// }}}


// {{{ LANDING PAGE
if ($('#landing_page').length) {
	$('#watch_video a').click(function(){
	$('#watch_video').media({
		src: 'img/landing/landingMovie.mov',
		width: 675,
		height: 440,
		autoplay: true,
		bgColor: 'transparent'
	});
	
	// stop changing background
	clearInterval(intervalchangeLandingBackground);
	return false;
});
	
	// Change background image
	var intervalchangeLandingBackground;
	var arrLandingImages = new Array();
	var urlJsonLanding = 'ajax/landing_images.json';
	$.getJSON(urlJsonLanding, function(data) {
		arrLandingImages = data.images;
		
		// preload images
		for(var i = 0; i<arrLandingImages.length; i++) {
			jQuery("<img>").attr("src", LANDING_IMAGES_PATH+'/'+arrLandingImages[i]);
		}
		
		changeLandingBackgroundImages();
		intervalchangeLandingBackground = setInterval(changeLandingBackgroundImages, LANDING_IMAGE_TIME_CHANGE);
	});
	function changeLandingBackgroundImages () {
		var newImageName = arrLandingImages.shift();
		$('#landing_page #site_content').css('background-image', 'url("'+LANDING_IMAGES_PATH+'/'+newImageName+'")');
		arrLandingImages.push(newImageName);
	}
}
// }}}

// {{{ Sign in button 
$('#signin_btn_menu').click(function() {
  $('#signin_panel').slideToggle();
});
// }}}


// left button
if ($('.menu_btn_left').mousehold != undefined) {
	
	$('.menu_btn_left').mousehold(200, function(){
		var c = $(this).parents('.rounded_large,.rounded_small').find('.menu_box_inner');

		if (c[0].currentPosition <= 0) {
			c[0].currentPosition = 0;
			return false;
		}
		c[0].currentPosition--;
		var newXpos = $(this).parents('.rounded_large,.rounded_small').find(".single_scroll:eq("+c[0].currentPosition+")").position().left;
		c[0].scrollTo(newXpos);

		return false;
	});

	// right  button
	$('.menu_btn_right').mousehold(200, func_right_button);
};


function func_right_button(){
	var c = $(this).parents('.rounded_large,.rounded_small').find('.menu_box_inner:first');
	c.css('position', 'relative');

	
	c[0].currentPosition++;
	
	// alert(c[0].currentPosition);
	// alert($(this).parents('.rounded_large,.rounded_small').find(".single_scroll").length);
	// alert(c[0].currentPosition != undefined && c[0].currentPosition > $(this).parents('.rounded_large,.rounded_small').find(".single_scroll").length)
	
	if (c.length == 0 || (c[0].currentPosition != undefined && c[0].currentPosition >= $(this).parents('.rounded_large,.rounded_small').find(".single_scroll").length)) {
		// stop horizontal scroll
		c[0].currentPosition--;
		return false;
	};

	// alert('oo')
	var nextElem = $(this).parents('.rounded_large,.rounded_small').find(".single_scroll:eq("+c[0].currentPosition+")")
	var newXpos = nextElem.position().left;
	
	// alert(newXpos+' > '+nextElem.width());
	// alert(newXpos+nextElem.width() + ' > '+(c.width() - c.parent().width()));
	if ((newXpos-nextElem.width()) > c.width() - c.parent().width()) {
		// alert('oltre');
		c[0].currentPosition--;
		return false;
	};
	c[0].scrollTo(newXpos);
	
	

	c.oldCssLeft = c.css('left');

	return false;
}
setCorners();
setFrames();


$('#list_categories li').hover(
	function(){
		$(this).addClass('li_hover');
	},
	function(){
		$(this).removeClass('li_hover');
	}
);



$('html').click(function() {
	//Hide the menus if visible
	$('#list_categories').hide();
	
	// $('#signin_panel').not(this).slideUp();
});

$('#btn_categories').click(function(event){
	event.stopPropagation();
});
$('#page_chain').click(function(event){
	event.stopPropagation();
});
$('#btn_categories').click(function() {
	$('#list_categories').toggle(/* function(){
			$('#btn_categories:visible').addClass('li_selected');
			$('#btn_categories:hidden').removeClass('li_selected');
	} */);
});



$('#index #large1 tr').each(function(){
$(this).click(function() {
	// add "selected" class for the <tr>
	$(this).parent().find('.selected_table').removeClass('selected_table');
	$(this).addClass('selected_table');
	
	var urlJson = 'ajax/index_recomm_box__'+$(this).find(':first').text()+'.json';
	var userid = $(this).attr('id').replace(/[^\d]+/, '');
		$.getJSON(urlJson, { id: userid }, function(data) {
		$('#large2 .horizontal_scroll').fadeOut( function () {
			
				// remove all the previous <li>
				$('#large2 .horizontal_scroll li:not(.template)').remove();
				
				// remove horizontal pagination
				$('#large2 .content_large #recomm_pagination').remove();
				$('#large2 .title_box .recomm_pagination a, #large2 .title_box .recomm_pagination img').remove();
								
				// add dots pagination
				$('#large2 .recomm_pagination').append('<img src="img/page_mark_current.png" />');
				for (var i=0; i<data.products.length-1; i++) {
					$('#large2 .recomm_pagination').append('<a href="#"><img src="img/page_mark.png" class="page_mark" /></a>');
				}
				
				// write products
				for (var i=0; i<data.products.length; i++) {
					var prod = data.products[i];
					var elemTpl = $(this).find('.template').clone();
					elemTpl.find('.txt_desc').text(prod.desc);
					elemTpl.find('.txt_title').text(prod.title);
					elemTpl.find('.txt_price').text('From '+prod.price);
					elemTpl.find('.cell img').attr('src', prod.img);
					
					elemTpl.removeClass('template');
					elemTpl.appendTo($(this).find('ul'));
				}
			
			
			/* text(data.products.title);
			$('.recomm_tot_pages').text(data.products.length); */
			
			// copy and remove tag .tempate
			// var elemTemplate = $('#large2 .horizontal_scroll .template').clone();
			// $('#large2 .horizontal_scroll .template').remove();
			
			// before initHScroll, remove the .template element.
			var elemTemplate = $('#large2 .template').clone();
			$('#large2 .template').remove();
			
			initHScroll('#box2', '.horizontal_scroll', 'recomm_pagination');
			
			// .. and insert after the scroll generation
			elemTemplate.appendTo($('#large2 .content_large .horizontal_scroll ul'));
			
			// move the left arrow
			$('#large2 .content_large #recomm_pagination').css('width', 20*(data.products.length+2));
			
			
			// elemTemplate.insertAfter(elemTpl);
			
			$(this).fadeIn();
			// $('#large2 .horizontal_scroll').fadeIn();
		});
	});
});
});


$('.table_hover tr').hover(
	function(){
		$(this).addClass('tr_hover');
	},
	function(){
		$(this).removeClass('tr_hover');
	}
);


// {{{ EDIT FRIENDS
$('#edit_friends #check_all_users').click(function(){
	$('#edit_friends #box_list_friends input[type=checkbox]').each(function(){
		$(this).attr('checked', 'checked');
	});
	return false;
});
$('#edit_friends #uncheck_all_users').click(function(){
	$('#edit_friends #box_list_friends input[type=checkbox]').each(function(){
		$(this).attr('checked', '');
	});
	return false;
});
// }}}


});

// END DOCUMENT READY

imgW = null;
imgH = null;
function setFrames () {
	
	
	// $("<div class=\"rb\"></div>").appendTo('.jFrames');
	// $("<div class=\"rb_content\"></div>").clone('.jFrames').append('.jFrames');
	// $('.jFrames').addClass('rb_content');
	// $('.jFrames').addClass('rb');
	$('.jFrames').each(function(){
		
		var aa = $(this).clone();
		
		$(this).empty();
		
		aa.attr('id', '');
		aa.attr('class', '');
		aa.addClass('rb_content');
		$(aa).appendTo(this);
		// $(this).find('.rb_content').append(aa);
		
		var arrDivs = new Array('tl', 'tr', 'bl', 'br', 't', 'b', 'l', 'r');
	
		for (var i=0; i<arrDivs.length; i++) {
			$('<div>')
			.addClass(arrDivs[i])
			.appendTo($(this))
		}
			
		
		// dimensions top left image
		
		var urlImgTL = $('.jFrames .tl').css('background-image').match(/((?:http:|file:)[^"]*)/).pop();
		
		var img = new Image();
		img.src = urlImgTL;
		$(this).find('.tl').css('height', img.height);
		$(this).find('.t').css('height', img.height);
		$(this).find('.tr').css('height', img.height);
		
		$(this).find('.tl').css('width', img.width);
		$(this).find('.l').css('width', img.width);
		$(this).find('.bl').css('width', img.width);
		
		
		$('.jFrames .t, .jFrames .b').css('left', img.width + 'px');
		$('.jFrames .l, .jFrames .r').css('top', img.height + 'px');
		
		$(this).find('.rb_content').css('margin-top', img.height + 'px');
		$(this).find('.rb_content').css('margin-left', img.width + 'px');
		
		
		// dimensions bottom right image
		var urlImgBR = $('.jFrames .br').css('background-image').match(/((?:http:|file:)[^"]*)/).pop();
		var img = new Image();
		img.src = urlImgBR;
		
		$(this).find('.br').css('height', img.height);
		$(this).find('.b').css('height', img.height);
		$(this).find('.bl').css('height', img.height);
		
		$(this).find('.br').css('width', img.width);
		$(this).find('.tr').css('width', img.width);
		$(this).find('.r').css('width', img.width);
		
		$('.jFrames .t, .jFrames .b').css('right', img.width + 'px');
		$('.jFrames .l, .jFrames .r').css('bottom', img.height + 'px');
		
		$(this).find('.rb_content').css('margin-bottom', img.height + 'px');
		$(this).find('.rb_content').css('margin-right', img.width + 'px');
		
		
	});
	
	
}

// {{{ Rounded corners
var classes = new Object();
/* classes.rounded_small = ['br', 'bl', 'tr', 'tl', 'r', 'l', 'b', 't']; */
classes.rounded_large = ['br', 'bl', 'tr', 'tl', 'r', 'l', 'b', 't'];
classes.rounded_panel = ['br', 'bl', 'tr', 'tl', 'r', 'l', 'b', 't'];

/* without firstLoad, returning to the page would result in repeated and unecessary multiple cornering */
var firstLoad = false;
function setCorners() {
	if (firstLoad) { return null; }
	firstLoad = true;
	for (i=0;i<document.getElementsByTagName("div").length; i++) {
		var className = document.getElementsByTagName("div").item(i).className;
		var listClasses = className.split(' ');
		for (var k=0; k<listClasses.length; k++) {
			className = listClasses[k];
			if (classes[className]) {
				var inner = document.getElementsByTagName("div").item(i).innerHTML;
				var classArray = classes[className];
				for (var j in classArray) {
					var style = '';// 'float:' + $(document.getElementsByTagName("div").item(i)).css('float');
					inner = divWrap(inner, className + '-' + classArray[j], style);
				}
				document.getElementsByTagName("div").item(i).innerHTML = inner;
			}
		}
	}
	
	initHScroll('#box2', '.horizontal_scroll', 'recomm_pagination');
	initHScroll();
}

function divWrap(value, className, style) {
	return '<div class="' + className + '" style="'+ style +'">' + value + '</div>';
}
// }}}


// {{{ horizontal scrollbar
var sudoSlider;
var idBox;
function initHScroll (idBox, idElemScroll, idElemPagination) {
	
	if ( idElemScroll != undefined) {
		sudoSlider = $(idBox + ' ' + idElemScroll).sudoSlider({
			auto: false,
			pause: 1000000,
			continuous: false,
			startSlide: false,
			controlsFade: "first",
			clickableAni:true,
			updateBefore: true,
			controlsAttr:'id="'+idElemPagination+'"',
			beforeAniFunc: function(t){
				$(idBox+' .recomm_pagination img').attr('src', 'img/page_mark.png');
				$(idBox+' .recomm_pagination img:eq('+(t-1)+')').attr('src', 'img/page_mark_current.png');
			}
		});
	} else {
		sudoSlider = $(".horizontal_scroll:not(.not_dafault_slide)").sudoSlider({
			auto: false,
			prevNext:true,
			pause: 1000000,
			continuous: false,
			startSlide: false,
			controlsFade: "first",
			clickableAni:true
		});
	}
}
// }}}

// {{{ Sort table
function sortTable(elem, table, mode) {
	// mode = "asc" | "desc"
	$(table).find("tr").tsort("td:eq(1)",{order:mode});
	
	var newSrc = $(elem).parent().find('.sort_desc').attr('src').replace(/(_hover)+/, '');
	$(elem).parent().find('.sort_desc').attr('src', newSrc);
	var newSrc = $(elem).parent().find('.sort_asc').attr('src').replace(/(_hover)+/, '');
	$(elem).parent().find('.sort_asc').attr('src', newSrc);

	if ( mode == 'asc') {
		newSrc = $(elem).find('img').attr('src').replace(/(\.\w{3,4})$/, '_hover$1');
	} else {
		newSrc = $(elem).find('img').attr('src').replace(/(\.\w{3,4})$/, '_hover$1');
	}
	$(elem).find('img').attr('src', newSrc);
}

// }}}




