$(document).ready(function(){

	ask_delete();
//	on_focus_select();
	formDefaultValues();
	rowToClick();
	hoverRow()
	closeFlashMessage()
	max_length();

	initAccordionHover('side_categories_list');
	
	mainShowVideo();

	manage_active_input_ie();
	
});



function manage_active_input_ie() {
	if ($.browser.msie) {
		$('input[type=text],textarea,input[type=password]').focus(function(){
			$(this).addClass('active_input')
		});
		$('input[type=text],textarea,input[type=password]').blur(function(){
			$(this).removeClass('active_input')
		});
	}
}

function mainShowVideo() {
	$('#main_view_video').click(function(){
		var height = 380;
		var width = 480;
		var fromTop = getVerticalCenter(height);
		
		var href = $(this).attr('href');
		href = ROOT+'app/webroot/uploads/media/'+href+'.flv';
		
		var title = $(this).attr('name');
		
		var link = '<a href="'+href+'" class="flashPlayer" id="showMainMovie">&nbsp;</a>';
		
		var div = '<div style="width: '+width+'px; height: '+(height+20)+'px; cursor:default;">';
		div += title;
		div += link;
		div += '</div>';
		
		div += $('#other_videos_block').html();
		
		
		$.blockUI({
			title:    'Video', 
            message:  div,
            css: { 
                width: width+'px', 
                height: height+'px', 
                top: fromTop+'px', 
                border: 'none',
                'text-align': 'left', 
                padding: '0px', 
                backgroundColor: 'transparent', 
                color: '#fff' 
            },
			overlayCSS:  { 
				backgroundColor: '#000', 
				opacity: 0.9
			}
		});
		
		var main_player = flowplayer("showMainMovie", ROOT+'flash/flowplayer-3.1.5.swf', href);
		main_player.play();

//		$('#showMainMovie').flowplayer(ROOT+'flash/flowplayer-3.1.5.swf', {
//			clip: {
//				autoPlay: true
//			}
//		}); 
		unblockPlayerUI();

		return false;
	});
}

function unblockPlayerUI() {
	$('.blockOverlay').click(function(){
		$('.blockUI').remove();
	});
}

function initAccordionHover(class_name) {
	$('.'+class_name+' ul').hide();
	
	$('.'+class_name+' li a').hoverIntent(function(){initAccordionHoverTimeOut(this,class_name)},function(){});
	
//	function(){initAccordionHoverTimeOut(obj,class_name)}
//	$('.'+class_name+' li a').mouseover(function() {
//		var obj = this;
//		setTimeout (function(){initAccordionHoverTimeOut(obj,class_name)}, 300 );
////		initAccordionHoverTimeOut(this,class_name);
//	});
}
function initAccordionHoverTimeOut(obj,class_name) {
	$('.'+class_name+' li a').removeClass('orange');
		$(obj).addClass('orange');
		$(obj).blur();
		
		var checkElement = $(obj).next();

		if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
			$('.'+class_name+' ul:visible').slideUp('normal');
			checkElement.slideDown('normal');
			return false;
		}
}

function initAccordion(class_name) {
	$('.'+class_name+' ul').hide();
	
	$('.'+class_name+' li a').click(function() {
		$('.'+class_name+' li a').removeClass('orange');
		$(this).addClass('orange');
		$(this).blur();
		var checkElement = $(this).next();
		if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
			checkElement.slideUp('normal');
			return false;
		}
		if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
			$('.'+class_name+' ul:visible').slideUp('normal');
			checkElement.slideDown('normal');
			return false;
		}
	});
}



function max_length() {
	maxKeyPress();
	maxKeyUp();
	maxPaste();
}

function closeFlashMessage() {
	$('#closeFlashMessage').click(function(){
		$('#flashMessage').remove();
	});
	
}

function hoverRow() {
	$('.listTable tr').hover(function(){
		$(this).addClass('hoverRow');
	}, function(){
		$(this).removeClass('hoverRow');
	});
	
}

function rowToClick() {
	$(".rowToClick td:not(.ignoreCell)").click(function(){
		var url = $(this).parent('tr').children('.cellToClick').children('.linkToClick').attr('href');
		window.location = url;
	});
}

function formDefaultValues() {
	$(".default-value").each(function(){
		var label_value = $('label[for=' + $(this).attr("id") + ']').html();
		$('label[for=' + $(this).attr("id") + ']').hide();
		$(this).val(label_value);
		if($(this).val() == label_value) {
			$(this).addClass("inactive_color");
		} else {
			$(this).removeClass("inactive_color");
			
		}
	});
	
	$(".default-value").focus(function() {
		var label_value = $('label[for=' + $(this).attr("id") + ']').html();
	
		if (this.value == label_value) {
			this.value = '';
			$(this).removeClass("inactive_color");
		}
		
		$(this).blur(function() {
			if (this.value == '') {
				$(this).addClass("inactive_color");
				this.value = label_value;
			}
		});
	});
}



