var savedHTML = '';
var savedHeight = 0;
var imageHTML = '';
var currPMADSelect = '';
var initPMADSelect = '';
var currPMADsite = ' ';
var initPMADsite = ' ';
var isImageLoading = false;
var imageToGo = undefined;//new Array();
var checkImagesInterval = 0;

function scrollDown() {
    $.scrollTo('#photo_select', 400, {
        axis:'y',
        offset:55
    });
}

function endLoading(bg,slide,thys,wp) {
    
    var arrows = $('#left_center_arrow,#right_center_arrow');
    if ($('#big_picture').width()>800) { $('#big_picture').width(800).css('height','auto'); }
    var h = $('#picture').innerHeight();
    if (bg) { bg.css('display','block'); }
    arrows.css({'margin':'0px'}).fadeOut(100);

    $('div#center_content').animate({opacity:1,height:h},500,'linear',function(){
        var t = $('#big_picture').position().top+($('#big_picture').height()/2);
        $(this).css('filter','');
        if (bg) { bg.css({'background-position':'0px -31px'}); }
        arrows.css({'top':t+'px'}).fadeIn('fast');
        if (slide) {
            $('div#photo_select').slideDown(500,function(){
                scrollDown();
            });
        } else {
            scrollDown();
        }
        isImageLoading = false;
    });

    if (thys.parents('div.archive_row').length) {
        $('#mycarousel li img:first-child').css('border','solid white 2px').get(wp).style.border = '2px solid #a84484';
    } else if (thys.parents('#mycarousel').length) {
        $('#mycarousel li img:first-child').css('border','solid white 2px');
        thys.children('img:first').css({'border':'2px solid #a84484'});
    }
}

function loadContent() { // START loadContent

    var sta = $('div#select_title a.sta');
    var sti = $('div#select_title span.sti');
    var img = new Image();
    var clicked_link = imageToGo;

    var wp = getIndex(clicked_link);

    $('div#center_content').html('<div id="loading_graphic" style="width: 100%; text-align: center; line-height: 200px; padding-top: 75px;"><img src="/design/dailydolce_des/images/ajax-loader.gif" alt="loading graphic"/></div>').animate({opacity:1},300,'linear');

    $(img).load(function(){

        var imag = $(this);

        $('div#center_content').animate({opacity:0},300,'linear',function(){ // START cc animation callback
            $(this).empty().append(imageHTML.remove().css({'display':'block'})).prepend('<a id="left_center_arrow" href="#"></a><a id="right_center_arrow" href="#"></a>');
            $('#big_picture').replaceWith(imag);

            var info = ['tag_photog','tag_agency','tag_designer','tag_model'];
            $.each(info,function(){
                var ih = '', ta = clicked_link.attr(this+'');
                if (ta.toLowerCase()!='unknown') {
                    if (this+''=='tag_photog') {
                        ih = '<a title="See everything from '+ta+'" href="/photographers/'+encodeURIComponent(ta.toLowerCase())+'">'+ta+'</a>';
                    } else if (this+''=='tag_agency') {
                        ih = '<a title="See everything from '+ta+'" href="/agencies/'+encodeURIComponent(ta.toLowerCase())+'">'+ta+'</a>';
                    } else if (this+''=='tag_designer') {
                        ih = '<a title="See everything from '+ta+'" href="/designers/'+encodeURIComponent(ta.toLowerCase())+'">'+ta+'</a>';
                    } else if (this+''=='tag_model') {
                        ih = '<a title="See everything from '+ta+'" href="/models/'+encodeURIComponent(ta.toLowerCase())+'">'+ta+'</a>';
                    } else {
                        ih = ta;
                    }
                } else {
                    ih = ta;
                }
                $('.'+this).html(ih);
            });

            if (sta.length) {
                $('.photographer_cname').text(clicked_link.attr('tag_photog'));
                if (clicked_link.attr('photog_url').indexOf('http://')==0) {
                    $('.photographer_site').attr('href',clicked_link.attr('photog_url')).
                        text(clicked_link.attr('photog_url').substring(7));
                } else {
                    $('.photographer_site').attr('href','#').html('');
                }

                $('.model_cname').text(clicked_link.attr('tag_model'));
                if (clicked_link.attr('model_url').indexOf('http://')==0) {
                    $('.model_site').attr('href',clicked_link.attr('model_url')).
                        text(clicked_link.attr('model_url').substring(7));
                } else {
                    $('.model_site').attr('href','#').html('');
                }

                $('.agency_cname').text(clicked_link.attr('tag_agency'));
                if (clicked_link.attr('agency_url').indexOf('http://')==0) {
                    $('.agency_site').attr('href',clicked_link.attr('agency_url')).
                        text(clicked_link.attr('agency_url').substring(7));
                } else {
                    $('.agency_site').attr('href','#').html('');
                }

                $('.designer_cname').text(clicked_link.attr('tag_designer'));
                if (clicked_link.attr('designer_url').indexOf('http://')==0) {
                    $('.designer_site').attr('href',clicked_link.attr('designer_url')).
                        text(clicked_link.attr('designer_url').substring(7));
                } else {
                    $('.designer_site').attr('href','#').html('');
                }

            }

            $("a#share1").attr('href','/soc/send?articleLink='+clicked_link.attr('href')+'&noframe=true').fancybox({
                'frameWidth': 760,
                'frameHeight': 400
            });


            $('a#share2').unbind().removeAttr('onmouseover').removeAttr('onmouseout').removeAttr('onclick').
                bind('mouseover',function(){
                    return addthis_open(this, '', clicked_link.attr('share_url'), clicked_link.attr('title'));
                }).bind('mouseout',function(){
                    addthis_close();
                }).bind('click',function(){
                    return addthis_sendto();
                });

            if (sta.length) {
                endLoading(sta,true,clicked_link,wp);
            } else if (sti.length) {
                endLoading(sti,false,clicked_link,wp);
            } else {
                endLoading(false,false,clicked_link,wp);
            }

        }); // END cc animation callback

    }).error(function(){

        alert('Sorry. There was a problem loading the image. Either reload the page or try clicking another image.');
        isImageLoading = false;

    }).each(function(){
        $(this).attr({'src':imageToGo.attr('big_href'),'id':'big_picture'});
    });

} // END loadContent

