blink = 0;
activated = false;
fade = 0;

msie = (navigator.appName=="Microsoft Internet Explorer") ? 1 : 0;

opacRange = (msie) ? 10 : 5;

requestAnimFrame = (function() {
  return window.requestAnimationFrame ||
     window.webkitRequestAnimationFrame ||
     window.mozRequestAnimationFrame ||
     window.oRequestAnimationFrame ||
     window.msRequestAnimationFrame ||
     function( callback, element ) {
       window.setTimeout(callback, 30);
     };
})();


function RemoveActiveState() {

	if (!msie) return;

	var elements = document.getElementsByTagName('a');

	for(var i=0;(elm = elements[i]);i++) {
		elm.attachEvent('onmouseup',function(){event.srcElement.blur();});
	}

}

function BlinkMenu() {

    menuItem = document.getElementById("chartMenu");
    if(blink%2)
        menuItem.style.backgroundColor = '#FB2C5E';
    else
        menuItem.style.backgroundColor = '';
        
    blink++;
    if(!activated) {
        mybl = setInterval('BlinkMenu()',133);
        activated = true;
    }
    
    if(blink > 10) {
        blink = 0;
        clearInterval(mybl);
        activated = false;
    }    
}

function AddToChart(item) {
    blink = 0;
    BlinkMenu();
    $removed = false;
    $thisButton = document.getElementById('item_'+item);
    $thisCookie = ReadCookie('DivinaChart');
    
    if(!$thisCookie) {
        CreateCookie('DivinaChart',item,1);
        $thisButton.innerHTML = "Remove from selection";
    }
    else {
        $arItems = $thisCookie.split('_');
        for($x=0;$x < $arItems.length;$x++) {
            if($arItems[$x] == item) {
                $arItems.splice($x,1);
                $thisCookie = $arItems.join('_');
                $removed = true;
                break;
            }
        }


        if(!$removed) {
             $newValue = $thisCookie+"_"+item;
             $thisButton.innerHTML = "Remove from selection";
        }
        else {
            $newValue = $thisCookie;
            $thisButton.innerHTML = "Add to selection";
        }
        CreateCookie('DivinaChart',$newValue,1);
        //alert($newValue);
    }

	$thisCookie = ReadCookie('DivinaChart');
	var $myItems = 0;
	
	if($thisCookie) { 
		$arItems = $thisCookie.split('_');
		$myItems = $arItems.length;
		
	}
	document.getElementById('chartMenu').innerHTML = 'your selection ('+$myItems+')';
		
	
}

function RemoveSelection(item) {
	
	elm = document.getElementById('row_'+item);
	elm.parentNode.removeChild(elm);
	
	$thisCookie = ReadCookie('DivinaChart');
	var $myItems = 0;
	
	if($thisCookie) { 
		$arItems = $thisCookie.split('_');
		$myItems = $arItems.length;
		
	}
	if($myItems == 0) location.reload();	

}

function CreateCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}


function ReadCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}


function EraseCookie(name) {
	CreateCookie(name,"",-1);
}


function ClearSelection() {

	var status = confirm('Do you really want to clear your selection?');
	if(!status) return;
	EraseCookie('DivinaChart');
	location.reload();

}


var currentPic = -1;

function SetImage(thumbObj, targetObj) {

	$('#thumbscroll').stop(true);
	
	var thisPic = parseInt( $(thumbObj).attr('id').replace('thumb_','') );
	if(thisPic == currentPic)
		return;

	currentPic = thisPic;
	
	$('#thumbscroll a').removeClass('thumb2On');
  	$(thumbObj).addClass('thumb2On');
	
	// Remove old image
	$('#picOver').remove();
	
	// Create new div for new image

	$(targetObj).append("<div id='picOver' class='bigpic'></div>");	

	if($(targetObj).css('background-image'))
		$('#picOver').css('background-image', $(targetObj).css('background-image') );

	
	var newImage = new Image();

	// new Image loaded event
	$(newImage).load(function(){
	
		var thisURL = newImage.src.replace('(','%28');
		thisURL = thisURL.replace(')','%29');
		
	 	$('#picCont').css('background-image','url('+thisURL+')');
		$('#picOver').fadeOut(500);

	});


	// Load new image 1384
	newImage.src = $(thumbObj).attr('href');
	

	var posX = $(thumbObj).position().left;

	ThumbsScroll( -(posX-173) );
	
/*

	var contWidth = $('#thumbscroll').width();
	
	if(posX > 0 && posX <= contWidth-173*2 ) {
		
		$('#thumbscroll').animate({ left: -(posX-173) }, 500, 'easeOutCubic');	

	}
	

	if(posX > 173 && contWidth > 173*3 ) {
	
		if( $('#thumbPrev').css('display') != 'block' ) {
			
			$('#thumbPrev').fadeIn(500);
		}
		
	} else {
	
		
		$('#thumbPrev').fadeOut(500);
	
	}
		
	
	if(posX < contWidth-173*2 && contWidth > 173*3) {
	
		if( $('#thumbNext').css('display') != 'block' ) {

			$('#thumbNext').fadeIn(500);
		}
	} else {
		
		$('#thumbNext').fadeOut(500);
	}
*/
}

var deltaX1 = 0, deltaX2 = 0;
var deltaTime = 0;

function ThumbsAnimate(event) {

	var myObj = $('#thumbscroll');

	myObj.stop(true);
	
	var thisDelta = (deltaX1-deltaX2)/deltaTime;		
	
	thisDelta = Math.round(thisDelta)*173*2;

	var newPosX = parseInt( myObj.css('left') );
	
	newPosX = newPosX-thisDelta;

	ThumbsScroll(newPosX, deltaTime*10);
	
	
}

function ThumbsScroll(deltaX, deltaTime) {

	var scrollObj = $('#thumbscroll');
	var viewWidth = 173*3;
	var contWidth = scrollObj.width();
	var currentX = -(scrollObj.position().left);
	
	var deltaT = (deltaTime) ? deltaTime : 500;

	if(	deltaX > 0 )
		deltaX = 0;
	if( deltaX < -contWidth+viewWidth )	
		deltaX = -contWidth+viewWidth;
		
	scrollObj.animate({ left: deltaX }, deltaT, 'easeOutCubic');


	if( -deltaX > 0 && contWidth > viewWidth ) 
		$('#thumbPrev').show();
	else 
		$('#thumbPrev').hide();

	if( -deltaX < contWidth-viewWidth && contWidth > viewWidth ) 
		$('#thumbNext').show();
	else 
		$('#thumbNext').hide();

	
}

function ThumbsNext() {

	var currentX = ( $('#thumbscroll').position().left );
	ThumbsScroll(currentX-173*3, 500);
}

function ThumbsPrev() {

	var currentX = ( $('#thumbscroll').position().left );
	ThumbsScroll(currentX+173*3, 500);

}
