// JavaScript Document

jQuery(function(){

	jQuery('img').bind("contextmenu",function(e){  
		return false;  
	});

    document.forms[0].action = window.location.href;

	// Make corner boxes
	var cornerableBoxes = jQuery(".roundedBox")
    for(var x = 0; x < cornerableBoxes.length; x++)
    {
	    rndNum = Math.random().toString();
	    rndNum = rndNum.replace(".","");
	    cornerMe(cornerableBoxes[x],rndNum);
    }
        
    // Side menu
	jQuery('#side > li > a').each(function(){		
	   
		jQuery(this)
		    .css("text-indent","-1000px");		    
		    if (jQuery(this).next().children().length > 0)
		    {
		        jQuery(this)
		            .toggle(function(){
			            jQuery(this).addClass("select");
			            jQuery(this).next()
				            .css("opacity",0)
				            .slideDown(300, function(){
					            jQuery(this).animate({ "opacity" : 1 }, 200)
				            });
		            }, function(){
			            jQuery(this).removeClass("select");
			            jQuery(this).next()
				            .animate({ "opacity" : 0 }, 200, function(){
						            jQuery(this).slideUp(300);
				            });
		            })
		            .next().hide().end();
		    }
			
			if (jQuery(this).attr("class") == "select")
			{
			    jQuery(this).triggerHandler("click");
			}
	});	
	
	// Next article show/hide
	jQuery('div#news-holder > div').hide();
        jQuery('div#news-holder > span > a').each(function(){
            jQuery(this).toggle(function(){        
                jQuery(this)
                    .text("hide")
                    .parent().next().slideDown(400);
            }, function(){
                jQuery(this)
                    .text("view")
                    .parent().next().slideUp(400);
            });        
        })
	
	// Remove validator inline style
    jQuery(".action-message", ".site-form").removeAttr("style").hide();
    
    // create a function for validator focus
    jQuery(".action-message", ".site-form").focus(function(){
        if (jQuery(this).attr("style") == "display: inline;")
            jQuery(this).removeAttr("style").show();
    });
    
    // Add event to restyle the validators when/if they show after the submit button is clicked
    jQuery("input:submit").bind("click", function(){
        jQuery(".action-message", ".site-form").each(function(){
            jQuery(this).triggerHandler("focus");
        })
    });
    
    // Add event handler for the inputs to restyle validators
    jQuery("input").bind("blur", function(){
        jQuery(".action-message", ".site-form").each(function(){
            jQuery(this).triggerHandler("focus");
        })
    });

});

function ReplaceDateFilter()
{
    $parent = jQuery('.orderby-select').attr("id", "fancyselect");
    
    orderby = window.location.href.split('/')
    orderby = orderby[orderby.length-1]

    count = 0;
    html = '<span class="selected">order by date...</span><ul>';    
    jQuery('select option', $parent).each(function(){    
        html += '<li id="val_' + jQuery(this).val() + '"'
            if (orderby == jQuery(this).val() || orderby == "projects" && count == 0) html += 'class="selected"'
        html += '>' + jQuery(this).text() + '</li>';
        count++;
    });
    html += '</ul>';    
    
    $parent.append(html);
    
    if (orderby == "asc" || orderby == "desc")
    {    
        jQuery('span.selected', $parent).text( jQuery('ul li[id=val_' + orderby + ']', $parent).text());
    }
    else
    {
        jQuery('span.selected', $parent).text('order by date...');
    }
    
    $parent.click(function(){
        jQuery('ul', this).slideToggle(200);
    })
    
    jQuery('ul li', $parent).each(function(){
        jQuery(this).click(function(){
            val = jQuery(this).attr("id").replace("val_", "");
            windowloc = "/projects/page/1/"
            window.location.href = windowloc + val;
        });
    });
}

function ChangeImage(img_src)
{
    $holder = jQuery('div.main-image');
    jQuery('div.overlay', $holder).fadeIn(430, function(){
        
        var NewImage = new Image();
        NewImage.onload = function(){
            jQuery('div.main-image div.overlay').fadeOut(800);
        }
        NewImage.src = img_src;
        
        jQuery('img', $holder).attr("src", img_src);        
    });
}
