$(document).ready(function(){

//	if($('#player').length > 0) {
//		flowplayer("player", ROOT+"flash/flowplayer-3.1.5.swf",  { 
//		    clip: { 
//		        // these two configuration variables does the trick 
//		        autoPlay: true,  
//		        autoBuffering: true // <- do not place a comma here   
//		    } 
//		}); 
//	}
	
	$("a[href*=.flv]").addClass('flashPlayer');
	$("a[href*=.flv]").flowplayer(ROOT+"flash/flowplayer-3.1.5.swf");
	
	check_catalog_short_photos();
	check_article_short_photos();
	
	disable_class_selection('disable_selection');
	
	
	checboxes_as_radio();
	
	activate_cust_checkboxes();
	
	manage_order_checkboxes();
	
	
	put_error_after('users_label');
	put_error_after('consultation_label');
	
	put_flash_message();
	
	bindDescriptionClick('product_short_description');
	bindDescriptionClick('category_short_description');
	bindDescriptionClick('click_prev');
	
	manage_index_images();
	
//	$("a.fancy_gallery").attr('rel', 'gallery').fancybox({
////		'autoScale'	: false,
//		'padding'	: 0,
//		'margin'	: 0,
//		'cyclic'	: true
//	});	
	
	$("a.fancy_gallery").fancybox({
		'autoScale'	: false,
		'padding'	: 0,
		'margin'	: 0,
		'overlayOpacity': 0.7,
		'overlayColor': '#000',
		'cyclic'	: true
	});

	confirm_reservation();
	show_reservation_info();
});

function show_reservation_info() {

//$(".reservation_info").qtip({
//$(".make_reservation").qtip({
	$(".reservation_info").qtip({
		position: {
			target: false,
			adjust: {x: -195, y: -171}
		},

		style: {
		  width: 300,
		  height: 110,
		  padding: 10,
	      background: '#ffffff',
		  'font-size': '12px',
		  'line-height': '1.6',
	      color: 'black',
	//      textAlign: 'center',
		  border: {
			 width: 4,
			 radius: 4,
			 color: '#64524b'
		  },
		  tip: {
			  corner: 'bottomMiddle',
			  size: {
				x: 15, // Be careful that the x and y values refer to coordinates on screen, not height or width.
				y : 7 // Depending on which corner your tooltip is at, x and y could mean either height or width!
			 }
		},
		  name: 'dark' // Inherit the rest of the attributes from the preset dark style
	   },

		show: 'mouseover',
		hide: 'mouseout'
	});
}

function confirm_reservation() {
	$('a.my_time, a.reserved_time').click(function(){
		$.alerts.okButton = '&nbsp;'+texts[2]+'&nbsp;';
		$.alerts.cancelButton = '&nbsp;'+texts[3]+'&nbsp;';
		var link = this;
		jConfirm('&nbsp;'+texts[7]+'&nbsp;', '', function(r) {
			if (r) {
				window.location = $(link).attr('href');
			}
		});
		return false;
	});
	$('a.make_reservation').click(function(){
		
		if($(this).hasClass('not_logged')) {
			$.alerts.okButton = '&nbsp;'+texts[4]+'&nbsp;';
			jAlert(texts[9], 'vienas');
		} else {
			$.alerts.okButton = '&nbsp;'+texts[2]+'&nbsp;';
			$.alerts.cancelButton = '&nbsp;'+texts[3]+'&nbsp;';
			var link = this;
			jConfirm('&nbsp;'+texts[8]+'&nbsp;', '', function(r) {
				if (r) {
					window.location = $(link).attr('href');
				}
			});
		}
		return false;
	});
	
}

var IE6UPDATE_OPTIONS = {
	icons_path: "img/ie6update/",
	message: texts[6]
}

function manage_index_images() {
	
	if(index_images.length > 0) {
		var n = index_images.length;
		var i = 1;
		setTimeout(function() {timeout_index_images(n,i);}, 3000); 
	}
}

function timeout_index_images(n,i) {
	if(i == n) {
		i = 0;
	}
	var bckg = 'transparent url("'+ROOT+'uploads/images/pradinis/'+index_images[i]+'") no-repeat right bottom';

	$('#index_content_top div.main').fadeOut( 300, function(){
		$('#index_content_top div.main').css('background', bckg);
		$('#index_content_top div.main').fadeIn(1000);
	});
	
//	$('#index_content_top div.main').css('background', bckg);

	i++;
	setTimeout(function() {timeout_index_images(n,i);}, 3000);
}

//function timeout_index_images(n,i) {
//	if(i == n) {
//		i = 0;
//	}
//	var bckg = 'transparent url("'+ROOT+'uploads/images/pradinis/'+index_images[i]+'") no-repeat right bottom';
//	$('#index_content_top div.main').css('background', bckg);
//	i++;
//	setTimeout(function() { timeout_index_images(n,i); }, 2200);
//}

function bindDescriptionClick(class_name) {
	$('.'+class_name).click(function(){
		var href = $(this).prev().children('a').attr('href');
		if(href && href != undefined) {
			window.location = href;
		} else {
			href = $(this).prev('a').attr('href');
			if(href && href != undefined) {
				window.location = href;
			} else {
				return false;
			}
		}
		
	});
	$('.'+class_name).css('cursor','pointer');
}

function start_scrolling_photos() {
	$('.category_short_photo').each(function(){
		var images = null;
		images = $(this).next('.category_products_photos').find('img');
		var obj = $(this).children('a').children('img');
		
		timeOutSetPhoto(obj,images,0,0);
	});
}

