/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}

 $(function() {
    $('.copyembedcode:first').each(function() {
        $(this).find('a').click(function(){console.log('flash is not over this'); return false;})
        
        var clip = new ZeroClipboard.Client();
        clip.setHandCursor( true );

        clip.addEventListener('load', function (client) {
                //debugstr("Flash movie loaded and ready.");
        });

        clip.addEventListener('mouseOver', function (client) {
                // update the text on mouse over
                clip.setText($('textarea.embedsourcecode:visible').val());
        });

        clip.addEventListener('complete', function (client, text) {
                alert('Embed code is now copied in your clipboard!');
        });

        clip.glue($(this)[0]);
    });
});

function openMySpace(RAurl,RAtitle,RAcontent)
{
    window.location='http://home.myspace.com/'
        +'Modules/PostTo/Pages/'
        +'?u='+encodeURIComponent($.trim(RAurl))
        +'&t='+encodeURIComponent($.trim(RAtitle))
        +'&c='+encodeURIComponent($.trim(RAcontent.replace(new RegExp( "\\n", "g" ),'')).replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/</g,"&lt;").replace(/>/g,"&gt;"));
    return false;
}

function openWordpress(RAurl,RAtitle,RAcontent,RAshop)
{
    var savedWpUrl = getCookie('raSharingWordPressURL' && RAshop != '');
    if(typeof(savedWpUrl) == 'undefined' || savedWpUrl == '')
    {
        savedWpUrl = 'http://www.';
    }
    var wpUrl = prompt('What is the URL of your Wordpress site?', savedWpUrl);
    if(typeof(wpUrl) != 'undefined' && wpUrl != null && wpUrl != '' && wpUrl != 'http://www.'  && wpUrl != 'http://' && wpUrl.split('.').length > 1)
    {
        if(typeof(RAshop) != 'undefined')
        {
            $.ajax({url:'/com/redantenna/step/Step.action?shop='+RAshop+'&title=promote', success:function(){
                if($('#promote').length > 0 && !$('#promote').hasClass('done'))
                {
                    $('#promote').addClass('done');
                }
            }});
        }

        window.open(wpUrl+'/wp-admin/press-this.php'
            +'?u='+RAurl
            +'&t='+RAtitle
            +'&s='+encodeURIComponent($.trim(RAcontent.replace(new RegExp( "\\n", "g" ),'')).replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/</g,"&lt;").replace(/>/g,"&gt;"))
            +'&v=2'
        );
        setCookie('raSharingWordPressURL', wpUrl, 365);
    } else {
        alert('You haven\'t entered a valid URL');
    }
}

function openBlogger(RAurl,RAtitle,RAcontent, RAshop)
{
    if(typeof(RAshop) != 'undefined' && RAshop != '')
    {
        $.ajax({url:'/com/redantenna/step/Step.action?shop='+RAshop+'&title=promote', success:function(){
            if($('#promote').length > 0 && !$('#promote').hasClass('done'))
            {
                $('#promote').addClass('done');
            }
        }});
    }
    if($('#bloggerForm').length > 0){$('#bloggerForm').remove();}
    var hiddenFormForBlogger = $('<form method="post" class="hidden" id="bloggerForm" target="_blank" action="http://www.blogger.com/add-widget">'
            +'<input type="hidden" name="infoUrl" value="'+RAurl+'"/>'
            +'<input type="hidden" name="logoUrl" value="http://www.flickr.com/images/flickr_logo_gamma.gif"/>'
            +'<input type="hidden" name="widget.title" value="'+RAtitle+'"/>'
            +'<textarea name="widget.content" style="display:none;">'
                +RAcontent.replace(new RegExp( "\\n", "g" ),'').replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/\</g,"&lt;").replace(/\>/g,"&gt;")
            +'</textarea>'
            +'<input type="hidden" name="widget.template" value="&lt;data:content/&gt;" />'
        +'</form>');
    $('body').append(hiddenFormForBlogger);
    $('#bloggerForm').submit();
}

function toggleAlbumOrCompilationFields()
{
    $('input[name=compilation]').click(function(){
        if($(this).is(':checked'))
        {
            // is compilation
            $('input[name=artist]').parents('.row').fadeOut('slow');
            $('input[name=label]').parents('.row').fadeOut('slow');
            $('select[name=album.genre]').parents('.row').fadeOut('slow');
        } else {
            // is album
            $('input[name=artist]').parents('.row').fadeIn('slow');
            $('input[name=label]').parents('.row').fadeIn('slow');
            $('select[name=album.genre]').parents('.row').fadeIn('slow');
        }
    });
}

