/* some func */
function in_array(needle, haystack, argStrict) {
    var found = false, key, strict = !!argStrict;

    for (key in haystack) {
        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
            found = true;
            break;
        }
    }

    return found;
}

Cufon.replace('h1', {textShadow: '#222 1px 1px', separate: 'words', hover: true, hoverables: {h1: true}});
var albumsCache = new Array();

frameHeight = 358;

$(function(){
    initTabs($('.tabs'));
    initParts($('.parts'));
    initFAQ($('.faq'));
    //$('.media-photo a').fancybox({padding: 0});
    $('.media-video a').fancybox({padding: 0, frameWidth: 546, frameHeight: frameHeight, callbackOnStart: fancyVideoStart, callbackOnClose: fancyVideoClose, hideOnContentClick: false});
    $('.feedback .submit').hover(
        function(){$(this).addClass('hover');},
        function(){$(this).removeClass('hover');}
    );
	var albCount = 0;
	$('a.albLink', $('#albums')).click(function() {
		var secID = $(this).attr('rel');
		if(in_array(secID, albumsCache)) {
			
			var pBox = $('#p'+secID);
			$('a',pBox).fancybox();
			$('.firstP',pBox).click()
						
			return false;
		}
        $.post("/inc/request.php", {
            sid: secID,
            act: 'getAlbum'
        }, function(data){
			albumsCache[albCount] = secID;
			albCount++;
			$('#hidePhotos').append(data);
			var pBox = $('#p'+secID);
			$('a',pBox).fancybox();
			$('.firstP',pBox).click()
        });
		
		
		 
		return false;  
		});
		
});

function fancyVideoStart() {
    $('#fancy_bg').css({'z-index': 70});
    $('#fancy_close').css({'top': -21, 'right': -21});
    $('.fancy_bg_nw').css({'top': -50, 'left': -50});
    $('.fancy_bg_n').css({'top': -50, 'left': 15, 'right': 15}).width(516);
    $('.fancy_bg_ne').css({'top': -50, 'right': -50});
    $('.fancy_bg_e').css({'right': -50, 'top': 15, 'bottom': 15}).height(frameHeight-20);//was 288
    $('.fancy_bg_se').css({'bottom': -50, 'right': -50});
    $('.fancy_bg_s').css({'bottom': -50, 'left': 15, 'right': 15}).width(516);
    $('.fancy_bg_sw').css({'bottom': -50, 'left': -50});
    $('.fancy_bg_w').css({'left': -50, 'top': 15, 'bottom': 15}).height(frameHeight-20); //was 288
}


function fancyVideoClose() {
    $('#fancy_bg').css({'z-index': 101});
    $('#fancy_close').css({'top': -25, 'right': -25});
    $('.fancy_bg_nw').css({'top': -54, 'left': -54});
    $('.fancy_bg_n').css({'top': -54, 'left': 11, 'right': 11}).width('auto');
    $('.fancy_bg_ne').css({'top': -54, 'right': -54});
    $('.fancy_bg_e').css({'right': -54, 'top': 11, 'bottom': 11}).height('auto');
    $('.fancy_bg_se').css({'bottom': -54, 'right': -54});
    $('.fancy_bg_s').css({'bottom': -54, 'left': 11, 'right': 11}).width('auto');
    $('.fancy_bg_sw').css({'bottom': -54, 'left': -54});
    $('.fancy_bg_w').css({'left': -54, 'top': 11, 'bottom': 11}).height('auto');
}


function initFAQ(obj) {
    var faqList = obj;
    var faq = $('a', faqList);
    var ansList = faqList.next();
    var ans = $('.i', ansList);

    ans.hide();

    faq.click(function(){
        var id = $.url.setUrl(this.href).attr('anchor');
        faq.removeClass();
        $(this).addClass('a').blur();
        if (isPngFix()) {
            faq.each(function(){
                var q = this;
                setTimeout(function(){
                    DD_belatedPNG.applyVML(q);
                }, 1);
            });
        }
        ans.not('#'+id).slideUp('fast');
        ans.filter('#'+id+':invisible').slideDown('fast');
        return false;
    });

    if ($('#' + jQuery.url.attr('anchor')).length) {
        $('a[href="#'+jQuery.url.attr('anchor')+'"]', faqList).trigger('click');
    }
}


