
var img_main;
var selection;
var img_cover;

var thumb           = new Object();

var mouse           = new Object();
    mouse.xPos = 0;
    mouse.yPos = 0;
    mouse.isInSel = false;
    mouse.isInImg = false;

function initPage()
{
	img_main = document.getElementById('img_main');
	img_cover = document.getElementById('img_cover');
	selection = document.getElementById('selection');
	heightField = document.getElementById('thumbHeight');
	widthField = document.getElementById('thumbWidth');

    selection.style.cursor = 'move';
    selection.style.top    = 0;
    selection.style.left   = 0;
    selection.style.width  = 5;
    selection.style.height = 5;

    thumb.height    = heightField.value;
    thumb.width     = widthField.value;
    thumb.prop      = thumb.height/thumb.width;

    // Setup events
    document.onselectstart = selectStart;
    document.onmouseup     = mouseUp;
    window.onresize        = handleResize;

}

function changeHeight() {
	thumb.height = heightField.value;
    thumb.prop = thumb.height/thumb.width;
	resetSel();
}

function changeWidth() {
	thumb.width = widthField.value;
    thumb.prop      = thumb.height/thumb.width;
	resetSel();
}


function resetSel() {

    selection.style.top    = 0;
    selection.style.left   = 0;
    selection.style.width  = 5;
    selection.style.height = 5;

}

function selectStart(e)
{
    if( event )
    {
        event.cancelBubble = true;
    }

    return false;
}


function imageLoaded()
{
    img_main = document.getElementById('img_main');
    img_main.top  = getTop(img_main);
    img_main.left = getLeft(img_main);

	img_cover = document.getElementById('img_cover');
    img_cover.style.top    = img_main.top;
    img_cover.style.left   = img_main.left;
    img_cover.style.width  = img_main.width;
    img_cover.style.height = img_main.height;
}



function getLeft(obj)
{
	var curleft = 0;

    while(obj.offsetParent)
    {
        curleft += obj.offsetLeft;
        obj = obj.offsetParent;
    }

	return curleft;
}


function getTop(obj)
{
	var curtop = 0;

    while (obj.offsetParent)
    {
        curtop += obj.offsetTop;
        obj = obj.offsetParent;
    }

	return curtop;
}

function fillForm()
{
    document.thumbForm.selX.value      = parseInt(selection.style.left) - img_main.left;
    document.thumbForm.selY.value      = parseInt(selection.style.top) - img_main.top;
    document.thumbForm.selWidth.value  = parseInt(selection.style.width);
    document.thumbForm.selHeight.value = parseInt(selection.style.height);

    return true;
}



function moveSel(x, y)
{
    if( x < img_main.left ) x = img_main.left;
    else if( x + parseInt(selection.style.width) - img_main.left > img_main.width )
        x = img_main.width - parseInt(selection.style.width) + img_main.left;

    if( y < img_main.top ) y = img_main.top;
    else if( y + parseInt(selection.style.height) - img_main.top > img_main.height )
        y = img_main.height - parseInt(selection.style.height) + img_main.top;

    selection.style.left = parseInt(x);
    selection.style.top  = parseInt(y);
}



function resizeSel(width, height)
{
    if( height < 5 || width < 5 ) return;

    if (height/width > thumb.prop) width = (height/thumb.prop);
    else height = (thumb.prop * width);

    if( (parseInt(selection.style.top) - img_main.top) + height <= img_main.height && (parseInt(selection.style.left) - img_main.left) + width <= img_main.width )
    {
        selection.style.height   = height;
        selection.style.width    = width;
    }
}



function handleResize()
{
	img_main.top  = getTop(img_main);
	img_main.left = getLeft(img_main);

	img_cover.style.top    = img_main.top;
	img_cover.style.left   = img_main.left;
	img_cover.style.width  = img_main.width;
	img_cover.style.height = img_main.height;
}



function mouseUp()
{
    mouse.isInImg = false;
    mouse.isInSel = false;

    selection.style.cursor = 'move';
}



function mouseDownImg(e)
{
    if( !img_main.complete )
    {
        alert('Please wait for the image to load completely!');
        return;
    }


    if( !mouse.isInImg )
    {
        handleResize();

        mouse.isInImg = true;
        mouse.xPos = e.pageX ? e.pageX : e.offsetX + img_main.left;
        mouse.yPos = e.pageY ? e.pageY : e.offsetY + img_main.top;
      
        resizeSel(5, 5);
        moveSel(mouse.xPos, mouse.yPos);

        selection.style.visibility = 'visible';
        selection.style.cursor     = 'default';
    }
}



function mouseMoveImg(e)
{
    if( mouse.isInImg )
    {
        var new_x = e.pageX ? e.pageX : e.offsetX + img_main.left;
        var new_y = e.pageY ? e.pageY : e.offsetY + img_main.top;

        if( new_x > mouse.xPos && new_y > mouse.yPos )
        {
            resizeSel(new_x - mouse.xPos, new_y - mouse.yPos);
        }
    }
}