function timeOutSetPhoto(obj, images, images_i, global_i) {
	if($(images).length > 0) {
		if(images_i == $(images).length) {
			images_i = 0;
		}

		if(images_i == 0 && ($(images).length == 1 || global_i == 0)) {
			$(obj).attr('src', $(images).eq(images_i).attr('src'));
		} else {
			var obj1 = obj;
			var images1 = images;
			var images_i1 = images_i;
			$(obj).fadeOut(300, function(){
				$(obj1).attr('src', $(images1).eq(images_i1).attr('src'));
				$(obj).fadeIn(800);
			});
		}
		
//		$(obj).attr('src', $(images).eq(images_i).attr('src'));

		images_i++;
		global_i++;
		setTimeout(function() {timeOutSetPhoto(obj,images,images_i,global_i);},3000);
	}
}

function put_flash_message() {
	if($('#flashMessage').length > 0) {
//		setTimeout('fadeOutflashMessage();', '7000');
		if($('div.page_title').length > 0) {
			$('div.page_title').after($('#flashMessage'));
		}
		
		if($('div#forum_path').length > 0) {
			$('div#forum_path').after($('#flashMessage'));
		}
		
		if($('.newsletter_error').length > 0) {
			$('#newsletter_block .text_div').remove();
			$('#newsletter_block h3').after($('#flashMessage'));
			$('#flashMessage').css('margin','10px 0 5px');
		}
		
		if($('.login_error').length > 0) {
			$('.user_login_block h3').after($('#flashMessage'));
			$('#flashMessage').css('margin','-5px 0 -12px 5px');
		}
		
		if($('.post_added_message').length > 0) {
			$('#flashMessage').css('margin','10px 0 0 0');
		}
		if($('.post_error_message').length > 0) {
			$('#flashMessage').css('margin','10px 0 0 0');
		}
		
		if($('.search_error').length > 0) {
			show_flash_as_alert();
		}
		
		if($('.register_message').length > 0) {
			show_flash_as_alert();
		}
		
		if($('.alert_message').length > 0) {
			show_flash_as_alert();
		}
		
		if($('.admin_edit_page_block').length > 0 ) {
			$('#flashMessage').css('margin','0px 0 10px 0');
		}
	}
}
function show_flash_as_alert() {
	$.alerts.okButton = '&nbsp;'+texts[4]+'&nbsp;';
	jAlert($('#flashMessage').html(), texts[5]);
	if($('#flashMessage').hasClass('error_message')) {
		$('#popup_content').addClass('error_message_popup');
	} else {
		$('#popup_content').addClass('successfull_message_popup');
	}
	$('#flashMessage').remove();
}

function put_error_after(divclass) {
	if($('.error-message').length > 0 && $('.'+divclass).length) {
		$('.error-message').each(function(){
			var next_div = $(this).next('.'+divclass);
			$(next_div).after($(this));
//			$(next_div).after('<div class="clear" style="height:2px;"></div>');
		});
	}
}

function manage_order_checkboxes(){
	$('#CategoryOrderForm .checkbox').click(function(){
		$('#CategoryOrderForm input[type=checkbox]').attr('checked',false);
		$('#CategoryOrderForm .checkbox').removeClass('checked');
		
		var checkbox_class = $(this).prev('input').attr('class');
		checkbox_class = checkbox_class.split(' ');
		var set_class = checkbox_class[0];
		
		$('.'+set_class).attr('checked',true);
		$('.'+set_class).next('label').addClass('checked');
		
		return false;
		
//		$('#CategoryOrderForm .checkbox').each(function(){
//			if($(this).hasClass('checked')){
//				$(this).removeClass('checked');
//			}
//		});
//		$(this).addClass('checked');
	});
}

function activate_cust_checkboxes() {
	$("input[type='checkbox']").prettyCheckboxes({
		checkboxWidth: 17,
		checkboxHeight: 17
	});
}

function checboxes_as_radio() {
	$('.checboxes_as_radio').click(function(){
		$('.checboxes_as_radio').attr('checked', false);
		$(this).attr('checked', true);
	});
	
	$('label[for="MailerOrder"]').click(function(){
		$('label[for="MailerReject"].checked ').trigger('click');
	});
	$('label[for="MailerOrder"]').mouseup(function(){
		if($(this).hasClass('checked')) {
			$(this).trigger('click');
		}
	});
	
	$('label[for="MailerReject"]').click(function(){
		$('label[for="MailerOrder"].checked ').trigger('click');
		
	});
	$('label[for="MailerReject"]').mouseup(function(){
		if($(this).hasClass('checked')) {
			$(this).trigger('click');
		}
	});
}

function check_catalog_short_photos() {
	$('.product_short_description').each(function(){
		var image = $(this).find('img');
		if(image.length > 0) {
			$(this).parent().children('.product_short_photo').children('a').html(image);
		}
	});
}
function check_article_short_photos() {
	$('.article_short_description').each(function(){
		var image = $(this).find('img');
		if(image.length > 0) {
			$(this).parent().children('.article_short_photo').children('a').html(image);
			$('.article_short_photo').children('a').children('img').css('width','100px');
			$('.article_short_photo').children('a').children('img').css('height','100px');
		}
	});
}

function fadeOutflashMessage(){
	$('#flashMessage').fadeOut('slow');
}


function disable_class_selection(class_name) {
	$('.'+class_name).each(function(element){
		disableSelection(this);
	});
	
	$('input').focus(function(element){
		$('.'+class_name).each(function(element){
			enableSelection(this);
		});
	});
	
	$('input').blur(function(element){
		$('.'+class_name).each(function(element){
			disableSelection(this);
		});
	});
}

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