function initTabs(obj) {
    obj.each(function(){
        var tabs = $('h1', this);
        var tabContent = $('.tabcontent', $(this).next('.rel'));
		var tabImages = $('.tabimages .tabcontent');
        tabs.hover(
            function () {
                var tab = $(this);
                if (tab.hasClass('c')) {
                    tab.addClass('hover');
                    if (isPngFix()) {
                        var tabDOM = this;
                        setTimeout(function(){
                            DD_belatedPNG.applyVML(tabDOM);
                        }, 1);
                    }
                }
            },
            function () {
                var tab = $(this);
                if (tab.hasClass('c')) {
                    tab.removeClass('hover');
                    if (isPngFix()) {
                        var tabDOM = this;
                        setTimeout(function(){
                            DD_belatedPNG.applyVML(tabDOM);
                        }, 1);
                    }
                }
            }
        ).click(function(){
            var tab = $(this);
            if (tab.hasClass('c')) {
                var index = tabs.index(tab);
                tabs.addClass('c');
                tab.removeClass();
                Cufon.refresh('h1');
                tabContent.filter(':visible').css('position', 'absolute').hide();
                tabContent.eq(index).css('position', 'static').show();
                tabImages.filter(':visible').css('position', 'absolute').hide();
                tabImages.eq(index).css('position', 'static').show();
				
            }
        });
    });
}


function initParts(obj) {
    obj.each(function(i){
        var parts = $(this);
        var descrs = $('.i', parts.next());
        var inputs = initRadios(parts);

        if (isPngFix()) {
            $('label', parts).hover(
                function(){
                    $(this).css('background-position', '0 0');
                },
                function(){
                    $(this).css('background-position', '-9999px 0');
                }
            );
        }

        inputs.click(function(){
            var input = $(this);
            var index = inputs.index(input);
            descrs.not(':eq('+index+')').slideUp('fast');
            descrs.eq(index).filter(':invisible').slideDown('fast');
        });
    });
}


function initRadios(obj) {
    var inputs = $('input:radio', obj);
    inputs.each(function(){
        var input = $(this);
        var label = $('label[for="'+this.id+'"]');

        if (input.is(':checked')) {
            label.addClass('checked');
            if (isPngFix()) $('em', label).css('background-position', '0 -80px');
        } else {
            label.removeClass('checked');
            if (isPngFix()) $('em', label).css('background-position', '0 0');
        }

        input.click(function(){
            var input = $(this);
            var label = $('label[for="'+this.id+'"]');
            var sisters = inputs.filter('[name="'+input.attr('name')+'"]');

            sisters.each(function(){
                var input = $(this);
                var label = $('label[for="'+this.id+'"]');

                if (input.is(':checked')) {
                    label.addClass('checked');
                    if (isPngFix()) $('em', label).css('background-position', '0 -80px');
                } else {
                    label.removeClass('checked');
                    if (isPngFix()) $('em', label).css('background-position', '0 0');
                }
            });
        });
    });
    return inputs;
}


function initCheckboxes(obj) {
    var inputs = $('input:checkbox', obj);
    inputs.each(function(){
        var input = $(this);
        var label = $('label[for="'+this.id+'"]');

        if (input.is(':checked')) {
            label.addClass('checked');
            if (isPngFix()) $('em', label).css('background-position', '0 -80px');
        } else {
            label.removeClass('checked');
            if (isPngFix()) $('em', label).css('background-position', '0 0');
        }

        input.click(function(){
            var input = $(this);
            var label = $('label[for="'+this.id+'"]');
            if (input.is(':checked')) {
                label.addClass('checked');
                if (isPngFix()) $('em', label).css('background-position', '0 -80px');
            } else {
                label.removeClass('checked');
                if (isPngFix()) $('em', label).css('background-position', '0 0');
            }
        })
    });
    return inputs;
}


function isPngFix() {
    if (typeof(DD_belatedPNG) == 'object') return true;
    return false;
}

if (isPngFix()) {
    var hasPNG = '.png';
    hasPNG += ', #m1 li';
    hasPNG += ', .tabs h1.c';
    hasPNG += ', .link';
    hasPNG += ', .parts label, .parts label em';
    hasPNG += ', .media .i em';
    hasPNG += ', .faq a';
    hasPNG += ', .feedback .text, .feedback textarea, .feedback .submit span';
    DD_belatedPNG.fix(hasPNG);
}
