//
// latina cams specific js functions
//

$(document).ready(function() {

	//
	// close any waiting page
	//

	$.unblockUI();

	//
	// setup blockui for ajax requests
	//

	//$(document).ajaxStart($.blockUI.ajaxStop($.unblockUI);

	//
	// setup ajax from the quick search form
	//

	/*
    $('#quick_search').ajaxForm({
        target:        '#content'
	}); 
	*/

	//
	// change the actual query
	//

	$('[name=ajax]').val(1); 

	//
	// turn static links into ajax links
	//

	create_ajax_links();

	//
	// to open waiting page during the search
	//

    $('#quick_search').ajaxForm({
        target:        '#content',
        beforeSubmit: function() {
			/*
			$.blockUI({ 
				message: 'searching...',
				css: { 
					border: 'none', 
					padding: '15px', 
					backgroundColor: '#000', 
					'-webkit-border-radius': '10px', 
					'-moz-border-radius': '10px', 
					opacity: .5, 
					color: '#fff' 
				} 
			}) 
			*/
		},
        success: $.unblockUI 
	}); 

	//
	// init the history plugin
	//

	/*
    $.history.init(history_callback);
    $("a[@rel='history']").click(function(){
        $.history.load(this.href.replace(/^.*#/, ''));
		history_callback();
        return false;
    });
	*/

});

/*
function history_callback(hash)
{
		// hash doesn't contain the first # character.
		if(hash) {
			// restore ajax loaded state
			$("#load").load(hash + ".html");
		} else {
			// start page
			$("#load").empty();
		}
}
*/

function create_ajax_links()
{
	//
	// convert navigation links into ajax
	//

	$(".grid_nav").each(function() {
		$(this).click(function() {

			var nav_link =$(this).attr('href');

			if (nav_link != '#')
			{
				$(this).attr('href', '#');

				/*
				$.blockUI({ 	
					message: 'searching...',
					css: { 
						border: 'none', 
						padding: '15px', 
						backgroundColor: '#000', 
						'-webkit-border-radius': '10px', 
						'-moz-border-radius': '10px', 
						opacity: .5, 
						color: '#fff' 
					} 
				});
				*/

				$.ajax({
					url: nav_link + '&ajax=1',
					success: function(html) { 
						$("div#content").html(html);
        				$.unblockUI;
					}
				});
			}
		});
	});

	//
	// convert all profile links to ajax
	//

	/*
	$(".profile_link").each(function() {

		//
		// need to extract the real url so we can append ajax=1
		// from the seo style url
		//
		// /HotStuff-from-mycams/ => /?seoname=HotStuff&site=mycams&ajax=1
		//

		$(this).click(function() {
			var match = /([\w\-\.\_]+)\-from\-([\w\.]+)/.exec($(this).attr('href'));
			$(this).attr('href', '#');

			$.ajax({
				url: '/?action=profile&ajax=1&seoname=' + match[1] + '&site=' + match[2],
				success: function(html) { 
					$("div#content").html(html);
				}
			});
		});
	});
	*/
}

function set_last_search(spec, skip)
{
	//
	// if we have come from a profile page reajax the correct content
	//

	//alert("in set_last_search() COOKIES search=" + $.cookie('last_search') + ' from_profile=' + $.cookie('from_profile'));
	if ($.cookie('from_profile') == 1)
	{
		search_spec = $.cookie('last_search');

		//
		// reset the cookies
		//

		$.cookie('from_profile', '0', { path: '/' });
		$.cookie('last_search', '', { path: '/' });

		//
		// ajax the correct content
		//

		$('div#content').css({ display: 'none' });

		$.ajax({
			url: '/index.php?action=search&ajax=1&' + search_spec,
			success: function(html) { 
				$("div#content").html(html);
				$('div#content').css({ display: 'block' });
			}
		});

	}
	else {
		var last_search = 'skip=' + skip;
		if (spec != '') last_search = spec + '&' + last_search;
		$.cookie('last_search', last_search, { path: '/' });
	}
}

function view_page(url)
{
	/*
	$.blockUI({ 
		message: 'searching...',
		css: { 
			border: 'none', 
			padding: '15px', 
			backgroundColor: '#000', 
			'-webkit-border-radius': '10px', 
			'-moz-border-radius': '10px', 
			opacity: .5, 
			color: '#fff' 
		} 
	});
	*/

	$.ajax({
		url: url + '&ajax=1',
		success: function(html) { 
			$("div#content").html(html);
		}
	});
}

function view_profile(site, model)
{
	$.ajax({
		url: '/index.php?action=profile&ajax=1&site=' + site + '&seoname=' + model,
		success: function(html) { 
			$("div#content").html(html);
		}
	});
}

function go(url)
{
	window.open(url);
}

function view_online_models()
{
/*
	$.blockUI({ 
	message: 'searching...',
	css: { 
		border: 'none', 
		padding: '15px', 
		backgroundColor: '#000', 
		'-webkit-border-radius': '10px', 
		'-moz-border-radius': '10px', 
		opacity: .5, 
		color: '#fff' 
	} });

	document.location = '/';
*/

/*
	$.ajax({
		url: '/index.php?ajax=1',
		success: function(html) { 
			$("div#content").html(html);
        	$.unblockUI;
		}
	});
*/
}

function add_bookmark(title, url)
{
    if (window.sidebar)
	{
		// firefox
        window.sidebar.addPanel(title, url, "");
	}
    else if(window.opera && window.print)
	{ 
		// opera
        var elem = document.createElement('a');
        elem.setAttribute('href',url);
        elem.setAttribute('title',title);
        elem.setAttribute('rel','sidebar');
        elem.click();
    } 
    else if(document.all)
	{
		// ie
        window.external.AddFavorite(url, title);
	}
}

