// ******************************************************************************************************************
// CHECK THAT FLASH VIDEOS ARE NOT TOO BIG AND THAT THEIR WMODE IS SET TO OPAQUE

function initFlashVideoCheck()
{
	if($('object').length != 0 && $('object').length > 0)
	{
		$('object').each(function(){
		
			var width = $(this).attr('width');
			if(width > 618)
			{
				$(this).attr('width',618);
				$(this).find('embed').attr('width',618);
			}

			if($(this).find('param[name=wmode]').length == 0)
			{
				$(this).append("<param name=\"wmode\" value=\"transparent\"></param>");
				$(this).find('embed').attr("wmode","transparent");
			}			

		});
	}
}

// ******************************************************************************************************************
// THIS MAKES SURE THE PAGE IS SCROLLED TO THE PREVIOUS POSITION WHEN GOING BACK

function initScrollHistory()
{
	$(window).scroll(
		function()
		{
			var this_scroll = $(window).scrollTop();
			$.cookie('hkazan_pagesNscrolls',this_scroll,{'path':'/'});
		}
	);
};

var first_scroll = false;
var first_scroll_top = 0;

function getLastScrollPositionAndSetCurPage()
{
	var cur_page = $.cookie('hkazan_curPage');
	var last_scroll = $.cookie('hkazan_pagesNscrolls');
	first_scroll = true;
	first_scroll_top = last_scroll;
	
	if(cur_page == window.location.href)
	{
		window.scrollTo(0,last_scroll);
	}
	$.cookie('hkazan_curPage',window.location.href,{'path':'/'});
};

// ============================================================================================================================================
// ADD THIS

var addthis_config = { ui_click:true, services_compact:'twitter,facebook,tumblr,stumbleupon,favorites,more' };

// ============================================================================================================================================
// SELECTIVE SCROLL

function initSelectiveScroll()
{
	// scroll only the images on the side and stop the details panel when it's bottom reaches the bottom of the page
	var detail_panel_h = $('.detail_panel').height();
	var offset_for_header_n_footer = 154;
	$(window).scroll(
		function()
		{
			detail_panel_h = $('.detail_panel').height();
			var chooser_h = $('#chooser').height();
			if(pageDetailBtmReached() && chooser_h > detail_panel_h)
			{
				if(detail_panel_h <= $(window).height()-offset_for_header_n_footer)	{ $('.detail_panel').addClass('detail_panel_fix_to_top'); }
				else																{ $('.detail_panel').addClass('detail_panel_fix_to_btm'); }
			}
			else
			{
				$('.detail_panel').removeClass('detail_panel_fix_to_top').removeClass('detail_panel_fix_to_btm');
			}
		}
	);
	
	function pageDetailBtmReached() 
	{
		var scroll_pos = ($(window).height()-offset_for_header_n_footer) + $(window).scrollTop();
		
		if(scroll_pos >= detail_panel_h || detail_panel_h <= $(window).height()-offset_for_header_n_footer)	{ return true; }
		else 																								{ return false;	}
	};
};

// ============================================================================================================================================
// FANCYBOX FOR DETAIL IMAGE

function initLightboxForDetailImage()
{
	var href = $('#detail_img').attr('src');
	
	var imgw = $('#detail_img').attr('width');
	var imgh = $('#detail_img').attr('height');
	
	var winw = $(window).width()-40;
	var winh = $(window).height()-40;
	
	var trgt_size = scaleSize(winw, winh, imgw, imgh, true);
	
	$('#detail_img').colorbox({width:trgt_size[0]-23, height:trgt_size[1], href:href, transition:"none"});
};

// ============================================================================================================================================
// COLUMNIZER

function initColumnizer()
{
	$('.landscape .page_detail_text').columnize({width:436, lastNeverTallest:true});
};

// ============================================================================================================================================
// LAZY LOAD

function initLazyLoad()
{
	$("img").lazyload({ 
		placeholder : "_images/transparent_white.png",
		effect : "fadeIn"
	});
};

// ============================================================================================================================================
// SEARCH PAGE

function initPageSearch()
{
	$('#search_txtbox').keypress(function(event){ if (event.keyCode == '13'){ searchPage(); } });
	$('#search_btn').click(function(){ searchPage(); });
};

