var a_elements=new Array();
var a_counter=5;

$(document).ready(function() {
 
	$(window).resize(function() {
	 realign_style_elements();
	});
	
	realign_style_elements();
	
	
   if ($(".page_gallery").length!=0)
	{
	$(".page_gallery").css("display","block");
	$(".page_gallery").jCarouselLite({
        btnNext: ".page_gallery_right",
        btnPrev: ".page_gallery_left",
		visible: 4,
		circular:false,
		scroll:3
	 });
	}
	
	
	//default value field
	$("[ftype=def_value]").each( function(index) {
		$(this).bind("focusin", function() {
			clear_field($(this).get(0));
		});
		
		$(this).bind("focusout", function() {
			restore_field($(this).get(0));
		});		
	});
	
	//slideshow
	var a_num=0;
	if ($(".slide_container").length!=0){
		//prepare slides
		$("[type=slideshow_slide]").each( function(index) {
			
			//set default
			if (a_num==0)
				{
				$(".slider").css("background-image","url('"+$(this).attr("image")+"')");
				slider_active_slide($(this).get(0));
				}
			a_num++;
			
			$(this).bind("mouseover", function() {
				if ($(this).attr("active")!="1")
					{
					$(this).attr("class","slideshow_item_hover");
					}
			});
			
			$(this).bind("mouseout", function() {
				if ($(this).attr("active")!="1")
					{
					$(this).attr("class","slideshow_item");
					}
			});
			
			$(this).bind("click", function() {
				if ($(this).attr("active")!="1")
					{
					slider_active_slide($(this).get(0));
					}
			});
		});
		
			
		//slide prev/next buttons
		$("#next_slide").bind("click",function(){$(this).attr("class","slide_nav_arrow_right_active"); switch_slide("next");	});
		$("#next_slide").bind("mouseover", function() { $(this).attr("class","slide_nav_arrow_right_hover");});
		$("#next_slide").bind("mouseout", function() { $(this).attr("class","slide_nav_arrow_right");});
			
		$("#prev_slide").bind("click",function(){$(this).attr("class","slide_nav_arrow_left_active"); switch_slide("prev");	});
		$("#prev_slide").bind("mouseover", function() { $(this).attr("class","slide_nav_arrow_left_hover");});
		$("#prev_slide").bind("mouseout", function() { $(this).attr("class","slide_nav_arrow_left");});		
		
		setTimeout("change_slide()",1000);
	}
	
});




function slider_active_slide(element)
{
a_counter=5;
$("[type=slideshow_slide]").each( function(index) {
	$(this).attr("active","0");
	$(this).attr("class","slideshow_item");
});
$(element).attr("active","1");
$(element).attr("class","slideshow_item_active");

$(".slider").animate({
	opacity: 0.3
	},700, function (){
	$("#slide_info").css("display","none");
	$("#slide_title").html($(element).attr("title"));
	$("#slide_body").html($(element).attr("body"));
	if ($(element).attr("title")!="" || $(element).attr("body")!="") $("#slide_info").css("display","block");
	$(".slider").css("background-image","url('"+$(element).attr("image")+"')");
	$(".slider").animate({opacity: 1},700,function () {});
});

a_counter=5;
}

function switch_slide(direction)
{
var a_active,a_total;
a_active=0; a_total=0;
$("[type=slideshow_slide]").each( function(index) {
	if ($(this).attr("active")=="1") a_active=index; 
	a_total=index;
});

if (direction=="next")
	{
	if (a_active>=a_total) 
		{
		slider_active_slide($("[type=slideshow_slide]").get(0));
		}else slider_active_slide($("[type=slideshow_slide]").get(a_active+1));
	}
	
if (direction=="prev")
	{
	if (a_active<=0) 
		{
		slider_active_slide($("[type=slideshow_slide]").get(a_total));
		}else slider_active_slide($("[type=slideshow_slide]").get(a_active-1));
	}
}

function change_slide()
{
if (a_counter>0)
	{
	a_counter--;
	} else
	{
	switch_slide("next");
	}
setTimeout("change_slide()",1000);
}


