var a_elements=new Array();
var a_tmp_div=document.createElement("div");

$(document).ready(function() {
 
 	$(window).resize(function() {
	 realign_style_elements();
	});
	
	//menu buttons
	$('[type=btn_menu]').each( function(index) {
		$(this).bind("mouseover", function() {
			$(this).attr("class","btn_menu_active_center");
			$("#"+$(this).attr("id")+"_left").attr("class","btn_menu_active_left");
			$("#"+$(this).attr("id")+"_right").attr("class","btn_menu_active_right");
		});
		
		$(this).bind("mouseout", function() {
			$(this).attr("class","btn_menu_center");
			$("#"+$(this).attr("id")+"_left").attr("class","btn_menu_left");
			$("#"+$(this).attr("id")+"_right").attr("class","btn_menu_right");
		});
	});

	$('[vtype=login_modal]').bind("click", function() {
		$('#modal_login').modal({
			overlayClose:true,
			onOpen: function (dialog) {
				dialog.overlay.fadeIn('fast', function () {
					dialog.data.hide();
					dialog.container.fadeIn('fast', function () {
						dialog.data.slideDown('slow');
					});
				});
			}
		});
		return false;
	});
	
	//winners
	$('[vtype^=show_winners_]').each(function(){
		$(this).bind("click", function() {
			$("#"+$(this).attr("div_id")).modal({overlayClose:true});
			return false;
		});
	});
	
	//user profile list items
	$('[type=user_list_item]').each( function(index) {
		$(this).bind("click", function() {
			unselect_items($(this));
		});
		
		if ($(this).attr("active")=="1") {
			unselect_items($(this));
		}
	});	
	
	//content_left_top_corner / top tabs
	$('[type=top_tab]').each( function(index) {
	$(this).bind("mouseover", function() {
			if ($(this).attr("first")=="1") {
				$(this).attr("class","tab_white_center_first");
				if ($("#content_left_top_corner").length!=0)
					{
					$("#content_left_top_corner").attr("class","tab_white_left");
					}
			} else {
				$(this).attr("class","tab_white_center");
			}
		});
		
	$(this).bind("mouseout", function() {
			if ($(this).attr("first")=="1") {
				$(this).attr("class","tab_gray_center_first");
				if ($("#content_left_top_corner").length!=0)
					{
					$("#content_left_top_corner").attr("class","tab_gray_left");
					}
			} else {
				$(this).attr("class","tab_gray_center");
			}
		});
	});
 

	
	//search box
    if ($("#search_box").length!=0)
	{
	$("#search_box").bind("keyup",function() {
		search_query($("#search_box").val());
	});
	
	$("#search_box").bind("focusin",function() {
		clear_field($("#search_box").get(0));
		$(this).css("color","#000000");
	});	
	
	$("#search_box").bind("focusout",function() {
		restore_field($("#search_box").get(0));
		$(this).css("color","#B4B4B4");
	});

    if ($("#page_body").length!=0)
		{
		$(a_tmp_div).html($("#page_body").html());
		}
	
	}
	
	//default field values
	$('input,textarea').each( function(index) {
		if ($(this).attr("def")!="" && $(this).val()=="")
			{
			//set default value
			$(this).val($(this).attr("def"));
			
			$(this).bind("click", function() {
				clear_field($(this).get(0));
			});
			
			$(this).bind("focusout", function() {
				restore_field($(this).get(0));
			});
			}
	});	
	
	
	//arrow navigation
	if ($("[vtype=image_enable_arrows]").length!=0){
		arrow_nav_bind();
	}

	prettyCounter();
	
	SI.Files.stylizeAll();
	realign_style_elements();

});


function unselect_items(element){

//user list items
$("[type|=user_list_item]").each(function(index) {
	$(this).attr("active","0");
	$(this).attr("class","user_list_item");
	$("#"+$(this).attr("div_id")).css("display","none");
});

$(element).attr("active","1");
$(element).attr("class","user_list_item_active");
$("#"+$(element).attr("div_id")).css("display","block");
$("[name='active_item']").val($(element).attr("div_id"));
}

function realign_style_elements(){
	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);
	}
	
	if ($(".slideshow").length!=0){
		$(".slideshow").css("width",$(document).width()+"px");
		$(".slideshow").css("height",$(document).height()+"px");
	}
 
}

function search_query(keyword){
	if (keyword!=""){
		$.ajax({
			type: "GET",
			url: http_path+'/includes/search_results.php',
			data: "search_string="+keyword,
			success: function(data) {
				if (keyword==$("#search_box").val()) $("#page_body").html(data);
			}
		});
	}else {
		$("#page_body").html($(a_tmp_div).html());
	}
}

