var gradientshadow={}
gradientshadow.depth=6 //Depth of shadow in pixels
gradientshadow.containers=[]

gradientshadow.create=function(){
var a = document.all ? document.all : document.getElementsByTagName('*')
for (var i = 0;i < a.length;i++) {
    if (a[i].className == "shadow") {
        for (var x=0; x<gradientshadow.depth; x++){
            var newSd = document.createElement("DIV")
            newSd.className = "shadow_inner"
            newSd.id="shadow"+gradientshadow.containers.length+"_"+x //Each shadow DIV has an id of "shadowL_X" (L=index of target element, X=index of shadow (depth) 
            if (a[i].getAttribute("rel"))
                newSd.style.background = a[i].getAttribute("rel")
            else
                newSd.style.background = "black" //default shadow color if none specified
            document.body.appendChild(newSd)
        }
    gradientshadow.containers[gradientshadow.containers.length]=a[i]
    }
}
gradientshadow.position()
window.onresize=function(){
    gradientshadow.position()
}
}

gradientshadow.position=function(){
if (gradientshadow.containers.length>0){
    for (var i=0; i<gradientshadow.containers.length; i++){
        for (var x=0; x<gradientshadow.depth; x++){
          var shadowdiv=document.getElementById("shadow"+i+"_"+x)
            shadowdiv.style.width = gradientshadow.containers[i].offsetWidth + "px"
            shadowdiv.style.height = gradientshadow.containers[i].offsetHeight + "px"
            shadowdiv.style.left = gradientshadow.containers[i].offsetLeft + x + "px"
            shadowdiv.style.top = gradientshadow.containers[i].offsetTop + x + "px"
        }
    }
}
}

if (window.addEventListener)
window.addEventListener("load", gradientshadow.create, false)
else if (window.attachEvent)
window.attachEvent("onload", gradientshadow.create)
else if (document.getElementById)
window.onload=gradientshadow.create

// To show the selected image
function showImage(imageUrl,width,height) {
    jQuery("#mainImage").hide();
    pad = Math.ceil((270-height)/2);
    jQuery("#mainImage").html('<img src="'+imageUrl+'" width="'+width+'" height="'+height+'" style="padding-top:'+pad+'px;"/>');
    jQuery("#mainImage").fadeIn("slow");
}

// To highlight the rating stars
function selectImage(imageNo) {
    imageId = 'star'+imageNo;
    jQuery("#"+imageId).attr("src",'../images/contest/stars/star'+imageNo+'-select.gif');    
}

// To remove highlighting of the rating stars
function deselectImage(imageNo) {
    imageId = 'star'+imageNo;
    jQuery("#"+imageId).attr("src",'../images/contest/stars/star'+imageNo+'.gif');    
}


function openDiv(i,kid)
{
	 	jQuery.blockUI({ message: jQuery('#voteKitchenForm'), css: { width: '425px', height: '225px' } }); 
		// jQuery('#voteKitchenForm').show(); 
	 	if(document.getElementById('ratingMsg').innerHTML!='')
		{
			jQuery("#errorSpan").show(); 
				jQuery("#errorSpan").html("<span style='color:#BB0000; font-size: 12px; font-weight:bold;'>"+document.getElementById('ratingMsg').innerHTML+"</span>");
				
				jQuery('#yes').click(function() { 
				jQuery("#errorSpan").show(); 
				jQuery("#errorSpan").html("<span style='color:#BB0000; font-size: 12px; font-weight:bold;'>You already rated this kitchen.</span>");
				jQuery.unblockUI(); 			
				 return false; 
			  });					  
		}
		else
		{
		
		jQuery('#yes').click(function() { 
        
		var emailID = document.getElementById('emailVal');
        var email = document.getElementById('emailVal').value;
		var captcha = document.getElementById('captchaVal').value;
			if(email == "") {
				jQuery("#errorSpan").show(); 
				jQuery("#errorSpan").html("<span style='color:#BB0000; font-size: 12px; font-weight:bold;'>Please enter valid email address.</span>");
			} 
			else if(IsEmail(emailID,'Please enter valid email address')==false)
			{
					 jQuery("#errorSpan").show(); 
				jQuery("#errorSpan").html("<span style='color:#BB0000; font-size: 12px; font-weight:bold;'>Please enter valid email address.</span>");
			}
			else if(captcha=="")
			{
					 jQuery("#errorSpan").show(); 
				jQuery("#errorSpan").html("<span style='color:#BB0000; font-size: 12px; font-weight:bold;'>Please enter code.</span>");
			}
			else 
			{
				//add code here to rate
				saveRating(i,kid,email,captcha);
			}
        });
	}
		
	 	jQuery('#no').click(function() { 
        jQuery("#errorSpan").hide(); 
        jQuery("#giftAmountVal").val(''); 
        jQuery.unblockUI(); 
        return false; 
        }); 
	
}


