/*
 *
 * Layout
 *
 */
$(document).ready(function() {
	
	// Display banner and cookie insertion on banner removal
	if(!$.cookie('DeskProBanner') && $('.productBanner')) $('.productBanner').removeClass('noDisplay');
	else $('productBanner').remove();
	$('.productBanner .removeLink').click(function() {
		$.cookie('DeskProBanner', 'DeskPro product banner', {path: '/', expires: 365});
		$('.productBanner').remove();
	});

	if($('#combo').length) {
		// Combo
		$('#combo').cycle({
			fx: 'scrollHorz',
			speed: 300,
			timeout: 0,
			easing: 'backinout',
			nowrap: 1,
			pager: '#combonav',
			startingSlide: 0,
			pagerAnchorBuilder: function(idx, slide) {
				// return selector string for existing anchor
				return '#combonav li:eq(' + idx + ') a';
			},
			pagerClick: function(slide, element) {
				$('#combonav li.active').removeAttr('class');
				$('#combonav li:eq(' + slide + ')').attr('class', 'active');
				scroll_vertical($('#combonav li:eq(' + slide + ') a').attr('href').replace('#', ''));
			},
			after: function() {
				$('.tabContent h2').css('visibility', 'hidden');
				sIFR.replace(metablack, {
				  selector: '.tabContent h2',
				  css: ['.sIFR-root {font-size: 36px; color: #ffffff; text-align: center;}', 'b {font-weight: bold;}'],
				  wmode: 'transparent',
					filters: {
						DropShadow: {
							hideObject:false,
							knockout: false,
							distance: 0.75,
							color: '#000000',
							strength: 0.75
						}
					}
				});
			}
		});
		scroll_vertical($('#combonav li:eq(' + 0 + ') a').attr('href').replace('#', ''));
	}
	// Quotes
	if($('#blockquote').length) {
		$('#blockquote').cycle({
			fx: 'scrollHorz',
			speed: 300,
			timeout: 0,
			easing: 'expoinout',
			next: '#quotes a.next',
			prev: '#quotes a.prev',
			prevNextClick: function(isNext, slide, element) {
				h = $('#blockquote div.quote:eq(' + slide + ')').height();
				$('#blockquote').css('height', h);
			}
		});
		// Fix Quotes initial height
		h = $('#blockquote div.quote:eq(0)').height();
		$('#blockquote').css('height', h);
	}
	
	// Clear text on focus
	$(':text.clearfocus').focus(function() {
		if ($(this).val().length > 0 && $(this).attr('title').length == 0) {
			$(this).attr('title', $(this).val());
			$(this).val('');
		}
	});
	$(':password.clearfocus').focus(function() {$(this).val('');});
	$('input[name=username]').blur(function() {if(!$(this).val()) $(this).val('Username');});
	$('input[name=password]').blur(function() {if(!$(this).val()) $(this).val('password');});
	$('input[name=q]').blur(function() {if(!$(this).val()) $(this).val('Type search terms...');});
	$('input[name=email]').blur(function() {if(!$(this).val()) $(this).val('Your Email');});
	
	// Login form roll out & roll in
	$('a[id=loginTrigger]').toggle(
		function() { $('#login form').slideDown('fast'); return false; },
		function() { $('#login form').slideUp('fast'); return false;}
	);
	
	// Site Nav roll out & roll in
	$('.siteNavLink').toggle(
		function() { $('#siteMap').css('display', 'none').removeClass('noShow').slideDown('fast'); return false; },
		function() {$('#siteMap').slideUp('fast', function() {$('#siteMap').addClass('noShow'); return false; })}
	);
	
	// Popup show
	$("a[class=popupTrigger]").click(function(){
		$('.popup').addClass('noShow');
		$(this).siblings('.popup').removeClass('noShow');
		return false;
    });
	$('.popup').click(function(e) {e.stopPropagation();});
	$(document).click(function() {$('.popup').addClass('noShow');});
	
	// Menu show
	$("a[class=menu]").hover(
		function() { $(this).siblings('.subNav').removeClass('noShow'); },
		function() { $(this).siblings('.subNav').addClass('noShow'); }
    );
	$(".subNav").hover(
		function() { $(this).removeClass('noShow'); },
		function() { $(this).addClass('noShow'); }
	);
	$('.subNav').hover(function(e) { e.stopPropagation(); }, function() {});
	$(document).click(function() { $('.subNav').addClass('noShow'); });

	// Scroll to the top
	$('#scrollup').click(function() { $('html, body').animate({scrollTop: 0}, 'fast'); });
	
	// Thumbnails
	dptheme_handlethumbs('.thumb, .thumbs, .featurethumb, .screenshots');
	
	// Videos
	if($('div#videoCarousel').length) {
		$('div#videoCarousel div.container div.items div.item:eq(0)').addClass('active')
		$('div#videoCarousel div.container div.items div.item img.thumbnail').css('opacity', .7);
		$('div#videoCarousel div.container div.items div.active img.thumbnail').css('opacity', 1);
		$('div#videoCarousel div.container div.items div.item').each(function() {
			var id = $('div#videoCarousel div.container div.items div.item').index(this);
			$(this).children('span.count').html(id + 1 + ' / ' + ($('div#videoCarousel div.container div.items div.item').size()));
		});
		$('div#videoCarousel .container').scrollable({
			size: 3,
			onSeek: function() {
				$('div#videoCarousel div.container div.items div.item img.thumbnail').css('opacity', 0.7);
				$('div#videoCarousel div.container div.items div.active img.thumbnail').css('opacity', 1);
				$('div.activeVideo').html($('div#videoCarousel div.container div.items div.active .content').html());
			}
		});
	}
});

