// jquery.cookie.min.js
jQuery.cookie=function(name,value,options){if(typeof value!='undefined'){options=options||{};if(value===null){value='';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;}
expires='; expires='+date.toUTCString();}
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('');}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]);if(cookie.substring(0,name.length+1)==(name+'=')){cookieValue=decodeURIComponent(cookie.substring(name.length+1));break;}}}
return cookieValue;}};

$(document).ready(function(){

    // open links in external window
    externalLinks();
    internalLinks();

    //funwith fab
    reset();
    var val;

    // jquery accordion panels Tour 2010
    $(".accordion div.acc-top:first");
    $(".accordion div.acc-btm").hide();

    $(".accordion div.acc-top").click(function(){
        $(this).next("div").slideToggle("800")
        .siblings("div.acc-btm:visible").slideUp("800");
        $(this).toggleClass("open");
        $(this).siblings("div.acc-top").removeClass("open");
    });
    $('div.acc-top').css( 'cursor', 'pointer' );

    // Vote
    var c = '';
    if ($.cookie('myfabland')) {
        c = $.cookie('myfabland');
    }
    $('.vote').each(function(index) {
        var id = $(this).find('a').attr('id').toString().replace('v', '');
        $(this).bind('click', function(event){
            event.preventDefault();
            if (c.indexOf('v'+id) != -1 &&
                parseInt(c.substring(c.indexOf('v'+id+'n')+('v'+id+'n').length, c.indexOf('v'+id+'n')+('v'+id+'n').length+1)) == 5) {
                //alert('Maximum number of votes has been reached!');
				jAlert('Maximum number of votes has been reached!');
            } else {
                $.ajax({
                    type: 'post',
                    url: '/design-a-den/vote_ajax.php',
                    data: 'id='+id,
                    success: function(resp){
                        if (c.indexOf('v'+id) == -1) {
							num = 0;
                            c = c + 'v' + id + 'n1';
                        } else {
                            num = c.substring(c.indexOf('v'+id+'n')+('v'+id+'n').length, c.indexOf('v'+id+'n')+('v'+id+'n').length+1);
                            c = c.replace('v'+id+'n'+num, 'v'+id+'n'+(parseInt(num)+1));
                        }
						//alert('Thank you! You have ' + parseInt(5 - num - 1) + ' votes left.');
						jAlert('Thank you! You have ' + parseInt(5 - num - 1) + ' votes left.', '');
                        $.cookie('myfabland', c, {
                            path: '/',
                            expires: 365
                        });
                    }
                });
            }
        });
    });

	// Share
	$('.share').each(function(index) {
		var id = $(this).find('a').attr('id').toString().replace('s', '');
		$(this).colorbox({
			innerWidth: '500px',
			innerHeight: '452px',
			iframe: true,
			href: 'share.php?id='+id
		});
	});
  
	// Textarea maxlength
	/*var onEditCallback = function(remaining){
		$(this).siblings('.charsRemaining').text("Characters remaining: " + remaining);
	    if(remaining > 0){
			$(this).css('background-color', 'white');
		}
    }
    var onLimitCallback = function(){
		$(this).css('background-color', 'red');
    }
    $('textarea[maxlength]').limitMaxlength({
		onEdit: onEditCallback,
	    onLimit: onLimitCallback,
    });*/

});

function reload1(form){
    reset();
    val=form.numberChildren.options[form.numberChildren.options.selectedIndex].value;

    if (val==2) {
        $('div#child-2').show();
        $('div#childage-2').show();
    };
    if (val==3) {
        $('div#child-2').show();
        $('div#childage-2').show();
        $('div#child-3').show();
        $('div#childage-3').show();
    };
    if (val==4  || val== '4+') {
        $('div#child-2').show();
        $('div#childage-2').show();
        $('div#child-3').show();
        $('div#childage-3').show();
        $('div#child-4').show();
        $('div#childage-4').show();
    };
}

function reset(){
        $('div#child-2').hide();
        $('div#child-3').hide();
        $('div#child-4').hide();

        $('div#childage-2').hide();
        $('div#childage-3').hide();
        $('div#childage-4').hide();
    }

function externalLinks() {
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for (var i=0; i<anchors.length; i++) {  var anchor = anchors[i];
    if (anchor.getAttribute("href") &&  anchor.getAttribute("rel") == "external")  anchor.target = "_blank";
    }
};

function internalLinks() {
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for (var i=0; i<anchors.length; i++) {  var anchor = anchors[i];
    if (anchor.getAttribute("href") &&  anchor.getAttribute("rel") == "internal")  anchor.target = "_self";
    }
};

// lightbox
$(function() {
    $('.gallery a').lightBox();
});
$(function() {
    $('.gallery2 a').lightBox();
});
$(function() {
    $('.gallery3 a').lightBox();
});