function mouseDownSel(e)
{
	mouse.isInSel = true;

	mouse.xPos = e.pageX ? e.pageX : e.x;
	mouse.yPos = e.pageY ? e.pageY : e.y;

	mouse.xPos -= parseInt(selection.style.left);
	mouse.yPos -= parseInt(selection.style.top);
}



function mouseMoveSel(e)
{
    if( mouse.isInImg )
    {
        img_cover.fireEvent('onmousemove', event);
		// mouseMoveImg(e);
    }
    else if( mouse.isInSel )
    {
        var destX = e.pageX ? e.pageX : e.x;
        var destY = e.pageY ? e.pageY : e.y;

        destX -= mouse.xPos;
        destY -= mouse.yPos;

        moveSel(destX, destY);
    }
}
function validateSearch(b_on_submit)
{
var ln=document.searchForm;
var errors='';
if (ln.search.value == "")
   {
     errors +="\n-Nu ai introdus nici un text pentru cautare ";
   }
if (ln.search.value.length<4 && ln.search.value!="" )
   {
     errors +="\n-Textul cautat trebuie sa aiba minim 4 caractere ";
   }
if (errors=='')
  {
    ln.submit(); 
  }
else 
  {
	 alert("Eroare la Cautare:" + errors);
     return false;
  }   
   
	
}

if (document.images){
  pic1 = new Image(220,19); 
  pic1.src = "images/rating_loading.gif"; 

  pic2 = new Image(25,75); 
  pic2.src = "images/rating_star.gif"; 

  pic3 = new Image(25,75); 
  pic3.src = "images/rating_star_2.gif"; 
  
  pic4 = new Image(16,13); 
  pic4.src = "images/rating_tick.gif";
  
  pic5 = new Image(14,14); 
  pic5.src = "images/rating_warning.gif";
}

// AJAX ----------------------------------------

var xmlHttp

function GetXmlHttpObject(){

var xmlHttp = null;

	try {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp = new XMLHttpRequest();
	  }
	catch (e) {
	  // Internet Explorer
	  try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
	  catch (e){
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	  }
	  
	return xmlHttp;

}

// Calculate the rating
function rate(rating,id,show5,showPerc,showVotes){

	xmlHttp = GetXmlHttpObject()
	
	if(xmlHttp == null){
		alert ("Your browser does not support AJAX!");
		return;
	  }

	xmlHttp.onreadystatechange = function(){
		
	var loader = document.getElementById('loading_'+id);
	var uldiv = document.getElementById('ul_'+id);
	
		if (xmlHttp.readyState == 4){ 
			
			//loader.style.display = 'none';
			var res = xmlHttp.responseText;
			
			//alert(res);
			
			if(res == 'already_voted'){
				
				loader.style.display = 'block';
				loader.innerHTML = '<div class="voted_twice">Ai votat Deja!</div>';
				
			} else {
				
				loader.style.display = 'block';
				loader.innerHTML = '<div class="voted">Iti multumim Pentru vot!</div>';
				if(show5 == true){
					var out = document.getElementById('outOfFive_'+id);
					var calculate = res/20;
					out.innerHTML = Math.round(calculate*100)/100; // 3.47;
					//out.innerHTML = Math.round((calculate*2),0)/2; // 3.5;
				} 
				
				if(showPerc == true){
					var perc = document.getElementById('percentage_'+id);
					//var newPerc = Math.round(Math.ceil(res/5))*5;
					var newPerc = res;
					perc.innerHTML = newPerc+'%';
				}
				
				else if(showPerc == false){
					var newPerc = res;
				}
				
				if(showVotes == true){
					var votediv = document.getElementById('showvotes_'+id).firstChild.nodeValue;
					var splitted = votediv.split(' ');
					var newval = parseInt(splitted[0]) + 1;
					if(newval == 1){
						document.getElementById('showvotes_'+id).innerHTML = newval+' Vot';
					} else {
						document.getElementById('showvotes_'+id).innerHTML = newval+' Voturi';
					}
				}
				
				var ulRater = document.getElementById('rater_'+id);
				ulRater.className = 'star-rating2';
				
				var all_li = ulRater.getElementsByTagName('li');
				
				// start at 1 because the first li isn't a star
				for(var i=1;i<all_li.length;i++){
					
					all_li[i].getElementsByTagName('a')[0].onclick = 'return false;';
					all_li[i].getElementsByTagName('a')[0].setAttribute('href','#');
					
				}
				
				if(navigator.appName == 'Microsoft Internet Explorer'){
					uldiv.style.setAttribute('width',newPerc+'%'); // IE
				 } else {
					uldiv.setAttribute('style','width:'+newPerc+'%'); // Everyone else
				 }
				
			}
		} else {
			loader.innerHTML = '<img src="images/rating_loading.gif" alt="loading" />';	
		}
	
	}
	var url = "includes/rating.php";
	var params = "id="+id+"&rating="+rating;
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);

} 
