/* JAVASCRIPT */
$(document).ready(function() {
	/* function for feading change logo */
	$('#header h1 a').hover(function() {
		var obj = $(this),
			image = $('<img />').attr({'alt': '', 'src': 'http://www.kameliya-atanasova.com/images_new/logo_hover.png'}),
			old_image = obj.find('img');
		
		obj.prepend(image);
		old_image.fadeOut('slow', function() {
			$(this).remove();
		});
	}, function() {
		var obj = $(this),
			image = $('<img />').attr({'alt': '', 'src': 'http://www.kameliya-atanasova.com/images_new/logo.png'}),
			old_image = obj.find('img');
		
		obj.prepend(image);
		old_image.fadeOut('slow', function() {
			$(this).remove();
		});
	});
	
	/* loading background images on homepage */
	if ($(".homepage").size() > 0) {
		$.supersized({
			
			//Functionality
			slideshow : 1, //Slideshow on/off
			autoplay : 1, //Slideshow starts playing automatically
			start_slide : 1, //Start slide
			slide_interval : 3000, //Length between transitions
			transition : 1, //0-None, 1-Fade, 2-Slide Top, 3-Slide Right, 4-Slide Bottom, 5-Slide Left, 6-Carousel Right, 7-Carousel Left
			transition_speed : 500, //Speed of transition
			new_window : 1, //Image links open in new window/tab
			pause_hover : 0, //Pause slideshow on hover
			keyboard_nav : 1, //Keyboard navigation on/off
			performance : 1, //0-Normal, 1-Hybrid speed/quality, 2-Optimizes image quality, 3-Optimizes transition speed // (Only works for Firefox/IE, not Webkit)

			//Size & Position
			min_width : 0, //Min width allowed (in pixels)
			min_height : 0, //Min height allowed (in pixels)
			vertical_center : 0, //Vertically center background
			horizontal_center : 1, //Horizontally center background
			fit_portrait : 0, //Portrait images will not exceed browser height
			fit_landscape : 0, //Landscape images will not exceed browser width

			//Components
			navigation : 1, //Slideshow controls on/off
			thumbnail_navigation : 1, //Thumbnail navigation
			slide_counter : 1, //Display slide numbers
			slide_captions : 0, //Slide caption (Pull from "title" in slides array)
			slides : [ //Slideshow Images
				{image : 'images_new/backgrounds/background_01.jpg', title : '', url : 'javascript:;', textcolor : '#000000'},
				{image : 'images_new/backgrounds/background_02.jpg', title : '', url : 'javascript:;', textcolor : '#000000'},
				{image : 'images_new/backgrounds/background_03.jpg', title : '', url : 'javascript:;', textcolor : '#000000'},
				{image : 'images_new/backgrounds/background_04.jpg', title : '', url : 'javascript:;', textcolor : '#FFFFFF'}
			], 
			text_obj : $('#content').find('h1')
		});
	};
	
	/* gallery carousel - porfolio 
		������ ������ � ������� ������� �� �������� � ���� ����� � ��� ����������� ��������� �� ��������� �� ����������.
		���� �������� �� ��������� �� ��������� �� �������� ������� ��� �������� �� ������ � ��������� �������.
		��� ����� ��������� �� ������ �� ���� ������� �� ������ ��� ������� ��� ������ ��������� ���� ����� ��� �������� 
		� �� ������� ���� ������� �� �������. ����, �� � ������� ������ ��� ���� ��� ��������.
		���� ����� ���� �� �� ������� ��� ���� ajax, �� ����� ��-������ ������� � ������� ������� �� �� ������ ���-����
		� ����� ���� � ��������� ������ ���� ��� ����� �� �������� ���� ���.
	*/
	
	/*$('#portfolio_carousel').portfolio_carousel({
		prev_btn : $('#prev_image'),
		next_btn : $('#next_image'),
		paging : $('.header_paging'),
		list_array : []
	});*/

	$('#services_gallery').services_carousel({
		prev_btn : $('.prev_service'),
		next_btn : $('.next_service'),
		left_menu : $('.leftMenuItem')
	});
	
	$('#concurs_gallery').concurs_carousel({
		prev_btn : $('.prev_service'),
		next_btn : $('.next_service'),
		left_menu : $('.leftMenuItem')
	});
	
	$('.video_list').fullscreen({
		prev_btn : $('.prev_video'),
		next_btn : $('.next_video'),
		close_btn : $('.close_video'),
		item_type : 'iframe'
	});
	
	$('#clien_zone_list').fullscreen({
		prev_btn : $('.cz_prev_image'),
		next_btn : $('.cz_next_image'),
		close_btn : $('.cz_close_fl'),
		paging : $('.fullscreen_paging span'),
		item_type : 'img'
	});
	
	$('.list_circle_thumbnails').fading({
		items: 'li'
	});
	
	$('.list_thumbnails').fading({
		items: 'a'
	});
	
	$('#recommendation_one').recommentdations();
	
	$('#recommendation_two').recommentdations();
	
	$('#recommendation_three').recommentdations();
	
	slide_sidebar();
});

