// JavaScript Document
/*function getHostName(){
	var domainName="";
	var hostName=window.location.hostname;
	if(hostName=='10.10.10.30'){domainName='http://10.10.10.30/cosco.in/'}
	else if(hostName=='localhost'){domainName='http://localhost/cosco.in/'}
	else if(hostName.indexOf('jil.in')>-1){domainName='http://'+hostName+'/cosco.in/'}
	else{if(window.location.protocol=='https:'){domainName='https://'+hostName+'/'}
	else{domainName='http://'+hostName+'/'}}
	return domainName
}*/

//alert(getHostName());

// GET URL ID FOR PRODUCT
var urlfull = document.URL;
var prodid = urlfull.substr(urlfull.lastIndexOf('/') + 1);
//var prodid = 5;
var urlpos = urlfull.search("/product/");

//alert(urlfull);
/*if(urlpos > 0){
	var car_controller = 'admin/product_gallery';	
}
else{
	var car_controller = 'admin/product_gallery';
}*/
var car_controller = 'admin/product_gallery';

//alert(n);

//this is the application.js file from the example code//
$(function () {
    'use strict';

    // Initialize the jQuery File Upload widget:
    $('#fileupload').fileupload();
   
    // Enable iframe cross-domain access via redirect option:
    $('#fileupload').fileupload(
        'option',
        'redirect',
        window.location.href.replace(
            /\/[^\/]*$/,
            './cors/result.html?%s'
            )
        );
            
           $('#download-files > .template-download > .add').each(function(e){
				alert(e);
			});
	//alert(getHostName());
	var mnurl = getHostName()+car_controller+'/get_files/'+prodid+'/';
	//alert(window.location.hostname);
    if ( (window.location.hostname === '10.10.10.30') || (window.location.hostname === 'jil.in') || (window.location.hostname === 'www.cosco.in') ) {
       var nurl = mnurl;
	  // alert(nurl);
	  	// Upload server status check for browsers with CORS support:
        if ($.ajaxSettings.xhr().withCredentials !== undefined) {
			//alert('hi');
			$.ajax({
                url: nurl,
                dataType: 'json', 
                
                success : function(data) { 
				
                    var fu = $('#fileupload').data('fileupload'), 
                    template;
                    fu._adjustMaxNumberOfFiles(-data.length);
                    template = fu._renderDownload(data)
                    .appendTo($('#fileupload .files'));
                    
                    // Force reflow:
                    fu._reflow = fu._transition && template.length &&
                    template[0].offsetWidth;
                    template.addClass('in');
                    $('#loading').remove();
                }  
         
                
            }).fail(function () {
                $('<span class="alert alert-error"/>')
                .text('Upload server currently unavailable - ' +
                    new Date())
                .appendTo('#fileupload');
            });
        }
    } else {
		// Load existing files:
		$('#fileupload').each(function () {
            var that = this;
            $.getJSON(this.action, function (result) {
                if (result && result.length) {
                    $(that).fileupload('option', 'done')
                    .call(that, null, {
                        result: result
                    });
                }
            });
        });
    }


    // Open download dialogs via iframes,
    // to prevent aborting current uploads:
    $('#fileupload .files a:not([target^=_blank])').live('click', function (e) {
        e.preventDefault();
        $('<iframe style="display:none;"></iframe>')
        .prop('src', this.href)
        .appendTo('body');
    });

});