function searchPage()
{
	var found = false;
	
	var img_divs = $('.img_with_caption').each(
		function()
		{
			$(this).removeClass('search_found');
			
			var term = $('#search_txtbox').attr('value').toLowerCase();
			var img_alt = $(this).find('img').attr('alt').toLowerCase();
			var caption = $(this).find('.boxcaption').text().toLowerCase();
			
			if(img_alt.indexOf(term) != -1 || caption.indexOf(term) != -1)
			{
				found = true;
				$(this).addClass('search_found');
			}
		}
	);
	
	if(found)
	{
		var first_elm_pos = $('.search_found:first').offset();
		$(window).scrollTo({top:first_elm_pos.top-108, left:0}, 800);
	}
	else
	{
		$('#search_txtbox').attr('value','Not found');
	}
	
};

// ============================================================================================================================================
// IMAGE CAPTIONS

function initImgCaptions()
{
	$("div.img_with_caption").each(
		function()
		{
			var img_w = $(this).find('img').width() + 2;
			
			$(this).find('div.boxcaption').css({'width':img_w,'max-width':img_w});
		}
	);
	
	// hover intent config
	var config = {    
	     sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
	     interval: 200, // number = milliseconds for onMouseOver polling interval    
	     over: openCaption, // function = onMouseOver callback (REQUIRED)    
	     timeout: 500, // number = milliseconds delay before onMouseOut    
	     out: closeCaption // function = onMouseOut callback (REQUIRED)    
	};
	
	function openCaption()
	{
		var hidden_y_pos = $('.boxcaption_home', this).length > 0 ? 501 : 155;
		
		console.log("hidden_y_pos: "+hidden_y_pos)
		
		if($('div.boxcaption', this).length > 0)
		{
			var cap_h = $('div.boxcaption', this).height();
			
			var cap_pad_top = $('div.boxcaption', this).css('padding-top');
			var cap_pad_top_nr = cap_pad_top.substr(0,cap_pad_top.indexOf('px'));
			
			var cap_pad_btm = $('div.boxcaption', this).css('padding-bottom');
			var cap_pad_btm_nr = cap_pad_btm.substr(0,cap_pad_btm.indexOf('px'));					
			
			var cap_open_pos = hidden_y_pos - (Number(cap_h) + Number(cap_pad_top_nr) + Number(cap_pad_btm_nr)) + 1; 
			$('div.boxcaption', this).stop().animate({top:cap_open_pos+'px'},{queue:false,duration:160})
		}
	};
	
	function closeCaption()
	{ 
		var hidden_y_pos = $('.boxcaption_home', this).length > 0 ? 501 : 155;
		$('div.boxcaption', this).stop().animate({top:hidden_y_pos+'px'},{queue:false,duration:160}) 
	};
	
	$('.img_with_caption').hoverIntent(config);
	
};

// ============================================================================================================================================
// NAVIGATION

function initNavigation()
{
	// hover intent config
	var config = {    
	     sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
	     interval: 200, // number = milliseconds for onMouseOver polling interval    
	     over: openMenu, // function = onMouseOver callback (REQUIRED)    
	     timeout: 200, // number = milliseconds delay before onMouseOut    
	     out: closeMenu // function = onMouseOut callback (REQUIRED)    
	};

	function openMenu()
	{
		var frm = $(this).find('div.submenu_frm');
		var ul = $(this).find('ul.submenu');
		
		if($(frm).attr('rel') == ''){ $(frm).attr('rel',$(ul).width()); }
		var menu_w = $(frm).attr('rel');
		
		$(ul).css({'display':'block','top':'-5px'});

		var win_h = $(window).height();
	
		frm.css({'display':'block','position':'absolute','width':(menu_w+'px'),'height':((win_h-109)+'px')}); // 99 = header height + 10 spacing
		
		$('.submenu_frm').mousemove(
			function(e)
			{
				var frm_h = $(this).height();
				var menu_h = $('ul',this).height();
				
				if(menu_h > frm_h)
				{
					var diff = menu_h - frm_h;
					var diff_1perc = diff / 100;
					var frm_h_1perc = frm_h / 100;
					
					var mouse_y = e.pageY; // 99 = height of header
					var menu_y = ((mouse_y / frm_h_1perc) * diff_1perc)-(45-18);
					$(this).find('ul').css('top',-menu_y);
				}
			}
		);
	}
	function closeMenu(){ $('ul', this).css('display', 'none'); $(this).find('div.submenu_frm').css('display', 'none'); }
	//function openMenu(){ $('ul', this).css('display', 'block'); }
	//function closeMenu(){ $('ul', this).css('display', 'none'); }

	$('#menu li.submenu_wrapper').hoverIntent(config);

	var config_client_highlight = {    
	     sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
	     interval: 200, // number = milliseconds for onMouseOver polling interval    
	     over: clientOver, // function = onMouseOver callback (REQUIRED)    
	     timeout: 500, // number = milliseconds delay before onMouseOut    
	     out: function(){} // function = onMouseOut callback (REQUIRED)    
	};
	
	// CLIENTLIST
	function clientOver()
	{
		var btn_title = $(this).html();
		$('#page_section img').each(
			function()
			{
				if($(this).attr('alt') == btn_title){ $(this).addClass('selected_image'); }
				else								{ $(this).removeClass('selected_image'); }
			}
		);
	};

	$('ul.submenu li a').hoverIntent(config_client_highlight);
	$('ul.submenu li a').hover(function(){$('#page_section img').each(function(){ $(this).removeClass('selected_image'); }); });
};

