Event.observe(window, 'load', init, false);

//MAIN FUNCTION
function init(){
	//focusForm_client();
	if($('header_dev')) {
		var animation_index = 1;
		var periodical = null;
		var delay = null;
		var items = $$('#header_dev > div');
		var animations = [];
		var animate_overlay = function() {
			new Effect.Tween($('header_dev_overlay'), 100, 0, { duration : 1 }, function(value) {
				this.setStyle({
					opacity : value / 100
				});
				if(value == 0){
					this.setStyle({
					 	display : 'none'
					});
					if(delay) window.clearTimeout(delay);
					periodical = new PeriodicalExecuter(function(pe) {
						if(animation_index >= items.length) animation_index = 0;
						animate(items[animation_index++]);
					}, 5);
				}
			});
		}
		$('header_dev_overlay_close').observe('click', function() {
			animate_overlay();
		});
		delay = animate_overlay.delay(10);
		var animate = function(item) {
			animations.each(function(animation) {
				animation.cancel();
			});
			animations = [];
			items.each(function(item) {
			    var layout = new Element.Layout(item);
			    if(layout.get('width') != 48) animations[animations.length] = new Effect.Tween(item, layout.get('width'), 48, function(value) {
			    	this.setStyle({
			    		width : value + 'px'
			    	});
			    });
			});
			var layout = new Element.Layout(item);
			animations[animations.length] = new Effect.Tween(item, layout.get('width'), (Prototype.Browser.IE ? 800 : 797), function(value) {
			    $(this).setStyle({
			    	width : value + 'px'
			    });
			});     
		}
		animate(items[0]);
		items.each(function(item) {
			item.observe('click', function() {
				animate(item);
				if(periodical) {
					periodical.stop();
					periodical = null;
				}		
			});
		});
	}
}

//LOGIN MENU FUNCTION
var login_menu_var	= false;

function login_menu(element, content, link, input){
	
	if(login_menu_var == false){
	
		new Effect.BlindDown(element, { duration: 2.5, mode: 'relative', transition: Effect.Transitions.spring, queue: 'front' });
		new Effect.Appear(content, { duration: 1.5, queue: 'end' });
		$(link).innerHTML = 'close';
		//$(input).focus();
		//focusForm_client();
		login_menu_var	= true;
		
	}else{
	
		new Effect.Fade(content, { duration: 0.5, queue: 'front' });
		new Effect.BlindUp(element, { duration: 1, queue: 'end' });
		$(link).innerHTML = 'login';
		login_menu_var	= false;
	}
	
}

var scroller = function() {
    var currentPosition = 0;
    var lock;

	//effect 1
    var sinoidal = function(val) {
      return (-Math.cos(val * Math.PI) / 2) + 0.5;
    }
    
    //effect 2
    var spring = function(val) {
      //return 1 - (Math.cos(val * 4.5 * Math.PI) * Math.exp(-val * 6));
      return 1 - (Math.cos(val * 2.5 * Math.PI) * Math.exp(-val * 10));
    }

    var scroll = function(index) {
        var totalOffset = index * scroller.scrollPanelWidth;
        var desiredOffset = totalOffset - currentPosition;
        var scrollee = $(scroller.scrollPanel);
        var tick = scroller.scrollTime / scroller.ticks;
        var timeIndex = 0;
        var inter;

        function animate() {
            timeIndex += tick;
            //change effect here
            var delta = sinoidal(timeIndex / scroller.scrollTime) * desiredOffset;
            if (timeIndex > scroller.scrollTime) {
                clearInterval(inter);
                currentPosition = scrollee.scrollLeft;
                lock = false;
                return;
            } 
            scrollee.scrollLeft = currentPosition + delta;
        }
        
        inter = setInterval(function() { animate(); }, tick);
    }

    var getScrollLinks = function() {
        var links = $$('a[rel=scroll]');
        links.each(function(elem, index) {
            elem.observe('click', function() {
                if (lock) {
                    return;
                }
                lock = true;    
	                scroll(index);   
            });
        });
    };
    
    return { 
        scrollTime: 0,
        ticks: 0,
        scrollPanel: '',
        scrollPanelWidth: 0,
    
        init: function(panel, duration, frames, panelWidth) {
            scroller.scrollPanel = panel;
            scroller.scrollTime = duration || 1500;
            scroller.ticks = frames || 60;
            scroller.scrollPanelWidth = panelWidth || 590;
            getScrollLinks();
        }
    };
}();


function linkActive(id, elname){
	if(elname == undefined){
		elname = 'link';
	}
	var number = 20;
	for(var i=0; i<=number; i++){
		if(id == elname+i){ 
			$(elname+i).addClassName('active_link'); 	
		}else{
			$(elname+i).removeClassName('active_link'); 
		}
	}
}

