﻿navHover = function() {
	var lis = document.getElementById("navmenu-h").getElementsByTagName("LI");
	for (var i=0; i<lis.length; i++) {
		lis[i].onmouseover=function() {
			this.className+=" iehover";
		}
		lis[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" iehover\\b"), "");
		}
	}
}
window.myconfirm = function(text, url){
	$.get("index.php?controller=main&action=myconfirm&text=" + text + "&confirm_ok_url=" + escape(url), function(responce){
		$("#general_popup").html(responce);
	});	
}

window.myalert = function(text, css){
	geogoer().site().lightBox("index.php?controller=main&action=myalert&text=" + text + "&classname=" + css);
}


if (window.attachEvent && $("#navmenu-h").get(0)) window.attachEvent("onload", navHover);

usermentHover = function() {
	
	var lis = document.getElementById("user_menu").getElementsByTagName("LI");
	for (var i=0; i<lis.length; i++) {
		lis[i].onmouseover=function() {
			this.className+=" iehover";
		}
		lis[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" iehover\\b"), "");
		}
	}
}
if (window.attachEvent && $("#user_menu").get(0)) window.attachEvent("onload", usermentHover);

function move(tr, direction, href)
{
	// Find previous or next sibling
	var anchor = (direction == 'up') ? $(tr).prev('tr') : $(tr).next('tr');
	
	// If this tr is first or last element in the table do nothing
	if(!anchor) return;


			// Move tr before previous or after next sibling
			if('up' == direction) 
			{
				$(anchor).before(tr);
			} 
			else 
			{
				$(anchor).after(tr);
			}
}
function remove(source){
	$(source).remove();
}
function remove_city_block(source)
{
	if($("select.cities_class").size() > 2)
	{
	   $(source).remove();
    }
    
    if($(".city_block").size() <= 2)
    {
       $("a.remove_city").css("display", "none");
    }
}

window.city_block_count = 2;
function add_city_block() {
	var clonedBlock = $(".city_block").get(0).cloneNode(true);	
    $(".cities_list").append(clonedBlock);
    clonedBlock = $('.cities_list').find('.city_block:last');

    var cities_list = clonedBlock.find(".cities_class");
    var name = cities_list.attr("name").replace(/^(\w+)\[\d*\]$/, "$1[" + (window.city_block_count++) + "]");
	cities_list.attr("name", name);
	
    $(clonedBlock).find("a.remove_city").bind("click", function(e) { 
        e.preventDefault(); 
        remove_city_block($(this).parents('.city_block').get(0));
    });
	
	if($(".city_block").size() > 2)
    {
       $("a.remove_city").css("display", "block");
    }
}

function calculate_price(field)
{
	var mul = 1;
	$(field).parents('tr').find('.dynamic_field').each(function() { mul *= parseInt(this.value) });
	$(field).parents('tr').find('.sum_field').attr('value', mul);
	
	var sum = 0;
	$('.sum_field').each(function() { sum += parseInt(this.value) });
	$('#sum_all').attr('value', sum);
}

function toggle_cost(select) {
    if(select.value == 'static')
    {
        $("input.cost_class").css('display','block');
    }
    else
    {
        $("input.cost_class").css('display','none');
    }
}

window.get_general_div_height = function(){
	header_div_height = $('#general_header_div').height(); 
	hole_window_height = $(window).height();
	content_div_height = hole_window_height - header_div_height;
	return content_div_height -1;
}
window.get_general_div_width = function(){
	right_div_width = $('#general_right_div').width(); 
	hole_window_width = $(window).width();
	content_div_width = hole_window_width - right_div_width;
	return content_div_width;
}
window.get_middle_div_height = function(){
	header_div_height = $('#general_header_div').height(); 
	hole_window_height = $(window).height();
	content_div_height = hole_window_height - header_div_height;
	return content_div_height;
}