// Reset navigation buttons and images
function resetNavBtnsAndImgs(cur_active_btn_id)
{
	if($(this).attr('id') != "clientlist_btn"){ $("#clientlist").animate({height:'hide'}); }
	
	$('#navigation li').each(
		function()
		{
			if($(this).attr('id') != cur_active_btn_id)
			{
				$(this).removeClass('navigation_selected');
			}
		}
	);

	$('#page_section img').each( function(){ $(this).removeClass('selected_image'); } );
};

var clientlist_is_open = false;

// ============================================================================================================================================
// NEWS SLIDES

var lis = new Array();
var cur_lis_inx = 0;

function initNewsSlides()
{
	$('#news_ticker').click(
		function()
		{
			window.location.href = "index.php?page=News&h=_Home";
		}
	);
	
	if($('#news_ticker').length > 0)
	{
		var top_img_w = 0;
		$('#top_imgs_row img').each(
			function()
			{
				top_img_w += $(this).attr('width')+18;
			}
		);
		var nt_w = 1024-(top_img_w+9);
		$('#news_ticker').css('width',nt_w+'px');
			
		$.ajax(
		{
			url:"_php/__frontend/_home/get_blog_imgs.php", 
			success:function(data)
			{
				var ul = $(data);
				$('#news_ticker').css('line-height',0);
				$('#news_ticker').text('');
				$('#news_ticker').append(ul);
				$('#news_slides li').each(function(){ lis.push($(this)); });
				newSlide();
			}
	 	});
	}
};

function newSlide()
{
	var window_w = $('#news_ticker').width();

	var cur_li = lis[cur_lis_inx];
	
	var cur_w = 0;
	for(var a=0; a<cur_lis_inx+1; a++)
	{
		cur_w -= lis[a].width();
	}
	
	var cur_left = parseInt($('#news_slides').css('left'));
	
	if(cur_left - window_w < cur_w)
	{
		var cur_li_cloned = cur_li.clone();
		$('#news_slides').append(cur_li_cloned);
		lis = new Array();
		$('#news_slides li').each(function(){ lis.push($(this)); });
		cur_lis_inx = cur_lis_inx < lis.length-1 ? cur_lis_inx+1 : 0;
	}
	
	setTimeout(function(){
		var next_left = parseInt($('#news_slides').css('left')) - 1;
		$('#news_slides').css('left',next_left+'px');
		newSlide();
	}, 25);
};

// ============================================================================================================================================
// BIG IMAGE

function initBigImage()
{
	if($('.big_img img').length != 0)
	{
		$(window).resize(
			function()
			{
				var top_space = parseInt($('#navigation').css('top')) + parseInt($('#navigation').height()) + 18 + parseInt($('#top_imgs_row').height()) + 18;
				var btm_space = parseInt($('#footer').height());
				var size = scaleSize(1024, $(window).height()-(top_space+btm_space), $('.big_img img').width(), $('.big_img img').height());
				var margin_left = (1024 - size[0]) / 2;
				$('.big_img img').css({'width':size[0],'height':size[1],'margin-left':margin_left});
			}
		);
		$(window).resize();
	}
};

// ============================================================================================================================================
// HELPER FUNCTIONS

function scaleSize(winW, winH, imgW, imgH, dont_scale_up)
{
	var ratio = imgH / imgW; 
	var returnW; 
	var returnH; 
	var win_w = winW; 
	var win_h = winH; 

	returnW = dont_scale_up && win_w > imgW ? imgW : win_w; 
	returnH = returnW * ratio; 
	
	if(returnH > win_h)
	{
		returnH = dont_scale_up && win_h > imgH ? imgH : win_h; 
		returnW = returnH / ratio; 
	}
	
	return [Math.round(returnW), Math.round(returnH)]; 
};

// ============================================================================================================================================


