/* HandySelector
-----------------------------------------------------------*/
droppedItems = 0;
$(document).ready(function() {
	
	toogleHandyItemDetail();
	toggleCompareBtn();

	var factsheet_id = getVar('add');
	var document_id = getVar('add_tid');	
	var directItemId = factsheet_id + '_' + document_id;
	var isDropped = $('div.droppedItem[rel="'+directItemId+'"]');

	if (factsheet_id != '') {
		if (isDropped.length == 0) {
			if(droppedItems < 3) {
				var selector = $('div[rel="'+directItemId+'"]');
				var itemToDrop = $('div[rel="'+directItemId+'"]');
				$(itemToDrop).clone().insertBefore('#handysSelected .compareDroppedItems');
				$(selector).parent().parent().addClass('dropped'+directItemId).hide();
				changeDroppedItem();
				droppedItems++;
				toggleCompareBtn();
				writeSession();
			}
			else {
				alert('Es können maximal 3 Handys verglichen werden!');
			}
		}
	}
	

	// Drop Item to Selected List
	$('.addItem').click(function() {
		addItem( this );
	});

	$('.modell').click(function() {
		$(this).next().find('.addItem').trigger('click');
	});

	// Delete dropped Item from Selected List
	$('.deleteDroppedItem').live('click', function () {
		var id = $(this).parent().attr('rel');
		
		$(this).parent().remove();
		droppedItems--;
		toggleCompareBtn();
		$('#handyList .dropped'+id).show().removeClass('dropped'+id);
		writeSession();
	});

	// Get Document-Ids from Selected Items write to Session
	$('.compareDroppedItems').click(function () {
		writeSession();
		parent.window.document.location.href = "http://www.tarifagent.com/de/mobile_world/tests/factsheet_compare.php";
		$(this).html('Handyvergleich wird gestartet...');
	});

	// Toolbox with max 3 Items on right Colum of Mainpage
	$('#container_compare .close').click(function(){
		deleteItemWriteSession(this);
		return false;		
	});

	$('#container_compare .more').click(function() {
		$(this).html('Handyvergleich wird gestartet...');
	});
	
});

function addItem ( btnRef ) {
	if(droppedItems < 3) {
		var itemToDrop = $(btnRef).parent().parent().parent('div').find('.itemToDrop');
		$(itemToDrop).clone().insertBefore('#handysSelected .compareDroppedItems');

		var id = $(btnRef).parent().parent().attr('rel');
		$(btnRef).parent().parent().parent().parent().addClass('dropped'+id).hide();

		changeDroppedItem();
		droppedItems++;
		toggleCompareBtn();
		writeSession();
	}
	else {
		alert('Es können maximal 3 Handys verglichen werden!');
	}
}


function toogleHandyItemDetail () {
	$('#handyList ul li ul li').hover(function(){
		$(this).find('.item').show();
	}, function(){
		$(this).find('.item').hide();
	});
}

function changeDroppedItem () {
	var droppedItem = $('#handysSelected .itemToDrop');
	var deleteBtn = '<span class="deleteDroppedItem">[x]</span>';

	$(droppedItem).prepend(deleteBtn);
	$(droppedItem).removeClass('itemToDrop').addClass('droppedItem');
}

function writeSession() {
	var factsheet_id = '';
	var document_id = '';
	$('#handysSelected .droppedItem').each(function(i){
		var tempId = $(this).attr('rel');
		var arrId = tempId.split('_');

		if(document_id == '') { 
			factsheet_id = arrId[0];
			document_id = arrId[1];
		}
		else {
			factsheet_id = factsheet_id + ',' +arrId[0];
			document_id = document_id + ',' + arrId[1];
		}
		
	});
	var url = 'http://www.tarifagent.com/helper/handytest_writeSession.php?factsheet_id=' + factsheet_id + '&document_id=' + document_id;
	$.post(url);	
}
function toggleCompareBtn () {
	if (droppedItems == 0) {
		$('.compareDroppedItems').hide();
	}
	else {
		$('.compareDroppedItems').show();
	}
}

function checkSelectedItems () {
	
	for(i=0; i < arrSelectedItems.length; i++){
		
		var id = arrSelectedItems[i];
		var itemToDrop = $('.itemToDrop[rel="' + id + '"]');
		$(itemToDrop).clone().insertBefore('#handysSelected .compareDroppedItems');
		$(this).parent('div').hide();
		$(itemToDrop).parent().parent().addClass('dropped' + id).hide();

		changeDroppedItem();
		if(id != '_'){
			droppedItems++;
		}
		toggleCompareBtn();
	}
}

// Get Post-Variable
function getVar(name) {
	get_string = document.location.search;         
	return_value = '';
	 
	do { //This loop is made to catch all instances of any get variable.
		name_index = get_string.indexOf(name + '=');
	    
		if (name_index != -1) {
			get_string = get_string.substr(name_index + name.length + 1, get_string.length - name_index);
	      
			end_of_value = get_string.indexOf('&');
			if(end_of_value != -1)                
				value = get_string.substr(0, end_of_value);                
			else                
				value = get_string;                
	        
			if(return_value == '' || value == '')
				return_value += value;
			else
				return_value += ', ' + value;
		}
	} while(name_index != -1)
	    
	//Restores all the blank spaces.
	space = return_value.indexOf('+');
	while(space != -1) { 
		return_value = return_value.substr(0, space) + ' ' + 
		return_value.substr(space + 1, return_value.length);
						 
		space = return_value.indexOf('+');
	}
	
	return(return_value);        
}

// Toolbox with max 3 Items on right Colum of Mainpage
function deleteItemWriteSession(btnRef) {

	$(btnRef).next().css('background', '');
	$(btnRef).remove();

	var factsheet_id = '';
	var document_id = '';
	$('#container_compare .img .close').each(function(i){
		var tempId = $(this).attr('rel');
		var arrId = tempId.split('_');

		if(document_id == '') { 
			factsheet_id = arrId[0];
			document_id = arrId[1];
		}
		else {
			factsheet_id = factsheet_id + ',' +arrId[0];
			document_id = document_id + ',' + arrId[1];
		}
		
	});
	var url = 'http://www.tarifagent.com/helper/handytest_writeSession.php?factsheet_id=' + factsheet_id + '&document_id=' + document_id;
	$.post(url, function(data){
		location.reload();
	});
	
}
