/**
 * @Package: WordPress Plugin
 * @Subpackage: Ultra Portfolio
 * @Since: Ultra Portfolio 1.0
 * @Current Version: Ultra Portfolio 3.1
 * @WordPress Version: 3.5 or above
 * This file is part of Ultra Portofolio WordPress Plugin.
 */

var $pj = jQuery.noConflict();


jQuery(document).ready(function($pj) {
    var which_one = "0";
    $pj('#thumbnail').click(function() {
        which_one = "1";
        tb_show('Upload a thumbnail', 'media-upload.php?referer=uport_add_new_portfolio_item&amp;type=image&amp;TB_iframe=true&amp;post_id=0', false);
        return false;
    });

    $pj('#thumbnail_grid1').click(function() {
        which_one = "grid1";
        tb_show('Upload a thumbnail', 'media-upload.php?referer=uport_add_new_portfolio_item&amp;type=image&amp;TB_iframe=true&amp;post_id=0', false);
        return false;
    });

    $pj('#image').click(function() {
        which_one = "2";
        tb_show('Upload an image', 'media-upload.php?referer=uport_add_new_portfolio_item&amp;type=image&amp;TB_iframe=true&amp;post_id=0', false);
        return false;
    });

    $pj('#extvideourl').click(function() {
        which_one = "3";
        tb_show('Upload Video', 'media-upload.php?referer=uport_add_new_portfolio_item&amp;type=image&amp;TB_iframe=true&amp;post_id=0', false);
        return false;
    });

    $pj('#extaudiourl').click(function() {
        which_one = "4";
        tb_show('Upload Audio', 'media-upload.php?referer=uport_add_new_portfolio_item&amp;type=image&amp;TB_iframe=true&amp;post_id=0', false);
        return false;
    });


    window.send_to_editor = function(selectedImg) {
		//console.log(selectedImg);
        selectedImg = selectedImg.replace(/"/g, "'");
        var startIndex = selectedImg.indexOf("href='") + 6;
        var endIndex = selectedImg.indexOf("'>", startIndex);
        var image_url = selectedImg.substring(startIndex, endIndex);

        if(selectedImg.indexOf("href='") == "-1"){
            var div = document.createElement('div');
            div.innerHTML = selectedImg;
            var firstImage = div.getElementsByTagName('img')[0];
            var imgSrc = firstImage ? firstImage.src : "";
            // or, if you want the unresolved src, as it appears in the original HTML:
            var rawImgSrc = firstImage ? firstImage.getAttribute("src") : "";
            //console.log(imgSrc);
            //console.log(rawImgSrc);
            var image_url = rawImgSrc;
        }





        if (which_one == "1") {
            $pj('#thumbnail_adr').val(image_url);
            $pj('#thumbnail_adr_area').html(selectedImg);
        } else if (which_one == "grid1") {
            $pj('#thumbnail_grid1adr').val(image_url);
            $pj('#thumbnail_grid1adr_area').html(selectedImg);
        } else if (which_one == "2") {
            $pj('#image_adr').val(image_url);
            $pj('#image_adr_area').html(selectedImg);
        } else if (which_one == "3") {
            $pj('#extvideo_adr').val(image_url);
            $pj('#extvideourl_area').html(selectedImg);
        } else if (which_one == "4") {
            $pj('#extaudio_adr').val(image_url);
            $pj('#extaudiourl_area').html(selectedImg);
        }
        tb_remove();

    }
});


/* DATEPICKER */
/*jQuery(document).ready(function($pj) {
    $pj('#date').datepicker({showAnim: 'fadeIn'});
});*/