/*function showcase_expand(el){
	new Effect.Morph(el, {  style: { backgroundColor:'#E8E8E8', height:'105px', color:'#666' }, duration: 0.8});
}

function showcase_min(el){
	new Effect.Morph(el, { style: { backgroundColor:'#3A4275', height:'82px', color:'#FFF' }, duration: 0.8});
}*/

function clientRoll(el, par){
	
		var img = {
		
					'desat' : 'icon_'+el+'_desat.jpg',
					'normal': 'icon_'+el+'.jpg'
		}
		
		function image_normal(){ 
			var new_image	= '/media/imgs/clients/'+img.normal;
			$(el).src=new_image;
			new Effect.Opacity(el, { from: 0, to: 1.0, duration: 0.5 });
			
		}
		
		function image_desat(){ 
			var old_image	= '/media/imgs/clients/'+img.normal;
			$(el).src=old_image;
			new Effect.Opacity(el, { from: 1.0, to: 0, duration: 0.5 });
			
		}
	
		if(par == 'normal'){
		
			image_normal();
			
		}else if(par == 'desat'){

			image_desat();
			
		}
	
}

function view_changer(mode, el, btn){
	
	
	//ICON VIEW
	if(mode == 'icon'){    
	
		//LOOP FOR VIEW NAVIGATION
		for(var i = 1; i<=3; i++){
			
			if(btn == "btn"+i){
				$(btn).addClassName('active');
			}else{
				$("btn"+i).removeClassName('active');
			}
			
		}
		
		//CHANGE CLASSES
		var elements = $$('div.element_view');
		elements.each(function(elem){
			elem.addClassName('icon_view');
			elem.removeClassName('icon_list_view');
			elem.removeClassName('list_view');
			elem.setStyle({ clear: 'none' });	            
		});
		
		$(el).setStyle({ overflow: 'hidden', width: '10000px', height:'auto' });
		var panels = $$('div.panel');
		panels.each(function(pan){	
     	   pan.setStyle({ clear: 'none'});           
		});
		
		$('panel_links').show();
	
	//ICON LIST VIEW
	}else if(mode == 'icon_list'){   
	        
        //LOOP FOR VIEW NAVIGATION  
		for(var i = 1; i<=3; i++){
	
			if(btn == "btn"+i){
				$(btn).addClassName('active');
			}else{
				$("btn"+i).removeClassName('active');
			}
			
		}
		
		//CHANGE CLASSES
		var elements = $$('div.element_view');
		elements.each(function(elem){
			elem.removeClassName('icon_view');
     	   elem.addClassName('icon_list_view');
     	   elem.removeClassName('list_view');	
     	   elem.setStyle({ clear: 'both' });
           
		});   
		
		$(el).setStyle({ width: '900px', height: '400px', overflowY: 'scroll', overflowX: 'hidden' });    
		var panels = $$('div.panel');
		panels.each(function(pan){	
     	   pan.setStyle({ clear: 'both'});           
		});
		
		$('panel_links').hide();
	
	//LIST VIEW
	}else if(mode == 'list'){       
        
        //LOOP FOR VIEW NAVIGATION    
		for(var i = 1; i<=3; i++){
	
			if(btn == "btn"+i){
				$(btn).addClassName('active');
			}else{
				$("btn"+i).removeClassName('active');
			}
			
		}	
		
		//CHANGE CLASSES
		var elements = $$('div.element_view');
		elements.each(function(elem){
			  elem.removeClassName('icon_view');
  		      elem.removeClassName('icon_list_view');
  		      elem.addClassName('list_view');	
  		      elem.setStyle({ clear: 'both' });            
		});      
		
		$(el).setStyle({ marginTop: '5px', width: '900px', height: '400px', overflowY: 'scroll', overflowX: 'hidden' }); 
		var panels = $$('div.panel');
		panels.each(function(pan){	
     	   pan.setStyle({ clear: 'both'});           
		});   		     
	
		$('panel_links').hide();
		
	}
		
}	

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
}

	
function enewsform(el,e, msg){
	
	//check form is blank
	if(($F(e)==null) || ($F(e)=="")){
		alert("Please Enter your Email");
		$(e).focus();
		return false;
	}
	
	//validate email
	if (echeck($F(e))==false){
		$(e).focus();
		return false;
	}
	
	var url = '/server/request/enewsform.php';
	var par = 'email='+escape($F(e));
	var complete = false;
	var req = new Ajax.Request(url, { method: 'post', postBody: par, onComplete: complete = true });
	
	//process email
	if(complete==true){
		Effect.BlindUp(el, { queue: 'front' });
		Effect.Appear(msg, { queue: 'end' });
	}
	
}



