RowClicker = {'timer':null, 'doubleClickTimeDelay':1000, 'addShopcart': false, 'addShopcartInput': false};

$(document).ready(function() {
	 //alert('articlebygroup.js');
	RowClicker.init();
	Accessories.init();
	ArticleDetails.init();
    Results.init();
});

RowClicker.init = function()
{
	this.tableId = 'id_table_articlesbygroup';
	var isToShowTooltip = true;	
			
	$('#' + this.tableId + ' tbody td').bind('click', function() {		
		var articleId = this.parentNode.firstChild.firstChild.value;
		  //alert('articleId = ' + articleId);
		 
		// alert('RowClicker.addShopcart = ' + RowClicker.addShopcart 
		// + ' RowClicker.addShopcartInput =' + RowClicker.addShopcartInput);
		
		if (RowClicker.addShopcart == true) {
			var inputAmount = $('#id_input_addCart__' + articleId).val();
			//alert(inputAmount);
			RowClicker.variants = 0;
			RowClicker.toShopcart(articleId, RowClicker.variants, inputAmount);
			//InputArticleCount.setValue(inputAmount);
		} 		
		
		if ( RowClicker.addShopcart == false && RowClicker.addShopcartInput == false ) {
			RowClicker.onClick(articleId);
		}		
	})
	
	.tooltip({'id':'tooltip',
		'bodyHandler': function(){ 
		   if ($(this).attr('class') == 'add-to-shopcart' || $(this).attr('class').indexOf('no-tooltip') > -1) {
		   	return '';
		   }
		   var img = $('<img/>').attr({
					'src': 'articles/group/detailspicture/article_id/' + this.parentNode.firstChild.firstChild.value + '/locale_id/' + Locales.localeId
			});	
			return img;
		}
	}, isToShowTooltip);	
	
	// 'To shopcart' button
	$('#' + this.tableId + ' img.addToCartSmall').bind('mouseover', function() {
		RowClicker.addShopcart = true;		
	})
	.bind('mouseout', function() {
		RowClicker.addShopcart = false;
	}).css({
		'cursor': 'pointer'
	});
	
	$('input.addCart').bind('focus', function() {	
		RowClicker.addShopcartInput = true;
	})
	.bind('blur', function() {
		RowClicker.addShopcartInput = false;
	});
}
/*
RowClicker_.init = function()
{
	RowClicker.isClickedAddToCart = false;
		
	$('#id_table_searchresults tbody tr').bind('click', function() {		
		
		RowClicker.setClickedId(this.firstChild.firstChild.nextSibling.value);
		var articleId = RowClicker.getClickedId();
		RowClicker.setVariants(this.firstChild.firstChild.nextSibling.nextSibling.nextSibling.value);
		
		if (RowClicker.addShopcart == true) {
			var inputAmount = $('#id_input_addCart__' + articleId).val();
			//console.log(inputAmount);
			ShortInfo.toShopcart(articleId, RowClicker.variants, inputAmount);
			//InputArticleCount.setValue(inputAmount);
		}
		
		RowClicker.setAmount();
		RowClicker.onClick();
	})
	.bind('mouseover', function() {
		RowClicker.setHoveredId(this.firstChild.firstChild.nextSibling.value);
        RowClicker.onHover();
		//ShortInfo.noShowPicture();
	});

	$('#id_table_searchresults').bind('mouseout', function() {
		RowClicker.xableControls();
	});
	
	$('#id_table_searchresults .addToCartSmall').bind('mouseover', function() {
		RowClicker.addShopcart = true;
		//var clikId = RowClicker.getClickedId();
		//console.log(this);
		//console.log('warenkorb fuer: ' + clikId);
	})
	.bind('mouseout', function() {
		RowClicker.addShopcart = false;
	}).css({
		'cursor': 'pointer'
	});
	
	RowClicker.clickFirstRow();

}
*/

RowClicker.onClick = function(selectedId) {
	//alert('clicked ' + selectedId);
	Accessories.showByArticleId(selectedId);
	ArticleDetails.showByArticleId(selectedId);
}

RowClicker.onDoubleClick = function(selectedId) {
	//alert('double clicked ' + selectedId);
	//ArticleDetails.showByArticleId(selectedId);
	//Accessories.showByArticleId(selectedId);
}

RowClicker._onSelect = function(selectedId)
{
	if (RowClicker._timer != null) {
		// Check if this is the same selectedId
		if ( RowClicker.selectedId == selectedId ) { // This is a double click
		   
			RowClicker.__timerKill();
			RowClicker.onDoubleClick(RowClicker.selectedId);
			RowClicker.selectedId = 0;
		} else { // This is a new click
			// Remember new selectedId
			RowClicker.selectedId = selectedId;
			// Restart timer
			RowClicker._timerStart();
		}
	} else {
		RowClicker.selectedId = selectedId;
		RowClicker._timerStart();		
	}

}		


RowClicker._timerStart = function() 
{
	RowClicker.__timerKill();
	RowClicker._timer = window.setTimeout(function(){
		RowClicker._timerStop();	
	}, RowClicker.doubleClickTimeDelay);	
}

RowClicker._timerStop = function()
{
	RowClicker.__timerKill();
	RowClicker.onClick(RowClicker.selectedId);
	RowClicker.selectedId = 0;
}

RowClicker.__timerKill = function() 
{
	if (RowClicker._timer != null) {
		window.clearTimeout(RowClicker._timer);
	}
	
	RowClicker._timer = null;
}

RowClicker.toShopcart = function(articleId, variants, inputAmount)
{
	var url = "articles/ajax/getcountvariants/articleId/" + articleId + '/localeId/' + Locales.localeId;

   	$.getJSON(url , function(json) {
			switch (json.isSuccess) {
				case true:										
					if (json.countVariants > 0) {	
					    ArticleDetails.showByArticleId(articleId, null, inputAmount);	
					} else {		
						RowClicker._gotoShopcart(articleId, inputAmount);
					}
				break;
				default:
					//SecteoInfo.show('id_','Info');
				break;
			}
			//
		});

}

RowClicker._gotoShopcart = function(articleId, inputAmount) {	 
	//var artilceCount =  $('#article_count').attr('value');
		var url = "default/shopcart/addarticle/article_id/" + articleId + '/article_count/' + inputAmount
		+ '/localeId/' + Locales.localeId + '/strLocale/' + Locales.strLocale
		//alert(url);
		//$.get(url ,  function(json) {   alert(json);
    	$.getJSON(url , {'arrVariations':[]}, function(json) {
			switch (json.isSuccess) {
				case true:					
					MiniShopcart.refresh();
				break;
				default:
					//SecteoInfo.show('id_info_shopcart','Article has been not added');
				break;
			}
			//
		});
}

Results = {};
Results.init = function(){
    Results.check();
}

Results.check = function(){
    var tooMuch = $('#id-toomuch').val();
    if (tooMuch == 'true'){
        //alert('too much');
        SecteoInfo.show('id_info_searchResults','Ihre Suchanfrage ergab über 50 Treffer.<br /><br />Bitte schränken Sie Ihre Suche ein.');
    }
}