function ask_delete() {
	$('.ask_delete').click(function(){
		var elem = this;
		
		$.alerts.okButton = '&nbsp;'+texts[2]+'&nbsp;';
		$.alerts.cancelButton = '&nbsp;'+texts[3]+'&nbsp;';
		
		jConfirm(texts[1], texts[0], function(result) {
			
			if(result) {
				window.location = $(elem).attr('href');
			}
			return false;
		});
		return false;
	});
}

function ask_delete_old() {
	$('.ask_delete').click(function(){
		var item = '';
		if($(this).attr('name')){
			item = ': '+$(this).attr('name');
		}
		
		var answer = confirm('Ar tikrai norite iĆ?trinti'+item+'?');
		if(!answer) {
			return false;
		}
	});
}


function on_focus_select() {
	$('.on_focus_select').focus(function(){
		$(this).select();
	});
}



//---PAGALBINES FUNKCIJOS----------------------------------------------

function var_dump(obj) {
   if(typeof obj == "object") {
      return "Type: "+typeof(obj)+((obj.constructor) ? "\nConstructor: "+obj.constructor : "")+"\nValue: " + obj;
   } else {
      return "Type: "+typeof(obj)+"\nValue: "+obj;
   }
}//end function var_dump

function getLeft(el) {
    xPos = el.offsetLeft;
    tempEl = el.offsetParent;
    while (tempEl != null) {
        xPos += tempEl.offsetLeft;
          tempEl = tempEl.offsetParent;
    }
    return xPos;
}
function getTop(el) {
    yPos = el.offsetTop;
    tempEl = el.offsetParent;
    while (tempEl != null) {
        yPos += tempEl.offsetTop;
          tempEl = tempEl.offsetParent;
    }
    return yPos;
}

function maxKeyPress(){
	$('textarea').keydown(function(evt){
		var obj = this;
		var maxLength = obj.getAttribute("maxlength");
		var e = window.event ? event.keyCode : evt.which;
	    if ( (e == 32) || (e == 13) || (e > 47)) { //IE
	        if(maxLength && (obj.value.length > maxLength-1)) {
	            if (window.event) {
	                window.event.returnValue = null;
	            } else {
	                evt.cancelDefault;
	                return false;
	            }
	        }
	    }
	});
}

function maxKeyUp(){
	$('textarea').keyup(function(evt){
		var obj = this;
	    var maxLength = obj.getAttribute("maxlength");
	     if(maxLength && obj.value.length > maxLength){
	           obj.value = obj.value.substr(0,maxLength);
	     }
	    sr = obj.getAttribute("showremain");
	    if (sr) {
	        document.getElementById(sr).innerHTML = maxLength-obj.value.length;
	    }
	});
}

function maxPaste(){
	$('textarea').keyup(function(evt){
		var obj = this;
		var maxLength = obj.getAttribute("maxlength");
		if(maxLength){
			var value = $(this).val();
			var count = $(this).val().length;
			 if(count > maxLength) {
			 	$(this).val(value.substr(0,maxLength))
			 }
		}
	});
}

function removeSpaces(string) {
	return string.split(' ').join('');
}

function getVerticalScrollCenter(height) {
	 var heightCenter = ($(window).height() - height) / 2 + $(window).scrollTop();
	 return heightCenter;
}
function getHorizontalScrollCenter(width) {
	 var widthCenter = ($(window).width() - width) / 2 + $(window).scrollLeft();
	 return widthCenter;
}
function getVerticalCenter(height) {
	 var heightCenter = ($(window).height() - height) / 2;
	 return heightCenter;
}
function getHorizontalCenter(width) {
	 var widthCenter = ($(window).width() - width) / 2;
	 return widthCenter;
}

function disableSelection(target){
	if (typeof target.onselectstart!="undefined") //IE route
		target.onselectstart=function(){return false}
	else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
		target.style.MozUserSelect="none"
	else //All other route (ie: Opera)
		target.onmousedown=function(){return false}
	target.style.cursor = "default"
}

function enableSelection(target){
	if (typeof target.onselectstart!="undefined") //IE route
		target.onselectstart=function(){return true}
	else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
		target.style.MozUserSelect="text"
	else //All other route (ie: Opera)
		target.onmousedown=function(){return true}
	target.style.cursor = "default"
}


function disableCtrlKeyCombination(e)
{
    //list all CTRL + key combinations you want to disable
//    var forbiddenKeys = new Array('a', 'c', 'x', 'v');
    var forbiddenKeys = new Array('a', 'c');
    var key;
    var isCtrl;
    if(window.event)
    {
        key = window.event.keyCode;     //IE
        if(window.event.ctrlKey)
            isCtrl = true;
        else
            isCtrl = false;
    }
    else
    {
        key = e.which;     //firefox
        if(e.ctrlKey)
            isCtrl = true;
        else
            isCtrl = false;
    }

    //if ctrl is pressed check if other key is in forbidenKeys array
    if(isCtrl)
    {
        for(i=0; i<forbiddenKeys .length; i++)
    	{
            //case-insensitive comparation
            if(forbiddenKeys[i].toLowerCase() == String.fromCharCode(key).toLowerCase())
            {
//                alert('Key combination CTRL + '+String.fromCharCode(key)+' has been disabled.');
                return false;
            }
        }
    }
    return true;
}