function checkImages() {
    var cc = $('div#center_content');
    if(cc.length && !isImageLoading) {
        isImageLoading = true;
        if ($('div#select_title a.sta').length) {
            $('div#photo_select').slideUp(500,function(){
                cc.animate({opacity:0,height:'200px'},500,'linear',loadContent);
            });
        } else {
            cc.animate({opacity:0,height:'200px'},500,'linear',loadContent);
        }
    }
}

$(function(){

    var sta = $('div#select_title a.sta');
    var sti = $('div#select_title span.sti');
    savedHTML = $('div#center_content').html();
    savedHeight = $('div#center_content').height();
    initPMADSelect = $('.photographer_cname, .model_cname, .agency_cname, .designer_cname').text();
    imageHTML = $('#picture');

    $('a.preview_pic').live("click",function(){

        if ($('div#center_content a.taglink').length || $('div#center_content div.archive_row').length) {
            savedHTML = $('div#center_content').html();
        }

        imageToGo = $(this);

        checkImages();

        return false;
    });
    
    if ($('#left_center_arrow,#right_center_arrow').length && $('#big_picture').parents('#center_content').length) {
        var t = $('#big_picture').position().top+($('#big_picture').height()/2);
        $('#left_center_arrow,#right_center_arrow').css('top',t+'px')
    }
    $('#left_center_arrow,#right_center_arrow').live('click',function(){
        	$('#ad_veil').css({'position':'static','top':'0px','height':'','width':''});
        	$('#panel_ad').css('display','none');        																	  
        var l=$('#mycarousel a.preview_pic');
        l.filter(':eq('+getArrowIndex($(this),l.length)+')').trigger('click');
        return false;
    });

    if (sta.length) {
        sta.click(function(){
            var cc = $('div#center_content');
            var tsh = '';
            if ($('div#center_content a.taglink').length || $('div#center_content div.archive_row').length) {
                savedHTML = $('div#center_content').html();
            }
            tsh = $(savedHTML);
            if ($('a.taglink').length) {
                currPMADSelect = $('a.taglink').filter(function() {
                                    return $(this).css('border');
                                }).attr('keyword');
            } else if (tsh.find('a.taglink').length) {
                currPMADSelect = tsh.find('a.taglink').filter(function() {
                                    return $(this).css('border');
                                }).attr('keyword');
            }

            function loadContent() {
                $('div#center_content').html(savedHTML);
                if (currPMADSelect) {
                    $('.photographer_cname, .model_cname, .agency_cname, .designer_cname').text(currPMADSelect);
                    $('.photographer_site, .model_site, .agency_site, .designer_site').text(currPMADsite.substring(7));
                } else {
                    $('.photographer_cname, .model_cname, .agency_cname, .designer_cname').text(initPMADSelect);
                    $('.photographer_site, .model_site, .agency_site, .designer_site').text(initPMADsite);
                }

                $('div#center_content').animate({opacity:1,height:savedHeight},500,'linear',function(){
                    sta.css({'background-position':'0px 0px'});
                    $('div#photo_select').slideDown(500);
                });
            }

            if(cc.length) {
                $('#mycarousel li img:first-child').css('border','solid white 2px');
                $('div#photo_select').slideUp(500,function(){
                    cc.animate({opacity:0,height:'200px'},500,'linear',loadContent);
                });
            }
            return false;
        });
    }

    if (sti.length) {
        sti.css('cursor','pointer');
        sti.click(function(){
            var cc = $('div#center_content');

            function loadContent() {
                $('div#center_content').html(savedHTML);
                $('div.archive_row a.preview_pic img:first-child').css('border','solid white 2px')
                $('div#center_content').animate({opacity:1,height:savedHeight},500,'linear',function(){
                    sti.css({'background-position':'0px 0px'});
                });
            }

            if(cc.length) {
                $('#mycarousel li img:first-child').css('border','solid white 2px');
                cc.animate({opacity:0,height:'200px'},500,'linear',loadContent);
            }
            return false;
        });
    }
});
