(function($){

	// to track if the mouse button is pressed
	var isMouseDown    = false;

	// to track the current element being dragged
	var currentElement = null;

	// callback holders
	var dropCallbacks = {};
	var dragCallbacks = {};
	
	// bubbling status
	var bubblings = {};

	// global position records
	var lastMouseX;
	var lastMouseY;
	var lastElemTop;
	var lastElemLeft;
	
	// track element dragStatus
	var dragStatus = {};	

	// if user is holding any handle or not
	var holdingHandler = false;

	// returns the mouse (cursor) current position
	$.getMousePosition = function(e){
		var posx = 0;
		var posy = 0;

		if (!e) var e = window.event;

		if (e.pageX || e.pageY) {
			posx = e.pageX;
			posy = e.pageY;
		}
		else if (e.clientX || e.clientY) {
			posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			posy = e.clientY + document.body.scrollTop  + document.documentElement.scrollTop;
		}

		return { 'x': posx, 'y': posy };
	};

	// updates the position of the current element being dragged
	$.updatePosition = function(e) {
		var pos = $.getMousePosition(e);

		var spanX = (pos.x - lastMouseX);
		var spanY = (pos.y - lastMouseY);

		$(currentElement).css("top",  (lastElemTop + spanY));
		$(currentElement).css("left", (lastElemLeft + spanX));
	};

	// when the mouse is moved while the mouse button is pressed
	$(document).mousemove(function(e){
		if(isMouseDown && dragStatus[currentElement.id] != 'false'){
			// update the position and call the registered function
			$.updatePosition(e);
			if(dragCallbacks[currentElement.id] != undefined){
				dragCallbacks[currentElement.id](e, currentElement);
			}

			return false;
		}
	});

	// when the mouse button is released
	$(document).mouseup(function(e){
		if(isMouseDown && dragStatus[currentElement.id] != 'false'){
			isMouseDown = false;
			if(dropCallbacks[currentElement.id] != undefined){
				dropCallbacks[currentElement.id](e, currentElement);
			}

			return false;
		}
	});

	// register the function to be called while an element is being dragged
	$.fn.ondrag = function(callback){
		return this.each(function(){
			dragCallbacks[this.id] = callback;
		});
	};

	// register the function to be called when an element is dropped
	$.fn.ondrop = function(callback){
		return this.each(function(){
			dropCallbacks[this.id] = callback;
		});
	};
	
	// disable the dragging feature for the element
	$.fn.dragOff = function(){
		return this.each(function(){
			dragStatus[this.id] = 'off';
		});
	};
	
	// enable the dragging feature for the element
	$.fn.dragOn = function(){
		return this.each(function(){
			dragStatus[this.id] = 'on';
		});
	};
	
	// set a child element as a handler
	$.fn.setHandler = function(handlerId){
		return this.each(function(){
			var draggable = this;
			
			// enable event bubbling so the user can reach the handle
			bubblings[this.id] = true;
			
			// reset cursor style
			$(draggable).css("cursor", "");
			
			// set current drag status
			dragStatus[draggable.id] = "handler";

			// change handle cursor type
			$("#"+handlerId).css("cursor", "move");	
			
			// bind event handler
			$("#"+handlerId).mousedown(function(e){
				holdingHandler = true;
				$(draggable).trigger('mousedown', e);
			});
			
			// bind event handler
			$("#"+handlerId).mouseup(function(e){
				holdingHandler = false;
			});
		});
	}

	// set an element as draggable - allowBubbling enables/disables event bubbling
	$.fn.easydrag = function(allowBubbling){

		return this.each(function(){

			// if no id is defined assign a unique one
			if(undefined == this.id || !this.id.length) this.id = "easydrag"+(new Date().getTime());
			
			// save event bubbling status
			bubblings[this.id] = allowBubbling ? true : false;

			// set dragStatus 
			dragStatus[this.id] = "on";
			
			// change the mouse pointer
			$(this).css("cursor", "move");

			// when an element receives a mouse press
			$(this).mousedown(function(e){
				
				// just when "on" or "handler"
				if((dragStatus[this.id] == "off") || (dragStatus[this.id] == "handler" && !holdingHandler))
					return bubblings[this.id];

				// set it as absolute positioned
				$(this).css("position", "absolute");

				// set z-index
				$(this).css("z-index", parseInt( new Date().getTime()/1000 ));

				// update track variables
				isMouseDown    = true;
				currentElement = this;

				// retrieve positioning properties
				var pos    = $.getMousePosition(e);
				lastMouseX = pos.x;
				lastMouseY = pos.y;

				lastElemTop  = this.offsetTop;
				lastElemLeft = this.offsetLeft;

				$.updatePosition(e);

				return bubblings[this.id];
			});
		});
	};

})(jQuery);