function clear_latlng(id){
	document.getElementById('lat_' + id).value = ''; 
	document.getElementById('lng_' + id).value = '';
}
function check_field_status(id, prefix){
	if(document.getElementById('lat_' + id).value == '' || document.getElementById('lng_' + id).value == ''){  
		$("#" + prefix + '' + id).removeClass('autocomplete_field_ok');
		$("#" + prefix + '' + id).removeClass('autocomplete_field_err');
	}
	else{
		$("#" + prefix + '' + id).removeClass('autocomplete_field_err');
		$("#" + prefix + '' + id).addClass('autocomplete_field_ok');
	}
}
function checkout_field_status(id, prefix){
	$("#" + prefix + '' + id).removeClass('autocomplete_field_err');
	$("#" + prefix + '' + id).removeClass('autocomplete_field_ok');
}



$(function() {

	//general div height (firstly)
	
	//$('#general_middle_div').css('height', window.get_middle_div_height() + 'px');
	//$('#general_content_div').css('height', window.get_middle_div_height() + 'px');
	
	//general div height (WHEN WINDOW RESIZED)
	$(window).bind("resize", function(e) {
		//$('#general_middle_div').css('height', window.get_middle_div_height() + 'px');
		//$('#general_content_div').css('height', window.get_middle_div_height() + 'px');
		//$('#general_right_div').css('height', window.get_middle_div_height() + 'px');
	});
	
    if($("select.cost_type_class").size() > 0) toggle_cost($("select.cost_type_class"));
	
	$("select.cost_type_class").bind("change", function(e) { toggle_cost(this) });
	$("a.up").bind("click", function(e) {
	    e.preventDefault();
		move($(this).parents('tr').get(0),'up', this.href);
	});

	$("a.down").bind("click", function(e) {
		e.preventDefault();
	    move($(this).parents('tr').get(0),'down', this.href);
	});
	
	/*-----------------------------------------------------------------------------*/
    //                                            GENERAL
	/*-----------------------------------------------------------------------------*/
	$("input.autocomplete_field").bind("focus", function(e) {
		$(this).addClass('autocomplete_field_hover');
	});
	$("input.autocomplete_field").bind("blur", function(e) {
		$(this).removeClass('autocomplete_field_hover');
	});
	$("input[@type='text'].form_field").bind("focus", function(e) {
		$(this).addClass('form_field_hover');
	});
	$("input[@type='text'].form_field").bind("blur", function(e) {
		$(this).removeClass('form_field_hover');
	});
	
	
	
	$("a.a_close").bind("click", function(e) {
		e.preventDefault();
		if($(this).parents('div.superfield_div').find('input.close_open_class').get(0).value=='open'){
			$(this).parents('div.superfield_div').find('input.close_open_class').get(0).value='close';
			$(this).parents('div.superfield_div').find('input.type_class').get(0).value='city';
			$(this).parents('div.superfield_div').addClass('superfield_close');
			$(this).parents('div.superfield_div').find('a.superfield_link').each(function(i) {
				$(this).removeClass("current");
			});
			$(this).parents('div.superfield_div').find('a.a_city').addClass("current");
		}
	});
	
	$("a#search_add").bind("click", function(e) {
		e.preventDefault();
		s = 0;
		$(this).parents("table").find('div.superfield_div').each(function(i){
			if(($(this).get(0).className == "superfield_div superfield_close") && (s==0)){
				s = 1;
				$(this).removeClass('superfield_close');
				$(this).find('input.close_open_class').get(0).value='open';
			}
		});
	});
	$(".facebox_colse").bind("click", function(e) {
		$.facebox.close();
	});
	$("div.superfield_div div.city_div").bind("click", function(e) {
		e.preventDefault();
		if($(this).parents('div.superfield_div').get(0).className == "superfield_div superfield_close"){
			$(this).parents('div.superfield_div').removeClass('superfield_close');
			$(this).parents('div.superfield_div').find('input.close_open_class').get(0).value='open';
			$(this).parents('div.superfield_div').find('input.type_class').get(0).value='city';
		}		
	});

					//$("a").each(function() {
					//	$(this).bind("click", function(e) {
					//		e.preventDefault();
					//		$.get(this.href, function(response){
//$("#general_content").html(response);
					//		});
					//	});
					//});
			
					//$("#general_content").load("templates/main.html");

//-------------------------------AJAX SEARCH LINE-------------------------------------------------------

	
	$("a.remove").bind("click", function(e) {
		e.preventDefault();
	    remove_city_block($(this).parents('tr').get(0), this.href);
	});
	$("a.remove_city").bind("click", function(e) {
		e.preventDefault();
	    remove_city_block($(this).parents('.city_block').get(0));
	});

//------------------------------------------------------------------------------------------------------------	
	
	
	$("a.new_city").bind("click", function(e) { e.preventDefault(); add_city_block() }); 
	
	$("input.dynamic_field").bind("keyup", function(e) { calculate_price(this) });
	
	
	
	$("a.issaugoti").bind("click", function(e) {
		e.preventDefault();
		count = 0;
		$("input.butinas").each(function(i) {
			if(this.value == ""){
				count++;
				this.style.backgroundColor = 'red';
			}
		});
		if(count != 0){alert("Ne visi laukai yra užpylditi");}
		else{
			i = 0;
			count2 = 0;
			
			$("input.pos").each(function(z) { this.value = (++count2) })

			$("#products_form").submit();
		}
	});
	$("a.delete_order").bind("click", function(e) {
		e.preventDefault();
		var question = confirm("Ar tikrai norite pasalinti uzsakyma?");
			if (question){
				 remove($(this).parents('tr').get(0), this.href);
				 $.get('index.php?controller=orders&action=delete&id=' + this.rel);
			}
	});
	$("table.content tr.row").each(function(i) {
	        $(this).addClass(i % 2 == 1 ? 'first' : 'second');
	});
	
	$("table.content2_hovered tr.row").bind('mouseover', function(i) {
	        $(this).css('background-color','#ffefa1');
			$(this).find('a.black_u_link').css('text-decoration','underline');
			$(this).css('cursor','hand');
	});
	$("table.content2_hovered tr.row").bind('mouseout', function(i) {
	        $(this).css('background-color','white');
			$(this).find('a.black_u_link').css('text-decoration','none');
			//$(this).css('cursor','auto');
	});
	$("table.content2_hovered tr.message_row td.coll").bind('click', function(i) {
			//$(this).find('a.black_u_link').preventDefault();
			if(!$(this).find("input[@type='checkbox']").val()){
				window.open($(this).parents('tr').find('a.black_u_link').attr('href'),'_self');
			}
	});
	
	$("input[@name='select_search_type']").bind("click", function() {
		if($("input[@name='select_search_type']:checked").val() == 'driver'){
			$("#passenger_div").css("display","block");
			$("#driver_div").css("display","none");
			//$("input[@name='link']")[0].value = $("input[@name='link_yes']")[0].value;
		}
		if($("input[@name='select_search_type']:checked").val() == 'passenger'){
			$("#passenger_div").css("display","none");
			$("#driver_div").css("display","block");
			//$("input[@name='link']")[0].value = $("input[@name='link_no']")[0].value;
		}
	});
	
	$("a.a_city").bind("click", function(e) {
		e.preventDefault();
		if($(this).parents('div.superfield_div').get(0).className != "superfield_div superfield_close"){
			$(this).parents('div.superfield_div').find('input.type_class').get(0).value='city';
		
			$(this).parents('div.superfield_div').find('a.superfield_link').each(function(i) {
				$(this).removeClass("current");
			});
			$(this).addClass("current");
		}
	});
	$("a.a_ivent").bind("click", function(e) {
		e.preventDefault();
		if($(this).parents('div.superfield_div').get(0).className != "superfield_div superfield_close"){
			$(this).parents('div.superfield_div').find('input.type_class').get(0).value='ivent';
			
			$(this).parents('div.superfield_div').find('a.superfield_link').each(function(i) {
				$(this).removeClass("current");
			});
			$(this).addClass("current");
		}
	});
	
		$('.rating').each(function(){
		var matches = this.id.match(/_(\d+)_([\d.]+)$/);
		var id = matches[1];
		var value = matches[2];
		
		$(this).rating('index.php?controller=groups&action=rating&id=' + id, {maxvalue:5, curvalue: value});
	});
	$("input.search_submit_button").bind("click", function(e) {
		$("#search_form").get(0).submit();
	});
	$("a.add_to_contacts").bind("click", function(e) {
		e.preventDefault();
		var question = confirm($(this).attr('title'));
		if (question){
			 $.get('index.php?controller=user&action=add_to_contacts&id=' + this.rel);
		}
		
	});
	 $('.close_popup').click($.unblockUI);
});