function dptheme_handlethumbs(selector)
{
	var els = $(selector);
	if (!els.size()) return;
	
	var as = els.find('a');
	if (!as.size()) return;
	
	// Set up lightbox
	as.lightBox({containerResizeSpeed: 150});
}

// Combo scroll vertical
function scroll_vertical(page) {
	$('.' + page + ' .content').cycle({
		fx: 'scrollUp',
		speed: 150,
		timeout: 0,
		next: '.tabCombo .next',
		prev: '.tabCombo .prev',
		easing: 'expoinout',
		startingSlide: 0,
		pager: 'ul.' + page,
		pagerAnchorBuilder: function(idx, slide) {
			// return selector string for existing anchor
			return 'ul.' + page + ' li:eq(' + idx + ') a';
		},
		prevNextClick: function(isNext, slide, element) {
			setTimeout('toolbar("' + page + '")', 0);
		},
		pagerClick: function(slide, element) {
			setTimeout('toolbar("' + page + '")', 0);
		},
		after: function() {
			sIFR.replace(metablack, {
			  selector: '.tabContent h2',
			  css: ['.sIFR-root {font-size: 36px; color: #ffffff; text-align: center;}', 'b {font-weight: bold;}'],
			  wmode: 'transparent',
				filters: {
					DropShadow: {
						hideObject:false,
						knockout: false,
						distance: 0.75,
						color: '#000000',
						strength: 0.75
					}
				}
			});
			setTimeout('toolbar("' + page + '")', 0);

			$('#combo .tabCombo').not('.'+page).hide();
		}
	});
}
function toolbar(page) {
	$('ul.' + page + ' li.active').removeClass('active');
	$('ul.' + page + ' li a.activeSlide').parent().attr('class', 'active');
}

/*
 *
 * Cookie extension for cookie manipulation
 *
 */
jQuery.cookie = function(name, value, options) {
	// name and value given, set cookie
    if (typeof value != 'undefined') {
        options = options || {};
        if (value === null) {
            value = '';
            // Clone object because of unexpected behavior if expired property was changed
            options = $.extend({}, options);
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            // Use 'expires' attribute because 'max-age' is not supported by IE
            expires = '; expires=' + date.toUTCString();
        }
        // Parenthesize options.path and options.domain or they will evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    }
    // Only name given, get cookie
    else {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