/* slide items in sidebar if item`s width is bigger than sidebar width */
function slide_sidebar() {
	var sidebar = $('.sidebar');
	if (sidebar.size() == 0) {
		return;
	};
	
	var sidebar_width = 182,
		items = new Array();
	
	sidebar.children('ul').children('li').children('a').each(function() {
		if ($(this).width() > sidebar_width) {
			items.push($(this));
		};
	});
	
	
	setTimeout(function() {
				slide_left();
			}, 4000);
				
	function slide_left() {
		for(var i = 0; i < items.length; i++) {
			items[i].animate({
				left: -((items[i].width())  - sidebar_width)
			}, 1000, function() {
				
			});
		};
		setTimeout(function() {
					slide_right();
				}, 4000);
	};
	
	function slide_right() {
		for(var i = 0; i < items.length; i++) {
			items[i].animate({
				left: 0
			}, 1000, function() {
				
			});
		};
		setTimeout(function() {
					slide_left();
				}, 4000);
	};

}

(function($) {
	/* slide recommendations */
	$.fn.recommentdations = function() {
		var object = $(this),
			text = object.find('p'),
			text_height = text.height(),
			visible_area = 128,
			steps = Math.ceil(text_height / visible_area),
			paging = object.find('.paging'),
			btn_prev,
			btn_next,
			pages,
			current = 0,
			moving = false;
		
		if (steps <= 1) {
			paging.hide();
			return false;
		} else {

			btn_prev = $('<a />').attr({
				'class' : 'prev',
				'href' : 'javasctipr:;',
				'title' : ''
			}).text('move up'),
			btn_next = $('<a />').attr({
				'class' : 'next',
				'href' : 'javasctipr:;',
				'title' : ''
			}).text('move down'),
			pages = $('<span />').attr('class', 'pages');
			
			paging.append(btn_prev).append(pages).append(btn_next);
			
			for (var i = 1; i <= steps; i++ ) {
				var link = $('<a />').attr({
					'href' : 'javascript:;'
				}).text(i);
				pages.append(link);
			};
			
			pages.find('a:first').addClass('active');
			pages = pages.find('a');
		};
		
		btn_prev.click(function() {
			var next = current - 1 < 0 ? steps - 1 : current - 1;
			return move(next);
		});
		
		btn_next.click(function() {
			var next = current + 1 >= steps ? 0 : current + 1;
			return move(next);
		});
		
		pages.click(function() {
			var next = $(this).index();
			return move(next);
		});
		
		function move(next) {
			if (moving) {
				return false;
			};
			moving = true;
			text.animate({
				top: (-1 * next * visible_area)
			}, 500, function() {
				current = next;
				paging.find('span.pages a.active').removeClass('active').end().find('span.pages a:eq('+ current +')').addClass('active');
				moving = false;
			});
			return false;
		}
		
	};
	
	/* fadin thumbnails mask*/
	$.fn.fading = function(options) {
		var object = $(this);
		if(object.size() == 0) {
			return;
		};
		
		var items = object.find(options.items);
		
		
		
		items.hover(function() {
			if(i_am_old_ie) {
				$(this).find('.hover_mask').show();
			} else {
				$(this).find('.hover_mask').fadeIn(50);
			}
		}, function() {
			if (i_am_old_ie) {
				$(this).find('.hover_mask').hide();
			} else {
				$(this).find('.hover_mask').fadeOut(50);
			};
		});
		
	};
	
	/* function fullscreen */
	$.fn.fullscreen = function(options) {

		var obj = $(this);
		if (obj.size() == 0) {
			return;
		};
		
		

		var items = obj.find('li a'),
			fullscreen = $('#fullscreen_mask'),
			media = fullscreen.find(options.item_type),
			prev_btn = options.prev_btn,
			next_btn = options.next_btn,
			close_btn = options.close_btn,
			paging = options.paging,
			current_image = 0;
		
		items.click(function() {
			var src = $(this).attr('href').split('#')[1];
				if(options.item_type == 'img') {
					var image = document.createElement('img');
						image.src = src;
						
					calculate_size(image);
				} else {
					media.attr('src', src).end().show();
				}
			
			$(this).parent().addClass('active');
			current_image = $(this).parent().index() + 1;
			
		
			
			update_paging();
			return false;
		});
		
		next_btn.unbind('click');
		
		next_btn.click(function() {
			var next_item = obj.find('li.active').next();
			
			if (next_item.size() == 0) {
				next_item = obj.find('li:first');
			};
				
			var src = next_item.find('a').attr('href').split('#')[1];
			if (options.item_type == 'img') {
				var image = document.createElement('img');
					image.src = src;
				calculate_size(image);
				
				
				/*
				var stage = $('.center_image'),
					old_img = stage.find('img'),
					new_image = $('<img />').attr('src', src);
					
					stage.prepend(new_image);
					old_img.fadeOut(function() {
						$(this).remove();
					});
				*/
			} else {
				media.attr('src', src);
			}
			
			obj.find('li.active').removeClass('active');
			next_item.addClass('active');
			current_image = next_item.index() + 1;
			update_paging();
		});
		
		prev_btn.unbind('click');
		
		prev_btn.click(function() {
			var prev_item = obj.find('li.active').prev();
			
			if (prev_item.size() == 0) {
				prev_item = obj.find('li:last');
			};
				
			var src = prev_item.find('a').attr('href').split('#')[1];
			
			if (options.item_type == 'img') {
				var image = document.createElement('img');
					image.src = src;
				calculate_size(image);
				/*
				var stage = $('.center_image'),
					old_img = stage.find('img'),
					new_image = $('<img />').attr('src', src);
					
					stage.prepend(new_image);
					old_img.fadeOut(function() {
						$(this).remove();
					});
				*/
			} else {
				media.attr('src', src);
			}
			
			obj.find('li.active').removeClass('active');
			prev_item.addClass('active');
			current_image = prev_item.index() + 1;
			update_paging();
		});
		
		close_btn.click(function() {
			obj.find('li.active').removeClass('active');
			fullscreen.hide();
			fullscreen.find('img').remove();
			media.attr('src', '');
			current_image = 0;
			update_paging();
		});
		
		function update_paging() {
			if (paging) {
				var images_length = obj.find('li').size(),
					text = current_image + '/' + images_length;
				paging.html(text);
			};
		};
		
		function calculate_size(image) {
			var image_width = image.width,
				image_height = image.height;
			if(image_width == 0 || image_height == 0) {
				setTimeout(function() {
					calculate_size(image);
				}, 100);
				return;
			};
			
			var window_width = $(window).width(),
				window_height = $(window).height();
			
			image.style.position = 'absolute';
			
			var ratio = window_height/window_width;
			
			//alert($(document).width());
			
			var text = "Image w: " + image_width;
			text += "</br> Image h: " + image_height;
			text += "</br> window w: " + window_width;
			text += "</br> window h: " + window_height;
			text += "</br> ration: " + ratio;
			
			
			
			
			
			if (image.height/image.width > ratio){
				
				// height is the problem
				if (image.height >= window_height){
					image.width = Math.round(image.width*((window_height - 50 )/image.height));
					image.height = window_height - 50;
				}
			} else {
				// width is the problem
				if (image.width > window_width - 150){
					image.height = Math.round(image.height*((window_width - 150)/image.width));
					image.width = window_width - 150;
				}
			} 
			
			top_margin = (window_height - image.height) / 2;
				image.style.top = top_margin + 'px';
			left_margin = (window_width - image.width) / 2;
				image.style.left = left_margin + 'px';
				
			$('.center_image').css({'top': top_margin, 'left':  left_margin});
			
			text += "</br> image c w: " + image.width;
			text += "</br> image c h: " + image.height;
			
			text += "</br> top margin: " + top_margin;
			text += "</br> left margin: " + left_margin;
				
			//paging.html(text);	
			
			$(window).resize(function() {
				window_width = $(window).width();
				window_height = $(window).height();
				ratio = window_height/window_width;
				if (image_height/image_width > ratio){
					// height is the proble
					if (image_height > window_height){
						image.width = Math.round(image_width*(window_height/image_height));
						image.height = window_height;
					} else {
						image.height = image_height;
						image.width = image_width;
					}
				} else {
					// width is the problem
					if (image_width > window_height){
						image.height = Math.round(image_height*(window_width/image_width));
						image.width = window_width;
					} else {
						image.height = image_height;
						image.width = image_width;
					}
				} 
				
				top_margin = (window_height - image.height) / 2;
					image.style.top = top_margin + 'px';
				left_margin = (window_width - image.width) / 2;
					image.style.left = left_margin + 'px';
			
			
					
				$('.center_image').css({'top': top_margin, 'left':  left_margin});
			});
			
			var old_image = fullscreen.find('img');
			if (old_image.size() == 0) {
				fullscreen.prepend(image).show();
			} else {
				fullscreen.prepend(image);
				old_image.fadeOut(function() {
					$(this).remove();
				});
			}
		};
	};
	
	function prev_fnk(){
		$(".prev_service").click();
	}
	
	function next_fnk(){
		$(".next_service").click();
	}
	
	/* carousel concurs */
	
	$.fn.concurs_carousel = function(options) {
		var list = $(this),
		speed = options.speed ? options.speed : 500,
		items = list.children('li'),
		items_size = items.size(),
		item_width = items.width(),
		list_width = (items.size() + 1) * item_width,
		counter = 1,
		paging = options.paging ? options.paging : false,
		menu = $('.sidebar_submenu'),
		progress = false,
		over = false;
		
		var
		History = window.History, 
		State = History.getState();
		
		if (list.size() == 0) {
			return;
		};
		
		if (items.size() <= 1) {
			options.prev_btn.hide();
			options.next_btn.hide();
		};
		
		list.css('width', list_width);

		options.left_menu.click(function()
		{
			var a_href = $($(this).html()).attr('href');
			var	expresion = new RegExp(/[\d]+/g);
			var	subCatIndex = expresion.exec(a_href.split('#')[1]);
			var	category = location.href.split('cat')[1].split('#')[0].replace('=','');
			
			var callBackUrl = 'http://www.kameliya-atanasova.com/getorderedservice.php?cat='+category+'&sub_cat='+subCatIndex;
			
			//var callBackUrl = 'http:///10.106.234.117:801/getorderedservice.php?cat='+category+'&sub_cat='+subCatIndex;
			
			$('#services_gallery').load(callBackUrl);
			
			var categoryTitle = $('#services_gallery').find('#' + subCatIndex).find('#category');
		
			$('#category_header_title').html(categoryTitle.html());
			
			menu.find('li.active').removeClass('active');
			menu.find('li#'+subCatIndex).addClass('active');
			
			$('a.facebook').attr('href', "http://www.facebook.com/share.php?u=http://www.kameliya-atanasova.com/services_viewer.php?cat=" + category + "&sub_cat=" + subCatIndex);
			
			$('a.twitter').attr('href', "http://twitter.com/share?url=" + encodeURIComponent("http://www.kameliya-atanasova.com/services_viewer.php?cat=" + category + "&sub_cat=" + subCatIndex));
			
			var fbImgSrc = $('#services_gallery').children('li').find('img').attr('src');

			
		});
		
		options.prev_btn.click(function() {
			
			if (progress) {
				return;
			};
			
			progress = true;
			var image = list.children('li:last');
			
			list.prepend(image.clone()).css('left', -item_width);
			list.animate({
				left: 0
			}, speed, function() {
				image.remove();
				var prev_menu_item = menu.find('li.active').removeClass('active').prev();
				if (prev_menu_item.size() == 0) {
					prev_menu_item = menu.find('li:last');
				};
				
				var title = list.find('#title');
				$('#concurs_header_title').html(title.html());
				
				var currentItemId = list.children('li').attr('id');
				
				if( prev_menu_item.attr('id') == currentItemId )
					prev_menu_item.addClass('active');

				var callBackUrl = 'http://www.kameliya-atanasova.com/services/getcomments.php?prs='+currentItemId;
				
				var realUrl = 'http://www.kameliya-atanasova.com/concurs_pics.php?id='+currentItemId;				  
			  	History.pushState({ path: this.path }, '', realUrl);
	
				//var callBackUrl = 'http://localhost:805/services/getcomments.php?prs='+currentItemId;
				
				$('#comments_container').load(callBackUrl);
	
				callBackUrl = 'http://www.kameliya-atanasova.com/services/getvotes.php?prs='+currentItemId;
				
				$('#totalVotes').load(callBackUrl);
				
				$('#c7').val(currentItemId);
				
				$("#c5").attr("class", "textbox");
				$("#c6").attr("class", "textbox");
				$("#c9").attr("class", "textbox");
				$("#c10").attr("class", "textbox");
				
				$('a.facebook').attr('href', "http://www.facebook.com/share.php?u=http://www.kameliya-atanasova.com/concurs_pics?id="+currentItemId);
				$('a.twitter').attr('href', "http://twitter.com/share?url=" + encodeURIComponent("http://www.kameliya-atanasova.com/concurs_pics?id="+currentItemId));
				progress = false;
			});
			
			
		});
		
		options.next_btn.click(function() {

			if(progress) {
				return;
			}
			
			progress = true;
			
			var image = list.children('li:first');

			list.append(image.clone());
			list.animate({
				left: -item_width
			}, speed, function() {
				image.remove();
				list.css('left', 0);
				var next_menu_item = menu.find('li.active').removeClass('active').next();
				if (next_menu_item.size() == 0) {
					next_menu_item = menu.find('li:first');
				};
				
				var title = list.find('#title');
				$('#concurs_header_title').html(title.html());
				
				var currentItemId = list.children('li').attr('id');			
				
				if( next_menu_item.attr('id') == currentItemId )
					next_menu_item.addClass('active');

				var href = menu.find('li.active').find('a').attr("href"),
							expresion = new RegExp(/[\d]+/g);
			
				var callBackUrl = 'http://www.kameliya-atanasova.com/services/getcomments.php?prs='+currentItemId;
				
				var realUrl = 'http://www.kameliya-atanasova.com/concurs_pics.php?id='+currentItemId;
				
			  	History.pushState({ path: this.path }, '', realUrl);
				
			  	
				$('#comments_container').load(callBackUrl);
	
				callBackUrl = 'http://www.kameliya-atanasova.com/services/getvotes.php?prs='+currentItemId;
				
				$('#totalVotes').load(callBackUrl);
				
				$('#c7').val(currentItemId);
				
				$("#c5").attr("class", "textbox");
				$("#c6").attr("class", "textbox");
				$("#c9").attr("class", "textbox");
				$("#c10").attr("class", "textbox");

				$('a.facebook').attr('href', "http://www.facebook.com/share.php?u=http://www.kameliya-atanasova.com/concurs_pics?id="+currentItemId);
				$('a.twitter').attr('href', "http://twitter.com/share?url=" + encodeURIComponent("http://www.kameliya-atanasova.com/concurs_pics?id="+currentItemId));
				progress = false;
			});
			
			
		});
	};
	
	/* carousel services */
	$.fn.services_carousel = function(options) {
		var list = $(this),
			speed = options.speed ? options.speed : 500,
			items = list.children('li'),
			items_size = items.size(),
			item_width = items.width(),
			list_width = (items.size() + 1) * item_width,
			counter = 1,
			paging = options.paging ? options.paging : false,
			menu = $('.sidebar_submenu'),
			progress = false,
			over = false;
		
		//alert();
		
		
		var url = location.href.split('#')[1],
			expresion = new RegExp(/[\d]+/g),
			digit = expresion.exec(url);
			
		if(!url){
			url = location.href.split('sub_cat')[1];
			expresion = new RegExp(/[\d]+/g);
			digit = expresion.exec(url);
		}
		
		var categoryTitle = list.find('#selected_service');
		$('#category_header_title').html(categoryTitle.html());
		
		if (list.size() == 0) {
			return;
		};
		
		if (items.size() <= 1) {
			options.prev_btn.hide();
			options.next_btn.hide();
		};
		
		list.css('width', list_width);

		options.left_menu.click(function()
		{
			var a_href = $($(this).html()).attr('href');
			var	expresion = new RegExp(/[\d]+/g);
			var	subCatIndex = expresion.exec(a_href.split('#')[1]);
			var	category = location.href.split('cat')[1].split('#')[0].replace('=','');
			
			var callBackUrl = 'http://www.kameliya-atanasova.com/getorderedservice.php?cat='+category+'&sub_cat='+subCatIndex;
			
			//var callBackUrl = 'http:///10.106.234.117:801/getorderedservice.php?cat='+category+'&sub_cat='+subCatIndex;
			
			$('#services_gallery').load(callBackUrl);
			
			var categoryTitle = $('#services_gallery').find('#' + subCatIndex).find('#category');
		
			$('#category_header_title').html(categoryTitle.html());
			
			menu.find('li.active').removeClass('active');
			menu.find('li#'+subCatIndex).addClass('active');
			
			$('a.facebook').attr('href', "http://www.facebook.com/share.php?u=http://www.kameliya-atanasova.com/services_viewer.php?cat=" + category + "&sub_cat=" + subCatIndex);
			
			$('a.twitter').attr('href', "http://twitter.com/share?url=" + encodeURIComponent("http://www.kameliya-atanasova.com/services_viewer.php?cat=" + category + "&sub_cat=" + subCatIndex));
			
			var fbImgSrc = $('#services_gallery').children('li').find('img').attr('src');

		});
		
	
		
		/*options.left_menu.click(function()
		{
				var href = $($(this).html()).attr('href');
			
				expresion = new RegExp(/[\d]+/g),
				digit = expresion.exec(href);
				
				//alert(digit);
				
				var selectedItem = 1;
				var activeItem = 0;
				var diff, left;
				var tmp = menu.find('li.active');
				
				//$(this).addClass('active');

				
				for(var i = 0; i < items.size(); i++){
					
					alert(items[i].id);
					
					if(items[i].id == $(this).attr("style"))
					{
						selectedItem = i;
					}

					if(items[i].id == tmp.attr("style"))
					{
						activeItem = i;
					}
				}
				
				diff = selectedItem - activeItem;
				left = diff * item_width;
				
				for(var i = 0; i < Math.abs(diff); i++){

					//if(diff >= 0)
					//	setTimeout(next_fnk, 520 * i);
					//else
					//	setTimeout(prev_fnk, 520 * i);
						 
				} 

			return;});*/
		
		options.prev_btn.click(function() {
					
			if (progress) {
				return;
			};
			
			progress = true;
			var image = list.children('li:last');
			
			list.prepend(image.clone()).css('left', -item_width);
			list.animate({
				left: 0
			}, speed, function() {
				image.remove();
				var prev_menu_item = menu.find('li.active').removeClass('active').prev();
				if (prev_menu_item.size() == 0) {
					prev_menu_item = menu.find('li:last');
				};
				
				var categoryTitle = list.find('#category');
				$('#category_header_title').html(categoryTitle.html());
				
				var currentItemId = list.children('li').attr('id');
				
				if( prev_menu_item.attr('id') == currentItemId )
					prev_menu_item.addClass('active');

				var href = menu.find('li.active').find('a').attr("href"),
					expresion = new RegExp(/[\d]+/g);
				
				if(href){
					var subCatIndex = expresion.exec(href.split('#')[1]),
						category = location.href.split('cat')[1].split('#')[0].replace('=','');
					
					$('a.facebook').attr('href', "http://www.facebook.com/share.php?u=http://www.kameliya-atanasova.com/services_viewer.php?cat=" + category + "&sub_cat=" + subCatIndex);
					
					$('a.twitter').attr('href', "http://twitter.com/share?url=" + encodeURIComponent("http://www.kameliya-atanasova.com/services_viewer.php?cat=" + category + "&sub_cat=" + subCatIndex));
					
				}else{
					var category = location.href.split('cat')[1].split('#')[0].replace('=','');
					
					$('a.facebook').attr('href', "http://www.facebook.com/share.php?u=http://www.kameliya-atanasova.com/services_viewer.php?cat=" + category);
					
					$('a.twitter').attr('href', "http://twitter.com/share?url=" + encodeURIComponent("http://www.kameliya-atanasova.com/services_viewer.php?cat=" + category));
				}
	
				progress = false;
			});
			
			
		});
		
		options.next_btn.click(function() {

			if(progress) {
				return;
			}
			
			progress = true;
			
			var image = list.children('li:first');

			list.append(image.clone());
			list.animate({
				left: -item_width
			}, speed, function() {
				image.remove();
				list.css('left', 0);
				var next_menu_item = menu.find('li.active').removeClass('active').next();
				if (next_menu_item.size() == 0) {
					next_menu_item = menu.find('li:first');
				};
				
				var categoryTitle = list.find('#category');
				$('#category_header_title').html(categoryTitle.html());
				
				var currentItemId = list.children('li').attr('id');			
				
				if( next_menu_item.attr('id') == currentItemId )
					next_menu_item.addClass('active');

				var href = menu.find('li.active').find('a').attr("href"),
							expresion = new RegExp(/[\d]+/g);
			
				if(href){
					var subCatIndex = expresion.exec(href.split('#')[1]),
						category = location.href.split('cat')[1].split('#')[0].replace('=','');
					
					$('a.facebook').attr('href', "http://www.facebook.com/share.php?u=http://www.kameliya-atanasova.com/services_viewer.php?cat=" + category + "&sub_cat=" + subCatIndex);
					
					$('a.twitter').attr('href', "http://twitter.com/share?url=" + encodeURIComponent("http://www.kameliya-atanasova.com/services_viewer.php?cat=" + category + "&sub_cat=" + subCatIndex));
					
				}else{
					var category = location.href.split('cat')[1].split('#')[0].replace('=','');
					
					$('a.facebook').attr('href', "http://www.facebook.com/share.php?u=http://www.kameliya-atanasova.com/services_viewer.php?cat=" + category);
					
					$('a.twitter').attr('href', "http://twitter.com/share?url=" + encodeURIComponent("http://www.kameliya-atanasova.com/services_viewer.php?cat=" + category));
				}

			
				progress = false;
			});
			
			
		});
	};
	
	
	/* carousel portfolio */
	$.fn.portfolio_carousel = function(options) {
		var list = $(this),
			list_array = options.list_array,
			speed = options.speed ? options.speed : 500,
			items_size = list_array.length,
			item_width = 800,
			list_width = (items_size + 2) * item_width,
			image_mask = list.parent().find('.image_mask'),
			counter = 1,
			paging = options.paging ? options.paging : false,
			progress = false,
			over = false,
			prev_slide = items_size - 1,
			current_slide = 0,
			next_slide = 1,
			interval;
			
		/* url hash */
		var url = location.href.split('img')[1],
			expresion = new RegExp(/[\d]+/g),
			digit = expresion.exec(url);
		
		
		if(digit) {
			counter = parseInt(digit[0], 10) > items_size || parseInt(digit[0], 10) == 0 ? 1  : parseInt(digit[0], 10);
			current_slide = parseInt(digit[0], 10) > items_size || parseInt(digit[0], 10) == 0 ? 0  : parseInt(digit[0], 10) - 1;
			prev_slide = current_slide - 1 < 0 ? items_size - 1 : current_slide - 1;
			next_slide = current_slide + 1 >= items_size ? 0 : current_slide + 1;		
		}

		if (items_size == 0) {
			return;
		};
		
		if (items_size <= 1) {
			options.prev_btn.hide();
			options.next_btn.hide();
		};

		function create_item(position) {
			
			var item = $('<li />'),
				img = $('<img />').attr({'src': list_array[position].src, 'alt': ''}),
				title = $('<h3 />').text(list_array[position].title),
				details_list = $('<ol />');
				photography = $('<li />').text('Photography').append(list_array[position].photography),
				assistent = $('<li />').text('Assistent').append(list_array[position].assistent),
				date = $('<li />').text('Date').append(list_array[position].date),
				image_location = $('<li />').text('Location').append(list_array[position].image_location);
				
				details_list.append(photography).append(assistent).append(date).append(image_location);
				details = $('<div />').append(title).append(details_list);
				item.append(img); //.append(details)
				//alert(list_array[position].src);
				return item;

		};
		
		
		list.append(create_item(prev_slide));
		list.append(create_item(current_slide));
		list.append(create_item(next_slide));
		
		list.css({'width': list_width, 'left': -item_width}).show();
		items = list.children('li');
		
		function show_mask() {
			/*clearTimeout(interval);
			if(over) {
				if(i_am_old_ie) {
					image_mask.show();
				} else {
					//image_mask.fadeIn();
				};
				show_details();
			} else {
				if(i_am_old_ie) {
					//image_mask.hide();
				} else {
					//image_mask.fadeOut();
				};
			};*/
		};
		
		options.prev_btn.click(function() {
			if (progress) {
				return;
			};
			progress = true;
			next_slide = current_slide;
			current_slide = prev_slide;
			if((prev_slide - 1) >= 0) {
				prev_slide = prev_slide - 1;
			} else {
				prev_slide = (items_size - 1);
			};

			list.animate({
				left: 0
			}, speed, function() {
				var new_item = create_item(prev_slide); 
				list.prepend(new_item);
				list.css('left', -item_width);
				list.children('li:last').remove();
				//show_details();
								
				var imgSrc = $(create_item(current_slide).html()).attr('src').split('/')[3],
					expresion = new RegExp(/[\d]+/g),
					imgNumber = expresion.exec(imgSrc);
				
				var href = document.location.href;
				
				href = href.replace(digit, imgNumber);
				$('a.facebook').attr('href', "http://www.facebook.com/share.php?u=" + href);
				$('a.twitter').attr('href', "http://twitter.com/share?url=" + encodeURIComponent(href));
				
				$('img.googleImg').attr('src',href);
				
				
				progress = false;
				
				
			});
			
			counter = (counter - 1) > 0 ? (counter - 1) : items_size;
			
			update_paging();
			items = list.children('li');
			items.bind('mouseover', function() {
				//image_mask.show();
			});
		});

		options.next_btn.click(function() {
			
			if(progress) {
				return;
			}
			//updateUrl();
			progress = true;
			prev_slide = current_slide;
			current_slide = next_slide;
			
			
			
			if((next_slide + 1) < items_size) {
				next_slide++;
			} else {
				next_slide = 0;
			};

			list.append(create_item(next_slide));
			list.animate({
				left: -(item_width * 2)
			}, speed, function() {
				list.css('left', -item_width);
				list.children('li:first').remove();
				
				var imgSrc = $(create_item(current_slide).html()).attr('src').split('/')[3],
				expresion = new RegExp(/[\d]+/g),
				imgNumber = expresion.exec(imgSrc);
		
				var href = document.location.href;
				
				href = href.replace(digit, imgNumber);
				$('a.facebook').attr('href', "http://www.facebook.com/share.php?u=" + href);
				$('a.twitter').attr('href', "http://twitter.com/share?url=" + encodeURIComponent(href));
			
				$('img.googleImg').attr('src',href);
				//show_details();
				progress = false;
			});
			
			counter = (counter + 1) > items_size ? 1 : counter + 1;

			update_paging();
			items = list.children('li');

			var src = $(create_item(current_slide).html()).attr("src");
			var imageName =src.split("/")[src.split("/").length - 1]; 
			
			items.bind('mouseover', function() {
				over = true;
				clearTimeout(interval);
				interval = setTimeout(function() { show_mask(); }, 300);
			});

		});
		

		options.prev_btn.hover(function() {
			over = true;
			clearTimeout(interval);
			interval = setTimeout(function() { show_mask(); }, 300);
		}, function() {
			over = false;
			clearTimeout(interval);
			interval = setTimeout(function() { show_mask(); }, 300);
		});
		
		items.mouseover(function() {
			over = true;
			clearTimeout(interval);
			interval = setTimeout(function() { show_mask(); }, 300);
		});
		
		image_mask.find('.info').mouseover(function() {
			over = true;
			clearTimeout(interval);
			interval = setTimeout(function() { show_mask(); }, 300);
		});
		
		image_mask.mouseover(function() {
			over = true;
			clearTimeout(interval);
			interval = setTimeout(function() { show_mask(); }, 300);
		});
		
		image_mask.mouseout(function() {
			over = false;
			clearTimeout(interval);
			interval = setTimeout(function() { show_mask(); }, 300);
		});
		
		function update_paging() {
			var text = counter + '/' + items_size;
			paging.html(text);
		};
		
		function show_details() {
			/*var info = $('<div />').addClass('info').html(list.children('li:eq(1)').children('div').html());
			image_mask.html(info);
			
			info.hover(function() {
				clearTimeout(interval);
				over = true;
				interval = setTimeout(function() { show_mask(); }, 300);
			}, function() {
				clearTimeout(interval);
				over = false;
				interval = setTimeout(function() { show_mask(); }, 300);
			});*/
		};
		
		update_paging();
	};
})(jQuery); 