function checkIfAgreesAndContinueTo(url)   {
    var e = 0;
    $('input[type=checkbox]').each(function(){
        if($(this).parent().find(':checked').length == 0)
        {
            $(this).parent().addClass('checkboxError');
            e = 1;
        } else if($(this).hasClass('checkboxError')) {
            $(this).parent().removeClass('checkboxError');
        }
    });

    if(e == 0)
    {
        window.location=url;
    }
}

$(function(){
    if($('input[name="user.password"][id!=donotcheck]').length > 0 && $('input[name="password"][id!=donotcheck]').length > 0)
    {
        var lengthChecker = $('<img src="/images/minus.gif" class="right checkMark"/>');
        $('input[name="user.password"][id!=donotcheck]').focus(function(){
            $(this).after(lengthChecker.fadeIn('slow'));
        }).keyup(function(){
            if($(this).attr('value').length >= 6)
            {
                lengthChecker.attr('src', '/images/isInFeaturedList.gif');
            }
            if($(this).attr('value').length < 6)
            {
                lengthChecker.attr('src', '/images/minus.gif');
            }
        }).blur(function(){
            lengthChecker.fadeOut('slow', function(){$(this).remove();})
        });

        var equalPassChecker = $('<img src="/images/minus.gif" class="right checkMark"/>');
        $('input[name="password"][id!=donotcheck]').focus(function(){
            $(this).after(equalPassChecker.fadeIn('slow'));
        }).keyup(function(){
            if($(this).attr('value') == $('input[name="user.password"]').attr('value'))
            {
                equalPassChecker.attr('src', '/images/isInFeaturedList.gif');
            } else {
                equalPassChecker.attr('src', '/images/minus.gif');
            }
        }).blur(function(){
            equalPassChecker.fadeOut('slow', function(){$(this).remove();})
        });
    }

    if($('.previewTab').length>0)
    {
        $('.previewTab').each(function(){
            if($(this).hasClass('jsversion'))
            {
                $(this).click(function(){
                    $('#minishopPreviewSocial').hide();
                    $('#minishopPreviewJS').show();
                    $('#embedsourcecodeNoJS').addClass('hidden');
                    $('#embedsourcecodeJS').removeClass('hidden');
                    $('.copyembedcodeNoJS').addClass('hidden');
                    $('.copyembedcodeJS').removeClass('hidden');
                    $('.previewTab').removeClass('on');
                    $(this).addClass('on');
                    return false;
                });
            }
            if($(this).hasClass('socialversion'))
            {
                $(this).click(function(){
                    $('#minishopPreviewSocial').show();
                    $('#minishopPreviewJS').hide();
                    $('#embedsourcecodeNoJS').removeClass('hidden');
                    $('#embedsourcecodeJS').addClass('hidden');
                    $('.copyembedcodeNoJS').removeClass('hidden');
                    $('.copyembedcodeJS').addClass('hidden');
                    $('.previewTab').removeClass('on');
                    $(this).addClass('on');
                    return false;
                });
            }
            if($(this).hasClass('generalVersion'))
            {
                $(this).click(function(){
                    $(this).addClass('on');
                    $('.myspaceVersion').removeClass('on');
                    $('#minishopV2embedMySpace').hide();
                    $('#minishopV2embed').show();
                    return false;
                });
            }
            if($(this).hasClass('myspaceVersion'))
            {
                $(this).click(function(){
                    $(this).addClass('on');
                    $('.generalVersion').removeClass('on');
                    $('#minishopV2embed').hide();
                    $('#minishopV2embedMySpace').show();
                    return false;
                });
            }
        });
    }

    rounded_buttons();
    bindSpamAvoider();

    if($('.productSelectorBar').length>0)
    {
        $('.productSelectorBar .select a:first').click(function(){
            $(this).parent('.button_left').addClass('on');
            $(this).parent().parent().find('ul.popup').mouseenter(function(){$(this).mouseleave(function(){$(this).fadeOut('slow'); $(this).parents('.select').find('.on').removeClass('on');});}).fadeIn('slow');
            return false;
        });
    }

    if($('.hugeButton').length > 0)
    {
        $('.hugeButton').mouseenter(function(){
            $(this).addClass('on');
        }).mouseleave(function(){
            $(this).removeClass('on');
        });
    }
});