// To save the user rating
function saveRating(rating,kitchenId,email,captcha) {    
    jQuery.ajax({
        url: 'saveRating.php',
        type: 'POST', 
        cache: false,
        dataType: 'text',
        data: { rating: rating, kitchenId: kitchenId, email: email, captcha: captcha},
        error: function(){
            alert('Error loading XML document');
        },
        success: function(html){
            if(!isNaN(html) && html > 0) {
                selectImage(rating);
                imageId = 'star'+rating;        
                jQuery("#"+imageId).removeAttr("onmouseout");
                jQuery("#ratingMsg").html("You awarded "+rating+" Ugly Points to this kitchen.");
                noOfVotes = eval(jQuery("#voteSpan").html())+1;
                points = eval(jQuery("#pointSpan").html())+rating;
                rank = html;
                jQuery("#voteSpan").html(noOfVotes);
                jQuery("#rankSpan").html(rank);
                jQuery("#pointSpan").html(points);
                jQuery("#spacer").remove();
                jQuery("#totalNo").show();
                jQuery("#rankLabel").html('Total Rank: ');
				
				jQuery("#errorSpan").hide(); 
		        jQuery("#giftAmountVal").val(''); 
        		jQuery.unblockUI(); 
				
            } else {
               // alert(html);    
			    jQuery("#errorSpan").show(); 
				jQuery("#errorSpan").html("<span style='color:#BB0000; font-size: 12px; font-weight:bold;'>"+html+"</span>");
            }
        }
     });
}

// To post a comment
function postComment(kitchenId) {
    var comment = document.getElementById("comment");
    var name = document.getElementById("name");
    var email = document.getElementById("email");    
    if(Trim(comment.value) == "") {
        alert("Please enter your comment");
        comment.focus();
    } else if(Trim(name.value) == "") {
        alert("Please enter your name");
        name.focus();
    } else if(!IsValidName(name,"Please enter a valid name")) {
        name.focus();
    } else if(Trim(email.value) == "") {
        alert("Please enter your email");
        email.focus();
    } else if(!IsEmail(email,"Please eneter a valid email")) {
        void(0);
    } else {
        jQuery("#postBtn").attr("value"," Saving... ");
        jQuery("#postBtn").attr("disabled","disabled");
        jQuery.ajax({
            url: 'saveComment.php',
            type: 'POST', 
            cache: false,
            dataType: 'text',
            data: { comment: comment.value, name: name.value, email: email.value, kitchenId: kitchenId},
            error: function(){
                alert('Error loading XML document');
            },
            success: function(html){
                if(html != "Error") {
                    comment.value = '';
                    name.value = '';
                    email.value = '';
                    jQuery('#noComment').hide();
                    jQuery("#postBtn").attr("disabled","");
                    jQuery("#postBtn").attr("value"," Post Comment ");
                    jQuery('#commentDiv').append(html);
                } else {
    
                }
            }
         });        
    }
}