function openVerMenu(mid){
	if($("#vsubmenu_"+mid).css("display")=="none"){
		$("#vsubmenu_"+mid).show(200);
		$("#vmenu_group_"+mid).css("background","#f1f4f0");
		saveOpenedMenu(mid,1);
	}else{
		$("#vsubmenu_"+mid).hide(200);
		$("#vmenu_group_"+mid).css("background","none");
		saveOpenedMenu(mid,0);
	}
}
function saveOpenedMenu(mid,stat){
	 $.ajax({
	   type: "get",
	   url: saveMenuScript,
	   data: "mid="+mid+"&stat="+stat,
	   success: function(msg){
		 void(0);
	   }
	 });
}

function equalHeight(group) {
	var tallest = 0;
	group.each(function() {
		var thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}

window.onload=function() {
	fancybox();
	if(!fontsizein){
		if((screen.width*0.6) < screen.height){
			fontsize=screen.width/10;
		}else{
			fontsize=(screen.height*1.67)/10;
		}
		$('body').css('font-size',fontsize+"%");
		$.ajax({url: '?fontsize='+fontsize});
	}
	$('input[type=text][title!=""]').each(function() {
	if ($.trim($(this).val()) == '') $(this).val($(this).attr('title'));
	if ($(this).val() == $(this).attr('title')) $(this).addClass('exampleText');
	}).focus(switchText).blur(switchText);
	$('.dev').hide();
	$('#dev0').show();
	if(product<1){
		$('#center_divs').fadeIn(2000);
		$('.leftside a.fades').click(function(){
			$('#center_divs').fadeOut(1000);
		});
		$('.rightside a.fades').click(function(){
			$('#center_divs').fadeOut(1000);
		});
	}else{
		$('#center_divs').show();
	}
	$("#fancybox-outer").easydrag(true);
	
  if(!p_title && product<1 && ie_detect<1){
  	$('#rightside').animate({"width": "18%"}, 500);
		$('.rightside').animate({marginRight: "0"}, 500);
	}
	$('#instuructions_txt table tr:first,#parks_ftext table tr:first').addClass("first_line");
	$('#rampas table tr:first').addClass("first_line");
	$('#overflash_div').height($('#overflash_div').width()*0.6);
	equalHeight($(".nosubmen"));
	if($('.rightside').height()>$('.leftside').height()){
			$('.leftside').height($('.rightside').height()+30);
		//	alert ($('.rightside').height());
		//	alert ($('.leftside').height());
		}
	$("#volume_control_div").draggable({ axis: 'x' },{ opacity: 0.35 },{ containment: 'parent' });
	$('a').click(function(){volume_music()});
	$("#gallery_div").hover(
	  function () {
			$('#mainpic_hov').show();
			$('#gallery_div_img').hide();
			$('#galdivdivs').animate({width: '155px'}, 1500);
	  },
		function (){
			$('#mainpic_hov').hide();
			$('#gallery_div_img').show();
			$('#galdivdivs').animate({width: '1px'}, 1);
		}
	);	
	if($('#motioncontainer').css("display")) fillup();
}
window.onbeforeunload=function(){
	position=$('#player_position').val();
	$.ajax({
	  url: '?position='+position
	});
}
function play_music(){
	volume=$('#volume_control_div').position().left;
	$.ajax({
  url: '?play=1&volume='+volume
});
}
function stop_music(){
	volume=$('#volume_control_div').position().left;
	$.ajax({
  url: '?stop=1&volume='+volume
});
}
function volume_music(){
	position=$('#player_position').val();
	volume=$('#volume_control_div').position().left;
	$.ajax({
	  url: '?volume='+volume+'&position='+position
	});
}

window.onresize=function() {
//	alert($('#overflash_div').width());
//	alert($('#overflash_div').width()*0.6);
	$('#overflash_div').height($('#overflash_div').width()*0.6);
	$('#aifreims').height($('#leftside').height()-100);
}
function form_f(form_url,prod_id,form_ajax){
	$("#form_li a").attr("href", form_ajax+form_url);
	$("#form_produce").val(prod_id);
//	$(".prolist_prodelem a").css("color","#232323");
//	$(".prolist_prodelem:hover a").css("color","#e2151d");
//	$("#prodlist_name_"+prod_id+" a").css("color","#e2151d");
	$("div").removeClass("prodlist_name2");
	$("#prodlist_name_"+prod_id).addClass("prodlist_name2");
}
function switchText(){
	if ($(this).val() == $(this).attr('title'))
		$(this).val('').removeClass('exampleText');
	else if ($.trim($(this).val()) == '')
		$(this).addClass('exampleText').val($(this).attr('title'));
}

var devc=1;		
function dev(){
	$('.dev').hide();
	if(devc<cdev){$('#dev'+devc).show();devc++;
	}else{
		$('#dev0').show();
		devc=1;
	}
}

function redy_img() {
	$('#fancybox-inner').height($('.rampas').height());
	$('#fancybox-wrap').height($('.rampas').height());
	setTimeout('redy_img2()',1000);
}
function redy_img2() {
	$('#fancybox-inner').height($('.rampas').height());
	$('#fancybox-wrap').height($('.rampas').height());
	setTimeout('redy_img3()',1000);
}
function redy_img3() {
	$('#fancybox-inner').height($('.rampas').height());
	$('#fancybox-wrap').height($('.rampas').height());
//	setTimeout(redy_img(),1500);
}

function fancybox(){
	$("a[rel=lightbox]").fancybox({
		'speedIn'			: 900,
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over'
	});
	
	$(".prod_ajax").fancybox({
		'speedIn'			: 900,
 //   'autoDimensions'	: false,
 		'padding'			: 0,
 		'autoScale'		: false,
    'height' 			: '424',
    'width'  			: '600',
//    'overlayShow'	:	false,
		'transitionIn': 'elastic',
		'transitionOut': 'none',
    'margin'			: 0,
    'scrolling'		: 'none',
		ajax    			: {}
	});
	$(".gal_ajax").fancybox({
		'speedIn'			: 900,
 		'padding'			: 0,
 		'autoScale'		: false,
// 		'autoDimensions'		: false,
    'height' 			: '424',
    'width'  			: '600',
		'transitionIn': 'elastic',
		'transitionOut': 'none',
 //   'overlayShow'	:	false,
    'margin'			: 0,
    'scrolling'		: 'none',
		ajax    			: {}
	});
	$(".form_ajax").fancybox({
		'speedIn'			: 900,
 		'padding'			: 0,
 		'autoScale'		: false,
// 		'autoDimensions'		: false,
    'height' 			: '424',
    'width'  			: '600',
		'transitionIn': 'elastic',
		'transitionOut': 'none',
 //   'overlayShow'	:	false,
    'margin'			: 0,
    'scrolling'		: 'none',
		ajax    			: {}
	});
}
function ajax_sendnews(link, container, value,name) {
	if(value != ''){
		$.ajax({
			url : link,
			data : { email:value,action:'asdsa', name:name, },
			type : 'post',
			success : function(msg){
				$('#'+container).html(msg).fadeIn(100);
				$('#'+container).fadeOut(6000);
			}
		});
	}
}
function formsubmit(){
	$.ajax({
	type: "POST",
	async: false,
	url: $('#url').val(),
	data: "name="+$('#name').val()+"&surname="+$('#surname').val()+"&email="+$('#email').val()+"&phone="+$('#phone').val()+"&commentform_tex="+$('#commentform_tex').val()+"&order_form="+$('#order_form').val()+"&url="+$('#url').val(),
	success: function(msg){
	if(msg==1)
	$("#error_login").hide().removeClass("error").empty();
	else
	$("#error_login").addClass("error").text(''+msg+'').show();
	}
	});
}