function realign_style_elements()
{
if ($(".slideshow_nav").length!=0){
	$(".slideshow_nav").css("display","block");
	var pp=$(".slider");
	$(".slideshow_nav").css("left",pp.offset().left-4+"px");
	$(".slideshow_nav").css("top",pp.offset().top+pp.height()-15+"px");
}
	
if ($(".slideshow_nav_arrows").length!=0)
	{
	$(".slideshow_nav_arrows").css("display","block");
	var pp=$(".slider");
	$(".slideshow_nav_arrows").css("left",pp.offset().left+pp.width()-50+"px");
	$(".slideshow_nav_arrows").css("top",pp.offset().top+pp.height()-15+"px");
	}
	
if ($("#google_map").length!=0)
	{
	map_initialize("google_map");
	map_change_type(a_map_type);
	map_set_zoom(a_map_zoom);
	map_set_center(a_coord_x,a_coord_y);
	map_add_marker(a_coord_x,a_coord_y,a_map_marker_text);
	}
}

//map utils
var map;

function map_initialize(container_id) {
var latlng = new google.maps.LatLng(0,0);
var myOptions = {
	zoom: 2,
	mapTypeControl: false,
	center: latlng,
	mapTypeId: google.maps.MapTypeId.ROADMAP,
	
    navigationControl: true,
    navigationControlOptions: {
        style: google.maps.NavigationControlStyle.ZOOM_PAN,
        position: google.maps.ControlPosition.TOP_RIGHT
		},
    scaleControl: true,
    scaleControlOptions: {
        position: google.maps.ControlPosition.TOP_LEFT
		}

	};

map = new google.maps.Map(document.getElementById(container_id), myOptions);
  
}


function map_set_center(coordx,coordy) {
map.setCenter(new google.maps.LatLng(coordx, coordy),map.getZoom(),google.maps.MapTypeId.SATELLITE);
}

function map_set_zoom(zoom_level) {
map.setZoom(parseInt(zoom_level)); 
}

function map_change_type(map_type)
{
map.setMapTypeId(map_type);
} 

function map_add_marker(coordx,coordy,text) {
var marker = new google.maps.Marker({
 position: new google.maps.LatLng(coordx,coordy),  
});  
 
var infowindow = new google.maps.InfoWindow({
    content:text
});

 
marker.setMap(map); 
if (text!="") infowindow.open(map,marker);

}





//jquery effects
function visual_effect(action,id)
{
	if (action=="fadeout")	$('#'+id).fadeOut('slow', function() { }); 
	if (action=="fadein")	$('#'+id).fadeIn('slow', function() { }); 
	if (action=="hide")	$('#'+id).hide('slow', function() { }); 
	if (action=="fasthide")	$('#'+id).hide('fast', function() { }); 
	if (action=="show")	$('#'+id).show('slow', function() { }); 
	if (action=="slidetoggle")	$('#'+id).slideToggle('slow', function() { }); 
	if (action=="slideup")	$('#'+id).slideUp('slow', function() { }); 
	if (action=="slidedown")	$('#'+id).slideDown('slow', function() { }); 
}



function clear_field(e)
{
var i=-1;
//check if element is already added
for (var a=0;a<a_elements.length;a++)
	{
	if (a_elements[a][0]==e.name)
		{
		i=a;
		break;
		}
	}

//add new element
if (i==-1)
	{
	var a_item=new Array(3);
	a_item[0]=e.name;
	a_item[1]=e.value;
	a_item[2]=1;
	e.value="";
	a_elements.push(a_item);
	} else
	{
	if (a_elements[i][2]==0)
		{
		if (e.value==a_elements[i][1])
			{
			e.value="";
			}
		a_elements[i][2]==1;
		}
	}

}

function restore_field(e)
{
//restore default value if element value is empty
if (e.value=="")
	{
	var i=-1;
	for (var a=0;a<a_elements.length;a++)
		{
		if (a_elements[a][0]==e.name)
			{
			i=a;
			break;
			}
		}
	if (i>-1)
		{
		e.value=a_elements[i][1];
		a_elements[i][2]=0;
		}
	
	}
	
}

