$(document).ready(function(){
	
	$('.dropdown').click(function(){
		$(this).parent().children('ul').toggle(0);	
	});
	
	$('.custom_dropdown ul li a').click(function(){
		$(this).parent().parent().parent('.custom_dropdown').children('.dropdown').html($(this).html());
		$(this).parent().parent().parent('.custom_dropdown').children('input').val($(this).html());
		$(this).parent().parent().parent('.custom_dropdown').children('ul').hide();
		return false;
	});

        //if previously submitted the form - select the appropriate option
        //get all custom dropdowns
        $('.custom_dropdown').each(function(){
            // $(this) is the dropdown - if input has value - we have to select the corresponding option
            if($(this).children('input').val()){
                //find the anchor
                $(this).children('ul').children().each(function(){
                    // $(this) the li under the ul
                    if($(this).children('a').html() == $(this).parent().parent('.custom_dropdown').children('input').val()){
                        //if we find it - simulate clicking
                        $(this).parent().parent('.custom_dropdown').children('.dropdown').html($(this).children('a').html());
                        $(this).parent().parent('.custom_dropdown').children('input').val($(this).children('a').attr('href'));
                        $(this).parent().parent('.custom_dropdown').children('ul').hide();
                    }
                });
            }
        });

	$('#request ul li a').click(function(){
		$(this).parent().parent().parent().parent().parent('.custom_dropdown').children('.dropdown').html($(this).html());
		$(this).parent().parent().parent().parent().parent('.custom_dropdown').children('input').val($(this).attr('href'));
		$(this).parent().parent().parent().parent().parent('.custom_dropdown').children('ul').hide();
		return false;
	});

        //if previously submitted the form - select the appropriate option
        //logic is the same as with the other custom dropdowns
        if($('#request').children('input').val()){
            $('#request ul').children().each(function(){
                if($(this).children('a').html() == $('#request').children('input').val()){
                    $(this).parent().parent().parent().parent('.custom_dropdown').children('.dropdown').html($(this).children('a').html());
                    $(this).parent().parent().parent().parent('.custom_dropdown').children('input').val($(this).children('a').attr('href'));
                    $(this).parent().parent().parent().parent('.custom_dropdown').children('ul').hide();
                }
            });
        }

	$('#rcompany').click(function(){
		$('#company').css('display','block');
		$('#individual').css('display','none');
	});
	
	$('#rindividual').click(function(){
		$('#company').css('display','none');
		$('#individual').css('display','block');
	});
	
	$('input[type=text], input[type=password], textarea').each(function(){			
	    var d_value = this.value;			    
	    $(this).focus(function(){
	        if(this.value == d_value){
	            this.value = '';
	        }
	    });
			    
	    $(this).blur(function(){
	    	if(this.value == ''){
	            this.value = d_value;
	        }
	    });		    
	});
	
	$('ul.tabs_projects').tabs('div.panes > div');
	
	$('.scrollable').scrollable({speed: 800});
	
	$('#projects .items div a').each(function(){
		$(this).mouseover(function(){
			$(this).children('ins').stop().animate({'height' : '50'}, 200);
		});

		$(this).mouseout(function(){
			$(this).children('ins').stop().animate({'height' : '16'}, 150);
		});
	});

    $('a.lang_en').click(function(){
    	$('form.lang_en').submit();
	});

	$('a.lang_bg').click(function(){
    	$('form.lang_bg').submit();
	});

    loadCaptcha('captchaImg');

    $('a#captchaLink').click(function(){
    	reloadCaptcha();
	});

	$('ul.projects_type li, ul#services_list li h2, ul.tabs_projects li').click(function(){
    	load_metainfo(CI.base_url + 'metainfo/' + $(this).attr('class'));
	}); 

});

function loadCaptcha(image_id){
     x = document.getElementById(image_id);
     x.src = CI.base_url+"captcha#" + Math.random();
}

function addEvent(obj, evType, fn){ 
	if (obj.addEventListener){ 
		obj.addEventListener(evType, fn, true); 
		return true; 
	}else if (obj.attachEvent){ 
		var r = obj.attachEvent("on"+evType, fn); 
		return r; 
	}else{ 
		return false; 
	} 
}

function hideFocusBorders(){
var theahrefs = document.getElementsByTagName("a");
	if (!theahrefs){return;}
		for(var x=0;x!=theahrefs.length;x++){
		theahrefs[x].onfocus = function stopLinkFocus(){this.hideFocus=true;};
	}
}

addEvent(window, 'load', hideFocusBorders);


function load_metainfo(url){
    var req = null;
    if (window.XMLHttpRequest) {
       req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
       req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    req.open("GET", url, true);
    req.onreadystatechange = callback( req );
    req.send(null);
}
function callback( req ){
    return function() {
        if (req.readyState == 4) {
            if (req.status == 200) {
                var metainfo = req.responseXML.documentElement;
                $('title').html(metainfo.getElementsByTagName('title')[0].childNodes[0].nodeValue);
                $('meta[name="description"]').attr('content',metainfo.getElementsByTagName('description')[0].childNodes[0].nodeValue);
                $('meta[name="keywords"]').attr('content',metainfo.getElementsByTagName('keywords')[0].childNodes[0].nodeValue);
            }
        }
    }
}
