

// Make grid
function makeGrid(objects, rowIndex){
	objects.each(function(i){
		var colIndex = i%10;
			if (colIndex == 0 && i > 0){
				rowIndex++;
			}
		$(this).css({'left': colIndex * (53), 'top': rowIndex * (78)});
	});
}

// Make arrays
function makeArray(items, array){
	items.each(function(){
		if ($(this).filter(':visible').hasClass('small')){
			array.push($(this));
		}else{
			array.push($(this));
			array.push($(this));
		}
	});
}

function checkHigh(itemA, itemB){
	posA = itemA.position();
	posB = itemB.position();
	if (posA.top+itemA.height() > posB.top+itemB.height()){
		return posA.top+itemA.height();
	}else{
		return posB.top+itemB.height();
	}
}

// Sort Coulmns
function sortCols(){
	$('#images img.moved').removeClass('moved');
	
	var prevLine = new Array();
	var thisLine = new Array();
	makeArray($('#line'+(colCount-1)+' img:visible'), prevLine);
	makeArray($('#line'+colCount+' img:visible'), thisLine);
	
	for (x in thisLine){ // small images
		if (thisLine[x].hasClass('small')){
			var prevPos = prevLine[x].position();
			thisLine[x].animate({'top': prevPos.top+prevLine[x].height()+8},250);
		}else{ // big images
			if (x == 0){
				continue;
			}else{
				if (thisLine[x-1].hasClass('small')){
					continue;
				}else{
					var prevHigh = checkHigh(prevLine[x], prevLine[x-1]);
					thisLine[x].animate({'top': prevHigh+8},250).addClass('moved');
				}
			}
		}
	}

	if (colCount == $('#images span').length-1){
		// to do: fix discolored images
		$('#iframe').remove();
		window.clearInterval(vertAlign);
	}else{
		colCount++;
	}
}

// Row reset
function rowReset(objects, rowIndex){
	objects.each(function(i){
		var colIndex = i%10;
		if (colIndex == 0 && i > 0){
			rowIndex++;
		}
		$(this).css({'top': rowIndex * (60)});//74/1.25
	});	
}

// Switch color 2 black & white images
function switchBW(image, state){
	var temp = image.attr('src');
	switch(state){
		case 'color':
			temp = temp.replace('bw_','');
			break;
		case 'black':
			if (temp.search(/bw_/) == -1){
				temp = temp.replace('pics/','pics/bw_');
			}
			break;
		default:
			return false;
	}
	image.attr('src',temp);
}

// show vote panel
function votePanel(obj){
	$.ajax({
		type: "GET",
		url: "info.asp",
		data: {id: obj.attr('rel')},
		cache: false,
		beforeSend: function(){
			$('#info').empty();
		},
		complete: function(){
			// omitted for the meantime
		},
		success: function(msg){
			// put together all the data parts
			$('#info').append('<h3>'+obj.attr('alt')+'<span>'+$('#categories a.selected').html()+'</span></h3>'+msg);
			if ($('#noVote').get(0) == undefined){
				$('#info').append('<a class="vote" href="javascript:void(0)" target="_self"><span>הצביעו בעד</span></a>');
				$('#info a.vote').click(function(){
					$.post("vote.asp", {id: obj.attr('rel')}, function(){
						$('#info a.vote').css({'backgroundPosition': '0 -78px', 'cursor': 'auto'}).unbind('click');
					});
				});
			}else{
				$('#info').append('<a class="noVote" href="javascript:void(0)" target="_self"><span>הצביעו בעד</span></a>');
			}
			var thisCat = obj.get(0).className;
			thisCat = thisCat.substr(3,1);
			$('#info').append('<a class="results" href="results.asp?cat='+thisCat+'" target="_self">צפו בתוצאות</a>');
			
			var infoPos = obj.position();
			$('#info').css({'top': infoPos.top+74, 'left': infoPos.left-19}).fadeIn('fast');
		}
	});
}