//map utils
var map;

function map_initialize(container_id) {
var latlng = new google.maps.LatLng(0,0);

var show_controls=true;
if (typeof a_map_controls != 'undefined' && !a_map_controls) show_controls=false;

var myOptions = {
	zoom: 2,
	mapTypeControl: false,
	center: latlng,
	mapTypeId: google.maps.MapTypeId.ROADMAP,
	
    navigationControl: show_controls,
    navigationControlOptions: {
        style: google.maps.NavigationControlStyle.ZOOM_PAN,
        position: google.maps.ControlPosition.TOP_RIGHT
		},
    scaleControl: show_controls,
    scaleControlOptions: {
        position: google.maps.ControlPosition.TOP_LEFT
		}

	};

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


function map_set_center(coordx,coordy) {
var stepy=0;
var stepx=0;

if (typeof a_map_marker_visible != 'undefined' && a_map_marker_visible) {
	stepy=0.0038518;
	stepx=0.0020000;
	//stepy=0;
	//stepx=0;
}
coordx=parseFloat(coordx)+stepy;
coordy=parseFloat(coordy)+stepx;

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); 
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 enter_submit(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
   myfield.form.submit();
   return false;
   }
else
   return true;
}



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;
		}
	
	}
	
}


//input makeup

if (!window.SI) { var SI = {}; };
SI.Files =
{
	htmlClass : 'file_wrapper',
	fileClass : 'file',
	wrapClass : 'cabinet',
	
	fini : false,
	able : false,
	init : function()
	{
		this.fini = true;
		
		var ie = 0 //@cc_on + @_jscript_version
		if (window.opera || (ie && ie < 5.5) || !document.getElementsByTagName) { return; } // no support for opacity or the DOM
		this.able = true;
		
		var html = document.getElementsByTagName('html')[0];
		html.className += (html.className != '' ? ' ' : '') + this.htmlClass;
	},
	
	stylize : function(elem)
	{
		if (!this.fini) { this.init(); };
		if (!this.able) { return; };
		
		elem.parentNode.file = elem;
		elem.parentNode.onmousemove = function(e)
		{
			if (typeof e == 'undefined') e = window.event;
			if (typeof e.pageY == 'undefined' &&  typeof e.clientX == 'number' && document.documentElement)
			{
				e.pageX = e.clientX + document.documentElement.scrollLeft;
				e.pageY = e.clientY + document.documentElement.scrollTop;
			};

			var ox = oy = 0;
			var elem = this;
			if (elem.offsetParent)
			{
				ox = elem.offsetLeft;
				oy = elem.offsetTop;
				while (elem = elem.offsetParent)
				{
					ox += elem.offsetLeft;
					oy += elem.offsetTop;
				};
			};

			var x = e.pageX - ox;
			var y = e.pageY - oy;
			var w = this.file.offsetWidth;
			var h = this.file.offsetHeight;

			this.file.style.top		= y - (h / 2)  + 'px';
			this.file.style.left	= x - (w - 30) + 'px';
		};
	},
	
	stylizeById : function(id)
	{
		this.stylize(document.getElementById(id));
	},
	
	stylizeAll : function()
	{
		if (!this.fini) { this.init(); };
		if (!this.able) { return; };
		
		var inputs = document.getElementsByTagName('input');
		for (var i = 0; i < inputs.length; i++)
		{
			var input = inputs[i];
			if (input.type == 'file' && input.className.indexOf(this.fileClass) != -1 && input.parentNode.className.indexOf(this.wrapClass) != -1)
			{
				this.stylize(input);
			};
		};
	}
}


function arrow_nav_bind(){
	$(window).bind("keypress",function(event){
		var code = (event.keyCode ? event.keyCode : event.which);
		if (code==39){
			//forward
			arrow_nav_forward();
		} else {
			if (code==37){
				//backward
				arrow_nav_backward();
			}
		}
	});
}

function arrow_nav_forward(){
	window.location=$("#nav_next_img").attr("href");
}

function arrow_nav_backward(){
	window.location=$("#nav_prev_img").attr("href");
}

function prettyCounter(){
	if ($(".frontpage_overall_num").length!=0){
		
		$.ajax({
			type: "POST",
			url: http_path+'/includes/counter.php',
			data: "get=1",
			success: function(data) {
				if ($(".frontpage_overall_num").text()!=data){
					$(".frontpage_overall_num").text(data);
				}
			}
		});	
	
		setTimeout("prettyCounter()",1000);
	}
}