// main function
function allActions(){
	// Initialize
	$('#preloader').remove(); 
	$('#categories a.'+current).addClass('selected');
	var allObj = $('#images img');
	var startCat = allObj.filter('.'+current);
	var finished = allObj.length - startCat.length;
	
	// order
	makeGrid(allObj, 0);
	startCat.nextAll().animate({'left': '+=53'}, 250).end().addClass('big');
	allObj.not('.'+current).addClass('small'); //fix for IE ...
	
	// hide
	allObj.css('visibility','visible');
	var safeItemsStart = allObj.not('.'+current);
	for (v=0; v<4; v++){
		var randomCat = Math.ceil(10*Math.random());
		safeItemsStart.filter('.cat'+randomCat).css('visibility','hidden');	
	}

	// Enlarge
	startCat.each(function(i){
		var temp =  $(this).attr('src');
		temp = temp.replace('bw_','');
		$(this).attr('src',temp);
		$(this).animate({'width':102, 'height':74}, 250);
	});
	
	// Sort
	colCount = 1;
	$('#images img.moved').removeClass('moved');
	vertAlign = self.setInterval('sortCols()',250);
		

	// preload color images (originally only for IE6)
	//if ($.browser.msie && $.browser.version < 7){
		var preload = new Array();
		allObj.filter('.small').each(function(i){
			preload[i] = new Image(0,0);
			preload[i].src = $(this).attr('src').replace('bw_','');
		});
	//}

	// Click actions
	$('#categories a').add('#images img').click(function(){
		// click block
		finished = 10;
		$('#categories a.selected').removeClass('selected');
		var thisCat = $(this).get(0).className;
		thisCat = thisCat.substr(0,4); // filter multipe class

		if (thisCat != current){
			allObj.css('visibility','visible');
			var safeItems = allObj.not('.'+current+', .'+thisCat);
			for (v=0; v<4; v++){
				var randomCat = Math.ceil(10*Math.random());
				safeItems.filter('.cat'+randomCat).css('visibility','hidden');	
			}
			$('body').prepend('<iframe id="iframe" name="iframe"></iframe>');
			rowReset(allObj.filter(':visible'), 0);

			allObj.filter('.'+current).each(function(){
				switchBW($(this), 'black');

				if($(this).nextAll().filter('.'+thisCat).get(0) !== undefined){
					$(this).nextAll().not($('.'+thisCat).nextAll()).animate({'left': '-=53'}, 250);
				}else{
					$(this).prevAll().andSelf().not($('.'+thisCat).prevAll().andSelf()).animate({'left': '+=53'}, 250);
				}
				
				$(this).removeClass('big').addClass('small').animate({'width': 49, 'height': 37}, 250);
			}).end().filter('.'+thisCat).each(function(){
				switchBW($(this), 'color');
				$(this).removeClass('small').addClass('big').animate({'width': 102, 'height': 74}, 250,function(){
					finished--;
					if (finished == 0){
						colCount = 1;
						vertAlign = self.setInterval('sortCols()',250);
					}
				});
			});
		}

		current = thisCat;
		$('#categories a.'+thisCat).addClass('selected');
	});
	
	$('#images img').hover(function(){
	   if ($(this).hasClass('small')){
		   $('#info:visible').hide();
			switchBW($(this), 'color');
			$(this).css('opacity',0.5).fadeTo('fast', 1);
		}else{
			$('#info:visible').hide();
			// open dialog delay
			$(this).addClass('opening').animate({'width': 102}, 500, function(){
				votePanel($(this));
			});
		}
	},function(){
		if ($(this).hasClass('small')){
			$(this).fadeTo('normal', 0.5,function(){
				$(this).css('opacity',1);
				switchBW($(this), 'black');
			});
		}else{
			$('#images img.opening').stop({clearQueue: true}).removeClass('opening');//.css('border', '1px solid red');
		}
	});
	
	$('#intro').add('#categories').add('#footer').mouseover(function(){
	   $('#info:visible').hide();
	});
}